Skip to content

Commit 2183d06

Browse files
author
Erlend Egeberg Aasland
authored
bpo-43251: sqlite3_column_name() failures now raise MemoryError (GH-24609)
1 parent 1e3c682 commit 2183d06

File tree

2 files changed

+31
-24
lines changed

2 files changed

+31
-24
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Improve :mod:`sqlite3` error handling: ``sqlite3_column_name()`` failures
2+
now result in :exc:`MemoryError`. Patch by Erlend E. Aasland.

Modules/_sqlite/cursor.c

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,6 @@ pysqlite_build_row_cast_map(pysqlite_Cursor* self)
135135
{
136136
int i;
137137
const char* pos;
138-
const char* colname;
139138
const char* decltype;
140139
PyObject* converter;
141140

@@ -152,21 +151,24 @@ pysqlite_build_row_cast_map(pysqlite_Cursor* self)
152151
converter = NULL;
153152

154153
if (self->connection->detect_types & PARSE_COLNAMES) {
155-
colname = sqlite3_column_name(self->statement->st, i);
156-
if (colname) {
157-
const char *type_start = NULL;
158-
for (pos = colname; *pos != 0; pos++) {
159-
if (*pos == '[') {
160-
type_start = pos + 1;
161-
}
162-
else if (*pos == ']' && type_start != NULL) {
163-
converter = _pysqlite_get_converter(type_start, pos - type_start);
164-
if (!converter && PyErr_Occurred()) {
165-
Py_CLEAR(self->row_cast_map);
166-
return -1;
167-
}
168-
break;
154+
const char *colname = sqlite3_column_name(self->statement->st, i);
155+
if (colname == NULL) {
156+
PyErr_NoMemory();
157+
Py_CLEAR(self->row_cast_map);
158+
return -1;
159+
}
160+
const char *type_start = NULL;
161+
for (pos = colname; *pos != 0; pos++) {
162+
if (*pos == '[') {
163+
type_start = pos + 1;
164+
}
165+
else if (*pos == ']' && type_start != NULL) {
166+
converter = _pysqlite_get_converter(type_start, pos - type_start);
167+
if (!converter && PyErr_Occurred()) {
168+
Py_CLEAR(self->row_cast_map);
169+
return -1;
169170
}
171+
break;
170172
}
171173
}
172174
}
@@ -210,10 +212,6 @@ _pysqlite_build_column_name(pysqlite_Cursor *self, const char *colname)
210212
const char* pos;
211213
Py_ssize_t len;
212214

213-
if (!colname) {
214-
Py_RETURN_NONE;
215-
}
216-
217215
if (self->connection->detect_types & PARSE_COLNAMES) {
218216
for (pos = colname; *pos; pos++) {
219217
if (*pos == '[') {
@@ -311,8 +309,9 @@ _pysqlite_fetch_one_row(pysqlite_Cursor* self)
311309
if (!converted && PyErr_ExceptionMatches(PyExc_UnicodeDecodeError)) {
312310
PyErr_Clear();
313311
colname = sqlite3_column_name(self->statement->st, i);
314-
if (!colname) {
315-
colname = "<unknown column name>";
312+
if (colname == NULL) {
313+
PyErr_NoMemory();
314+
goto error;
316315
}
317316
PyOS_snprintf(buf, sizeof(buf) - 1, "Could not decode to UTF-8 column '%s' with text '%s'",
318317
colname , text);
@@ -550,9 +549,15 @@ _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject* operation
550549
if (!descriptor) {
551550
goto error;
552551
}
553-
column_name = _pysqlite_build_column_name(self,
554-
sqlite3_column_name(self->statement->st, i));
555-
if (!column_name) {
552+
const char *colname;
553+
colname = sqlite3_column_name(self->statement->st, i);
554+
if (colname == NULL) {
555+
PyErr_NoMemory();
556+
Py_DECREF(descriptor);
557+
goto error;
558+
}
559+
column_name = _pysqlite_build_column_name(self, colname);
560+
if (column_name == NULL) {
556561
Py_DECREF(descriptor);
557562
goto error;
558563
}

0 commit comments

Comments
 (0)