Skip to content

Commit 5cb601f

Browse files
author
Erlend Egeberg Aasland
authored
bpo-43296: Handle sqlite3_value_blob() errors (GH-24674)
1 parent e07f4ab commit 5cb601f

File tree

3 files changed

+24
-7
lines changed

3 files changed

+24
-7
lines changed

Lib/sqlite3/test/userfunctions.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,10 @@ def test_any_arguments(self):
276276
val = cur.fetchone()[0]
277277
self.assertEqual(val, 2)
278278

279+
def test_empty_blob(self):
280+
cur = self.con.execute("select isblob(x'')")
281+
self.assertTrue(cur.fetchone()[0])
282+
279283
# Regarding deterministic functions:
280284
#
281285
# Between 3.8.3 and 3.15.0, deterministic functions were only used to
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Improve :mod:`sqlite3` error handling: ``sqlite3_value_blob()`` errors that
2+
set ``SQLITE_NOMEM`` now raise :exc:`MemoryError`. Patch by Erlend E.
3+
Aasland.

Modules/_sqlite/connection.c

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -546,7 +546,6 @@ _pysqlite_build_py_params(sqlite3_context *context, int argc,
546546
sqlite3_value* cur_value;
547547
PyObject* cur_py_value;
548548
const char* val_str;
549-
Py_ssize_t buflen;
550549

551550
args = PyTuple_New(argc);
552551
if (!args) {
@@ -571,26 +570,37 @@ _pysqlite_build_py_params(sqlite3_context *context, int argc,
571570
cur_py_value = Py_NewRef(Py_None);
572571
}
573572
break;
574-
case SQLITE_BLOB:
575-
buflen = sqlite3_value_bytes(cur_value);
576-
cur_py_value = PyBytes_FromStringAndSize(
577-
sqlite3_value_blob(cur_value), buflen);
573+
case SQLITE_BLOB: {
574+
sqlite3 *db = sqlite3_context_db_handle(context);
575+
const void *blob = sqlite3_value_blob(cur_value);
576+
577+
if (blob == NULL && sqlite3_errcode(db) == SQLITE_NOMEM) {
578+
PyErr_NoMemory();
579+
goto error;
580+
}
581+
582+
Py_ssize_t size = sqlite3_value_bytes(cur_value);
583+
cur_py_value = PyBytes_FromStringAndSize(blob, size);
578584
break;
585+
}
579586
case SQLITE_NULL:
580587
default:
581588
cur_py_value = Py_NewRef(Py_None);
582589
}
583590

584591
if (!cur_py_value) {
585-
Py_DECREF(args);
586-
return NULL;
592+
goto error;
587593
}
588594

589595
PyTuple_SetItem(args, i, cur_py_value);
590596

591597
}
592598

593599
return args;
600+
601+
error:
602+
Py_DECREF(args);
603+
return NULL;
594604
}
595605

596606
static void

0 commit comments

Comments
 (0)