bpo-43296: Handle sqlite3_value_blob() errors (GH-24674)

This commit is contained in:
Erlend Egeberg Aasland 2021-04-14 23:09:11 +02:00 committed by GitHub
parent e07f4ab26a
commit 5cb601f956
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 24 additions and 7 deletions

View file

@ -276,6 +276,10 @@ class FunctionTests(unittest.TestCase):
val = cur.fetchone()[0] val = cur.fetchone()[0]
self.assertEqual(val, 2) self.assertEqual(val, 2)
def test_empty_blob(self):
cur = self.con.execute("select isblob(x'')")
self.assertTrue(cur.fetchone()[0])
# Regarding deterministic functions: # Regarding deterministic functions:
# #
# Between 3.8.3 and 3.15.0, deterministic functions were only used to # Between 3.8.3 and 3.15.0, deterministic functions were only used to

View file

@ -0,0 +1,3 @@
Improve :mod:`sqlite3` error handling: ``sqlite3_value_blob()`` errors that
set ``SQLITE_NOMEM`` now raise :exc:`MemoryError`. Patch by Erlend E.
Aasland.

View file

@ -546,7 +546,6 @@ _pysqlite_build_py_params(sqlite3_context *context, int argc,
sqlite3_value* cur_value; sqlite3_value* cur_value;
PyObject* cur_py_value; PyObject* cur_py_value;
const char* val_str; const char* val_str;
Py_ssize_t buflen;
args = PyTuple_New(argc); args = PyTuple_New(argc);
if (!args) { if (!args) {
@ -571,19 +570,26 @@ _pysqlite_build_py_params(sqlite3_context *context, int argc,
cur_py_value = Py_NewRef(Py_None); cur_py_value = Py_NewRef(Py_None);
} }
break; break;
case SQLITE_BLOB: case SQLITE_BLOB: {
buflen = sqlite3_value_bytes(cur_value); sqlite3 *db = sqlite3_context_db_handle(context);
cur_py_value = PyBytes_FromStringAndSize( const void *blob = sqlite3_value_blob(cur_value);
sqlite3_value_blob(cur_value), buflen);
if (blob == NULL && sqlite3_errcode(db) == SQLITE_NOMEM) {
PyErr_NoMemory();
goto error;
}
Py_ssize_t size = sqlite3_value_bytes(cur_value);
cur_py_value = PyBytes_FromStringAndSize(blob, size);
break; break;
}
case SQLITE_NULL: case SQLITE_NULL:
default: default:
cur_py_value = Py_NewRef(Py_None); cur_py_value = Py_NewRef(Py_None);
} }
if (!cur_py_value) { if (!cur_py_value) {
Py_DECREF(args); goto error;
return NULL;
} }
PyTuple_SetItem(args, i, cur_py_value); PyTuple_SetItem(args, i, cur_py_value);
@ -591,6 +597,10 @@ _pysqlite_build_py_params(sqlite3_context *context, int argc,
} }
return args; return args;
error:
Py_DECREF(args);
return NULL;
} }
static void static void