Skip to content

Commit 4900d9f

Browse files
author
Erlend E. Aasland
committed
Handle sqlite3_value_blob() errors
1 parent f1eceaf commit 4900d9f

File tree

1 file changed

+20
-4
lines changed

1 file changed

+20
-4
lines changed

Modules/_sqlite/connection.c

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -572,8 +572,21 @@ _pysqlite_build_py_params(sqlite3_context *context, int argc,
572572
break;
573573
case SQLITE_BLOB: {
574574
const void *blob = sqlite3_value_blob(cur_value);
575-
Py_ssize_t size = sqlite3_value_bytes(cur_value);
576-
cur_py_value = PyBytes_FromStringAndSize(blob, size);
575+
576+
if (blob) {
577+
Py_ssize_t size = sqlite3_value_bytes(cur_value);
578+
cur_py_value = PyBytes_FromStringAndSize(blob, size);
579+
}
580+
else {
581+
sqlite3 *db = sqlite3_context_db_handle(context);
582+
assert(db != NULL);
583+
if (sqlite3_errcode(db) == SQLITE_NOMEM) {
584+
PyErr_NoMemory();
585+
goto error;
586+
}
587+
// Zero-sized blob.
588+
cur_py_value = PyBytes_FromStringAndSize(NULL, 0);
589+
}
577590
break;
578591
}
579592
case SQLITE_NULL:
@@ -582,15 +595,18 @@ _pysqlite_build_py_params(sqlite3_context *context, int argc,
582595
}
583596

584597
if (!cur_py_value) {
585-
Py_DECREF(args);
586-
return NULL;
598+
goto error;
587599
}
588600

589601
PyTuple_SetItem(args, i, cur_py_value);
590602

591603
}
592604

593605
return args;
606+
607+
error:
608+
Py_DECREF(args);
609+
return NULL;
594610
}
595611

596612
static void

0 commit comments

Comments
 (0)