diff options
| author | arcadia-devtools <[email protected]> | 2022-06-09 19:02:01 +0300 |
|---|---|---|
| committer | arcadia-devtools <[email protected]> | 2022-06-09 19:02:01 +0300 |
| commit | 4a29d649866ff133e0b8f8a1009e1000a44d7279 (patch) | |
| tree | 547229aded91b3760628c646a144af604f1c3e2b /contrib/tools/python3/src/Modules/_sqlite | |
| parent | 782f2445a283aed9a66e699137b3349af1689c29 (diff) | |
intermediate changes
ref:478170c7a5a1c0788ddd0d6513ce4ed86d7d7c99
Diffstat (limited to 'contrib/tools/python3/src/Modules/_sqlite')
| -rw-r--r-- | contrib/tools/python3/src/Modules/_sqlite/connection.c | 63 | ||||
| -rw-r--r-- | contrib/tools/python3/src/Modules/_sqlite/cursor.c | 46 |
2 files changed, 80 insertions, 29 deletions
diff --git a/contrib/tools/python3/src/Modules/_sqlite/connection.c b/contrib/tools/python3/src/Modules/_sqlite/connection.c index c9c10b41398..68c5aee79ab 100644 --- a/contrib/tools/python3/src/Modules/_sqlite/connection.c +++ b/contrib/tools/python3/src/Modules/_sqlite/connection.c @@ -1050,33 +1050,65 @@ static int _progress_handler(void* user_arg) * may change in future releases. Callback implementations should return zero * to ensure future compatibility. */ -static int _trace_callback(unsigned int type, void* user_arg, void* prepared_statement, void* statement_string) +static int +_trace_callback(unsigned int type, void *callable, void *stmt, void *sql) #else -static void _trace_callback(void* user_arg, const char* statement_string) +static void +_trace_callback(void *callable, const char *sql) #endif { - PyObject *py_statement = NULL; - PyObject *ret = NULL; - - PyGILState_STATE gilstate; - #ifdef HAVE_TRACE_V2 if (type != SQLITE_TRACE_STMT) { return 0; } #endif - gilstate = PyGILState_Ensure(); - py_statement = PyUnicode_DecodeUTF8(statement_string, - strlen(statement_string), "replace"); + PyGILState_STATE gilstate = PyGILState_Ensure(); + + PyObject *py_statement = NULL; +#ifdef HAVE_TRACE_V2 + const char *expanded_sql = sqlite3_expanded_sql((sqlite3_stmt *)stmt); + if (expanded_sql == NULL) { + sqlite3 *db = sqlite3_db_handle((sqlite3_stmt *)stmt); + if (sqlite3_errcode(db) == SQLITE_NOMEM) { + (void)PyErr_NoMemory(); + goto exit; + } + + PyErr_SetString(pysqlite_DataError, + "Expanded SQL string exceeds the maximum string length"); + if (_pysqlite_enable_callback_tracebacks) { + PyErr_Print(); + } else { + PyErr_Clear(); + } + + // Fall back to unexpanded sql + py_statement = PyUnicode_FromString((const char *)sql); + } + else { + py_statement = PyUnicode_FromString(expanded_sql); + sqlite3_free((void *)expanded_sql); + } +#else + if (sql == NULL) { + PyErr_SetString(pysqlite_DataError, + "Expanded SQL string exceeds the maximum string length"); + if (_pysqlite_enable_callback_tracebacks) { + PyErr_Print(); + } else { + PyErr_Clear(); + } + goto exit; + } + py_statement = PyUnicode_FromString(sql); +#endif if (py_statement) { - ret = PyObject_CallOneArg((PyObject*)user_arg, py_statement); + PyObject *ret = PyObject_CallOneArg((PyObject *)callable, py_statement); Py_DECREF(py_statement); + Py_XDECREF(ret); } - - if (ret) { - Py_DECREF(ret); - } else { + if (PyErr_Occurred()) { if (_pysqlite_enable_callback_tracebacks) { PyErr_Print(); } else { @@ -1084,6 +1116,7 @@ static void _trace_callback(void* user_arg, const char* statement_string) } } +exit: PyGILState_Release(gilstate); #ifdef HAVE_TRACE_V2 return 0; diff --git a/contrib/tools/python3/src/Modules/_sqlite/cursor.c b/contrib/tools/python3/src/Modules/_sqlite/cursor.c index 85267cc9e77..ac80c285fe9 100644 --- a/contrib/tools/python3/src/Modules/_sqlite/cursor.c +++ b/contrib/tools/python3/src/Modules/_sqlite/cursor.c @@ -26,6 +26,17 @@ #include "util.h" #include "clinic/cursor.c.h" +static inline int +check_cursor_locked(pysqlite_Cursor *cur) +{ + if (cur->locked) { + PyErr_SetString(pysqlite_ProgrammingError, + "Recursive use of cursors not allowed."); + return 0; + } + return 1; +} + /*[clinic input] module _sqlite3 class _sqlite3.Cursor "pysqlite_Cursor *" "pysqlite_CursorType" @@ -47,6 +58,10 @@ pysqlite_cursor_init_impl(pysqlite_Cursor *self, pysqlite_Connection *connection) /*[clinic end generated code: output=ac59dce49a809ca8 input=a8a4f75ac90999b2]*/ { + if (!check_cursor_locked(self)) { + return -1; + } + Py_INCREF(connection); Py_XSETREF(self->connection, connection); Py_CLEAR(self->statement); @@ -407,12 +422,9 @@ static int check_cursor(pysqlite_Cursor* cur) return 0; } - if (cur->locked) { - PyErr_SetString(pysqlite_ProgrammingError, "Recursive use of cursors not allowed."); - return 0; - } - - return pysqlite_check_thread(cur->connection) && pysqlite_check_connection(cur->connection); + return (pysqlite_check_thread(cur->connection) + && pysqlite_check_connection(cur->connection) + && check_cursor_locked(cur)); } static PyObject * @@ -810,27 +822,29 @@ pysqlite_cursor_iternext(pysqlite_Cursor *self) if (self->statement) { rc = pysqlite_step(self->statement->st, self->connection); if (PyErr_Occurred()) { - (void)pysqlite_statement_reset(self->statement); - Py_DECREF(next_row); - return NULL; + goto error; } if (rc != SQLITE_DONE && rc != SQLITE_ROW) { - (void)pysqlite_statement_reset(self->statement); - Py_DECREF(next_row); _pysqlite_seterror(self->connection->db, NULL); - return NULL; + goto error; } if (rc == SQLITE_ROW) { + self->locked = 1; // GH-80254: Prevent recursive use of cursors. self->next_row = _pysqlite_fetch_one_row(self); + self->locked = 0; if (self->next_row == NULL) { - (void)pysqlite_statement_reset(self->statement); - return NULL; + goto error; } } } return next_row; + +error: + (void)pysqlite_statement_reset(self->statement); + Py_DECREF(next_row); + return NULL; } /*[clinic input] @@ -973,6 +987,10 @@ static PyObject * pysqlite_cursor_close_impl(pysqlite_Cursor *self) /*[clinic end generated code: output=b6055e4ec6fe63b6 input=08b36552dbb9a986]*/ { + if (!check_cursor_locked(self)) { + return NULL; + } + if (!self->connection) { PyErr_SetString(pysqlite_ProgrammingError, "Base Cursor.__init__ not called."); |
