8000 bpo-44165: optimise sqlite3 statement preparation by passing string size by erlend-aasland · Pull Request #26206 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content
8000

bpo-44165: optimise sqlite3 statement preparation by passing string size #26206

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jun 2, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Raise OverflowError if SQL string is too long
  • Loading branch information
Erlend E. Aasland committed May 19, 2021
commit 081de33f3f63598c0f3b5dabd0eddd40fab29f06
3 changes: 3 additions & 0 deletions Modules/_sqlite/connection.c
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,9 @@ pysqlite_connection_init(pysqlite_Connection *self, PyObject *args,
return -1;
}

// Fetch the maximum length of an SQL string or BLOB
self->max_length = sqlite3_limit(self->db, SQLITE_LIMIT_LENGTH, -1);

self->Warning = pysqlite_Warning;
self->Error = pysqlite_Error;
self->InterfaceError = pysqlite_InterfaceError;
Expand Down
3 changes: 3 additions & 0 deletions Modules/_sqlite/connection.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ typedef struct
/* the timeout value in seconds for database locks */
double timeout;

/* the maximum length of an SQL string or BLOB */
int max_length;

/* for internal use in the timeout handler: when did the timeout handler
* first get called with count=0? */
double timeout_started;
Expand Down
6 changes: 5 additions & 1 deletion Modules/_sqlite/cursor.c
Original file line number Diff line number Diff line change
Expand Up @@ -695,6 +695,10 @@ pysqlite_cursor_executescript(pysqlite_Cursor *self, PyObject *script_obj)
if (!script_cstr) {
return NULL;
}
if (sql_len >= self->connection->max_length) {
PyErr_SetString(PyExc_OverflowError, "query string is too large");
return NULL;
}
} else {
PyErr_SetString(PyExc_ValueError, "script argument must be unicode.");
return NULL;
Expand All @@ -713,7 +717,7 @@ pysqlite_cursor_executescript(pysqlite_Cursor *self, PyObject *script_obj)
Py_BEGIN_ALLOW_THREADS
rc = sqlite3_prepare_v2(self->connection->db,
script_cstr,
(sql_len >= INT_MAX) ? -1 : (int)sql_len + 1,
(int)sql_len + 1,
&statement,
&tail);
Py_END_ALLOW_THREADS
Expand Down
12 changes: 5 additions & 7 deletions Modules/_sqlite/statement.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ int pysqlite_statement_create(pysqlite_Statement* self, pysqlite_Connection* con
rc = PYSQLITE_SQL_WRONG_TYPE;
return rc;
}
if (sql_cstr_len >= connection->max_length) {
PyErr_SetString(PyExc_OverflowError, "query string is too large");
return PYSQLITE_TOO_MUCH_SQL;
}
if (strlen(sql_cstr) != (size_t)sql_cstr_len) {
PyErr_SetString(PyExc_ValueError, "the query contains a null character");
return PYSQLITE_SQL_WRONG_TYPE;
Expand Down Expand Up @@ -93,16 +97,10 @@ int pysqlite_statement_create(pysqlite_Statement* self, pysqlite_Connection* con
break;
}

if (sql_cstr_len >= INT_MAX) {
sql_cstr_len = -1;
}
else {
sql_cstr_len += 1;
}
Py_BEGIN_ALLOW_THREADS
rc = sqlite3_prepare_v2(connection->db,
sql_cstr,
(int)sql_cstr_len,
(int)sql_cstr_len + 1,
&self->st,
&tail);
Py_END_ALLOW_THREADS
Expand Down
0