From 9d7765da487c3291e332dd699ba02681e0471517 Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Thu, 18 Feb 2021 12:35:42 +0100 Subject: [PATCH 1/2] bpo-43249: sqlite3_column_bytes() must follow sqlite_column_blob() --- Modules/_sqlite/cursor.c | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/Modules/_sqlite/cursor.c b/Modules/_sqlite/cursor.c index f8fe11ed1ea75b..486d25bbecd51d 100644 --- a/Modules/_sqlite/cursor.c +++ b/Modules/_sqlite/cursor.c @@ -278,9 +278,14 @@ _pysqlite_fetch_one_row(pysqlite_Cursor* self) converter = Py_None; } + /* + * Note, sqlite3_column_bytes() must come after sqlite3_column_blob() + * or sqlite3_column_text(). + * Ref. https://sqlite.org/c3ref/column_blob.html + */ if (converter != Py_None) { - nbytes = sqlite3_column_bytes(self->statement->st, i); val_str = (const char*)sqlite3_column_blob(self->statement->st, i); + nbytes = sqlite3_column_bytes(self->statement->st, i); if (!val_str) { converted = Py_NewRef(Py_None); } else { @@ -330,9 +335,13 @@ _pysqlite_fetch_one_row(pysqlite_Cursor* self) } } else { /* coltype == SQLITE_BLOB */ - nbytes = sqlite3_column_bytes(self->statement->st, i); - converted = PyBytes_FromStringAndSize( - sqlite3_column_blob(self->statement->st, i), nbytes); + const char *blob = sqlite3_column_blob(self->statement->st, i); + if (!blob) { + converted = Py_NewRef(Py_None); + } else { + nbytes = sqlite3_column_bytes(self->statement->st, i); + converted = PyBytes_FromStringAndSize(blob, nbytes); + } } } From d6be86f4f312b404f405391ca2b370bccd120cca Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Thu, 18 Feb 2021 16:03:29 +0100 Subject: [PATCH 2/2] Address review: fix comment --- Modules/_sqlite/cursor.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Modules/_sqlite/cursor.c b/Modules/_sqlite/cursor.c index 486d25bbecd51d..63176b81b10efb 100644 --- a/Modules/_sqlite/cursor.c +++ b/Modules/_sqlite/cursor.c @@ -281,7 +281,8 @@ _pysqlite_fetch_one_row(pysqlite_Cursor* self) /* * Note, sqlite3_column_bytes() must come after sqlite3_column_blob() * or sqlite3_column_text(). - * Ref. https://sqlite.org/c3ref/column_blob.html + * + * See https://sqlite.org/c3ref/column_blob.html for details. */ if (converter != Py_None) { val_str = (const char*)sqlite3_column_blob(self->statement->st, i);