bpo-43251: sqlite3_column_name() failures now raise MemoryError (GH-24609)

This commit is contained in:
Erlend Egeberg Aasland 2021-02-28 18:01:06 +01:00 committed by GitHub
parent 1e3c68246e
commit 2183d06bc8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 24 deletions

View File

@ -0,0 +1,2 @@
Improve :mod:`sqlite3` error handling: ``sqlite3_column_name()`` failures
now result in :exc:`MemoryError`. Patch by Erlend E. Aasland.

View File

@ -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;
}