Skip to content

Commit 47feb1f

Browse files
author
Erlend Egeberg Aasland
authored
bpo-43249: sqlite3_column_bytes() must follow sqlite_column_blob() (GH-24562)
1 parent 7be00ee commit 47feb1f

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

Modules/_sqlite/cursor.c

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -278,9 +278,15 @@ _pysqlite_fetch_one_row(pysqlite_Cursor* self)
278278
converter = Py_None;
279279
}
280280

281+
/*
282+
* Note, sqlite3_column_bytes() must come after sqlite3_column_blob()
283+
* or sqlite3_column_text().
284+
*
285+
* See https://sqlite.org/c3ref/column_blob.html for details.
286+
*/
281287
if (converter != Py_None) {
282-
nbytes = sqlite3_column_bytes(self->statement->st, i);
283288
val_str = (const char*)sqlite3_column_blob(self->statement->st, i);
289+
nbytes = sqlite3_column_bytes(self->statement->st, i);
284290
if (!val_str) {
285291
converted = Py_NewRef(Py_None);
286292
} else {
@@ -330,9 +336,13 @@ _pysqlite_fetch_one_row(pysqlite_Cursor* self)
330336
}
331337
} else {
332338
/* coltype == SQLITE_BLOB */
333-
nbytes = sqlite3_column_bytes(self->statement->st, i);
334-
converted = PyBytes_FromStringAndSize(
335-
sqlite3_column_blob(self->statement->st, i), nbytes);
339+
const char *blob = sqlite3_column_blob(self->statement->st, i);
340+
if (!blob) {
341+
converted = Py_NewRef(Py_None);
342+
} else {
343+
nbytes = sqlite3_column_bytes(self->statement->st, i);
344+
converted = PyBytes_FromStringAndSize(blob, nbytes);
345+
}
336346
}
337347
}
338348

0 commit comments

Comments
 (0)