diff options
-rw-r--r-- | Misc/NEWS.d/next/Library/2021-02-18-14-24-42.bpo-43251.n6WZDw.rst | 2 | ||||
-rw-r--r-- | Modules/_sqlite/cursor.c | 53 |
2 files changed, 31 insertions, 24 deletions
diff --git a/Misc/NEWS.d/next/Library/2021-02-18-14-24-42.bpo-43251.n6WZDw.rst b/Misc/NEWS.d/next/Library/2021-02-18-14-24-42.bpo-43251.n6WZDw.rst new file mode 100644 index 0000000..52ce9d9 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2021-02-18-14-24-42.bpo-43251.n6WZDw.rst @@ -0,0 +1,2 @@ +Improve :mod:`sqlite3` error handling: ``sqlite3_column_name()`` failures +now result in :exc:`MemoryError`. Patch by Erlend E. Aasland. diff --git a/Modules/_sqlite/cursor.c b/Modules/_sqlite/cursor.c index 7486073..ddacb27 100644 --- a/Modules/_sqlite/cursor.c +++ b/Modules/_sqlite/cursor.c @@ -135,7 +135,6 @@ pysqlite_build_row_cast_map(pysqlite_Cursor* self) { int i; const char* pos; - const char* colname; const char* decltype; PyObject* converter; @@ -152,21 +151,24 @@ pysqlite_build_row_cast_map(pysqlite_Cursor* self) converter = NULL; if (self->connection->detect_types & PARSE_COLNAMES) { - colname = sqlite3_column_name(self->statement->st, i); - if (colname) { - const char *type_start = NULL; - for (pos = colname; *pos != 0; pos++) { - if (*pos == '[') { - type_start = pos + 1; - } - else if (*pos == ']' && type_start != NULL) { - converter = _pysqlite_get_converter(type_start, pos - type_start); - if (!converter && PyErr_Occurred()) { - Py_CLEAR(self->row_cast_map); - return -1; - } - break; + const char *colname = sqlite3_column_name(self->statement->st, i); + if (colname == NULL) { + PyErr_NoMemory(); + Py_CLEAR(self->row_cast_map); + return -1; + } + const char *type_start = NULL; + for (pos = colname; *pos != 0; pos++) { + if (*pos == '[') { + type_start = pos + 1; + } + else if (*pos == ']' && type_start != NULL) { + converter = _pysqlite_get_converter(type_start, pos - type_start); + if (!converter && PyErr_Occurred()) { + Py_CLEAR(self->row_cast_map); + return -1; } + break; } } } @@ -210,10 +212,6 @@ _pysqlite_build_column_name(pysqlite_Cursor *self, const char *colname) const char* pos; Py_ssize_t len; - if (!colname) { - Py_RETURN_NONE; - } - if (self->connection->detect_types & PARSE_COLNAMES) { for (pos = colname; *pos; pos++) { if (*pos == '[') { @@ -311,8 +309,9 @@ _pysqlite_fetch_one_row(pysqlite_Cursor* self) if (!converted && PyErr_ExceptionMatches(PyExc_UnicodeDecodeError)) { PyErr_Clear(); colname = sqlite3_column_name(self->statement->st, i); - if (!colname) { - colname = "<unknown column name>"; + if (colname == NULL) { + PyErr_NoMemory(); + goto error; } PyOS_snprintf(buf, sizeof(buf) - 1, "Could not decode to UTF-8 column '%s' with text '%s'", colname , text); @@ -550,9 +549,15 @@ _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject* operation if (!descriptor) { goto error; } - column_name = _pysqlite_build_column_name(self, - sqlite3_column_name(self->statement->st, i)); - if (!column_name) { + const char *colname; + colname = sqlite3_column_name(self->statement->st, i); + if (colname == NULL) { + PyErr_NoMemory(); + Py_DECREF(descriptor); + goto error; + } + column_name = _pysqlite_build_column_name(self, colname); + if (column_name == NULL) { Py_DECREF(descriptor); goto error; } |