Skip to content

Commit c1323d4

Browse files
author
Erlend Egeberg Aasland
authored
bpo-45754: Use correct SQLite limit when checking statement length (GH-29489)
1 parent 4cdeee5 commit c1323d4

File tree

5 files changed

+14
-11
lines changed

5 files changed

+14
-11
lines changed

Lib/test/test_sqlite3/test_dbapi.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ def memory_database():
5555

5656
# Temporarily limit a database connection parameter
5757
@contextlib.contextmanager
58-
def cx_limit(cx, category=sqlite.SQLITE_LIMIT_LENGTH, limit=128):
58+
def cx_limit(cx, category=sqlite.SQLITE_LIMIT_SQL_LENGTH, limit=128):
5959
try:
6060
_prev = cx.setlimit(category, limit)
6161
yield limit
@@ -495,7 +495,7 @@ def test_connection_limits(self):
495495
prev_limit = self.cx.setlimit(category, new_limit)
496496
self.assertEqual(saved_limit, prev_limit)
497497
self.assertEqual(self.cx.getlimit(category), new_limit)
498-
msg = "string or blob too big"
498+
msg = "query string is too large"
499499
self.assertRaisesRegex(sqlite.DataError, msg,
500500
self.cx.execute, "select 1 as '16'")
501501
finally: # restore saved limit
@@ -1063,9 +1063,9 @@ def test_cursor_executescript_with_surrogates(self):
10631063
def test_cursor_executescript_too_large_script(self):
10641064
msg = "query string is too large"
10651065
with memory_database() as cx, cx_limit(cx) as lim:
1066-
cx.executescript("select 'almost too large'".ljust(lim-1))
1066+
cx.executescript("select 'almost too large'".ljust(lim))
10671067
with self.assertRaisesRegex(sqlite.DataError, msg):
1068-
cx.executescript("select 'too large'".ljust(lim))
1068+
cx.executescript("select 'too large'".ljust(lim+1))
10691069

10701070
def test_cursor_executescript_tx_control(self):
10711071
con = sqlite.connect(":memory:")

Lib/test/test_sqlite3/test_regression.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -362,11 +362,11 @@ def test_large_sql(self):
362362
with memory_database() as cx, cx_limit(cx) as lim:
363363
cu = cx.cursor()
364364

365-
cx("select 1".ljust(lim-1))
365+
cx("select 1".ljust(lim))
366366
# use a different SQL statement; don't reuse from the LRU cache
367-
cu.execute("select 2".ljust(lim-1))
367+
cu.execute("select 2".ljust(lim))
368368

369-
sql = "select 3".ljust(lim)
369+
sql = "select 3".ljust(lim+1)
370370
self.assertRaisesRegex(sqlite.DataError, msg, cx, sql)
371371
self.assertRaisesRegex(sqlite.DataError, msg, cu.execute, sql)
372372

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fix a regression in Python 3.11a1 and 3.11a2 where :mod:`sqlite3`
2+
incorrectly would use ``SQLITE_LIMIT_LENGTH`` when checking SQL statement
3+
lengths. Now, ``SQLITE_LIMIT_SQL_LENGTH`` is used. Patch by Erlend E. Aasland.

Modules/_sqlite/cursor.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -729,8 +729,8 @@ pysqlite_cursor_executescript_impl(pysqlite_Cursor *self,
729729

730730
size_t sql_len = strlen(sql_script);
731731
int max_length = sqlite3_limit(self->connection->db,
732-
SQLITE_LIMIT_LENGTH, -1);
733-
if (sql_len >= (unsigned)max_length) {
732+
SQLITE_LIMIT_SQL_LENGTH, -1);
733+
if (sql_len > (unsigned)max_length) {
734734
PyErr_SetString(self->connection->DataError,
735735
"query string is too large");
736736
return NULL;

Modules/_sqlite/statement.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ pysqlite_statement_create(pysqlite_Connection *connection, PyObject *sql)
6060
}
6161

6262
sqlite3 *db = connection->db;
63-
int max_length = sqlite3_limit(db, SQLITE_LIMIT_LENGTH, -1);
64-
if (size >= max_length) {
63+
int max_length = sqlite3_limit(db, SQLITE_LIMIT_SQL_LENGTH, -1);
64+
if (size > max_length) {
6565
PyErr_SetString(connection->DataError,
6666
"query string is too large");
6767
return NULL;

0 commit comments

Comments
 (0)