Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 42d67af

Browse files
Issue #21147: sqlite3 now raises an exception if the request contains a null
character instead of truncate it. Based on patch by Victor Stinner.
1 parent abf68ce commit 42d67af

4 files changed

Lines changed: 19 additions & 1 deletion

File tree

Lib/sqlite3/test/regression.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,16 @@ def CheckInvalidIsolationLevelType(self):
336336
sqlite.connect, ":memory:", isolation_level=123)
337337

338338

339+
def CheckNullCharacter(self):
340+
# Issue #21147
341+
con = sqlite.connect(":memory:")
342+
self.assertRaises(ValueError, con, "\0select 1")
343+
self.assertRaises(ValueError, con, "select 1\0")
344+
cur = con.cursor()
345+
self.assertRaises(ValueError, cur.execute, " \0select 2")
346+
self.assertRaises(ValueError, cur.execute, "select 2\0")
347+
348+
339349
def suite():
340350
regression_suite = unittest.makeSuite(RegressionTests, "Check")
341351
return unittest.TestSuite((regression_suite,))

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ Core and Builtins
3232
Library
3333
-------
3434

35+
- Issue #21147: sqlite3 now raises an exception if the request contains a null
36+
character instead of truncate it. Based on patch by Victor Stinner.
37+
3538
- Issue #21951: Fixed a crash in Tkinter on AIX when called Tcl command with
3639
empty string or tuple argument.
3740

Modules/_sqlite/connection.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1261,7 +1261,8 @@ PyObject* pysqlite_connection_call(pysqlite_Connection* self, PyObject* args, Py
12611261
if (rc == PYSQLITE_TOO_MUCH_SQL) {
12621262
PyErr_SetString(pysqlite_Warning, "You can only execute one statement at a time.");
12631263
} else if (rc == PYSQLITE_SQL_WRONG_TYPE) {
1264-
PyErr_SetString(pysqlite_Warning, "SQL is of wrong type. Must be string or unicode.");
1264+
if (PyErr_ExceptionMatches(PyExc_TypeError))
1265+
PyErr_SetString(pysqlite_Warning, "SQL is of wrong type. Must be string.");
12651266
} else {
12661267
(void)pysqlite_statement_reset(statement);
12671268
_pysqlite_seterror(self->db, NULL);

Modules/_sqlite/statement.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@ int pysqlite_statement_create(pysqlite_Statement* self, pysqlite_Connection* con
6363
rc = PYSQLITE_SQL_WRONG_TYPE;
6464
return rc;
6565
}
66+
if (strlen(sql_cstr) != (size_t)sql_cstr_len) {
67+
PyErr_SetString(PyExc_ValueError, "the query contains a null character");
68+
return PYSQLITE_SQL_WRONG_TYPE;
69+
}
6670

6771
self->in_weakreflist = NULL;
6872
Py_INCREF(sql);

0 commit comments

Comments
 (0)