Issue #21147: sqlite3 now raises an exception if the request contains a null
character instead of truncate it. Based on patch by Victor Stinner.
diff --git a/Modules/_sqlite/connection.c b/Modules/_sqlite/connection.c
index 7a8a5a1..0ed196d 100644
--- a/Modules/_sqlite/connection.c
+++ b/Modules/_sqlite/connection.c
@@ -1215,7 +1215,8 @@
if (rc == PYSQLITE_TOO_MUCH_SQL) {
PyErr_SetString(pysqlite_Warning, "You can only execute one statement at a time.");
} else if (rc == PYSQLITE_SQL_WRONG_TYPE) {
- PyErr_SetString(pysqlite_Warning, "SQL is of wrong type. Must be string or unicode.");
+ if (!PyErr_Occurred() || PyErr_ExceptionMatches(PyExc_TypeError))
+ PyErr_SetString(pysqlite_Warning, "SQL is of wrong type. Must be string or unicode.");
} else {
(void)pysqlite_statement_reset(statement);
_pysqlite_seterror(self->db, NULL);
diff --git a/Modules/_sqlite/statement.c b/Modules/_sqlite/statement.c
index 7a7a60f..edcebdd 100644
--- a/Modules/_sqlite/statement.c
+++ b/Modules/_sqlite/statement.c
@@ -74,12 +74,15 @@
rc = PYSQLITE_SQL_WRONG_TYPE;
return rc;
}
+ sql_cstr = PyString_AsString(sql_str);
+ if (strlen(sql_cstr) != (size_t)PyString_GET_SIZE(sql_str)) {
+ PyErr_SetString(PyExc_ValueError, "the query contains a null character");
+ return PYSQLITE_SQL_WRONG_TYPE;
+ }
self->in_weakreflist = NULL;
self->sql = sql_str;
- sql_cstr = PyString_AsString(sql_str);
-
Py_BEGIN_ALLOW_THREADS
rc = sqlite3_prepare(connection->db,
sql_cstr,