#10811: Fix recursive usage of cursors. Instead of crashing, raise a ProgrammingError now.

This commit is contained in:
Gerhard Haering 2011-05-09 12:24:09 +02:00
parent 83b8c0be93
commit 936d518dc8
3 changed files with 43 additions and 10 deletions

View File

@ -281,6 +281,29 @@ class RegressionTests(unittest.TestCase):
# Lone surrogate cannot be encoded to the default encoding (utf8) # Lone surrogate cannot be encoded to the default encoding (utf8)
"\uDC80", collation_cb) "\uDC80", collation_cb)
def CheckRecursiveCursorUse(self):
"""
http://bugs.python.org/issue10811
Recursively using a cursor, such as when reusing it from a generator led to segfaults.
Now we catch recursive cursor usage and raise a ProgrammingError.
"""
con = sqlite.connect(":memory:")
cur = con.cursor()
cur.execute("create table a (bar)")
cur.execute("create table b (baz)")
def foo():
cur.execute("insert into a (bar) values (?)", (1,))
yield 1
try:
cur.executemany("insert into b (baz) values (?)", ((i,) for i in foo()))
self.fail("should have raised ProgrammingError")
except sqlite.ProgrammingError:
pass
def suite(): def suite():
regression_suite = unittest.makeSuite(RegressionTests, "Check") regression_suite = unittest.makeSuite(RegressionTests, "Check")
return unittest.TestSuite((regression_suite,)) return unittest.TestSuite((regression_suite,))

View File

@ -430,9 +430,14 @@ static int check_cursor(pysqlite_Cursor* cur)
if (cur->closed) { if (cur->closed) {
PyErr_SetString(pysqlite_ProgrammingError, "Cannot operate on a closed cursor."); PyErr_SetString(pysqlite_ProgrammingError, "Cannot operate on a closed cursor.");
return 0; return 0;
} else {
return pysqlite_check_thread(cur->connection) && pysqlite_check_connection(cur->connection);
} }
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);
} }
PyObject* _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject* args) PyObject* _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject* args)
@ -455,9 +460,10 @@ PyObject* _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject*
int allow_8bit_chars; int allow_8bit_chars;
if (!check_cursor(self)) { if (!check_cursor(self)) {
return NULL; goto error;
} }
self->locked = 1;
self->reset = 0; self->reset = 0;
/* Make shooting yourself in the foot with not utf-8 decodable 8-bit-strings harder */ /* Make shooting yourself in the foot with not utf-8 decodable 8-bit-strings harder */
@ -470,12 +476,12 @@ PyObject* _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject*
if (multiple) { if (multiple) {
/* executemany() */ /* executemany() */
if (!PyArg_ParseTuple(args, "OO", &operation, &second_argument)) { if (!PyArg_ParseTuple(args, "OO", &operation, &second_argument)) {
return NULL; goto error;
} }
if (!PyUnicode_Check(operation)) { if (!PyUnicode_Check(operation)) {
PyErr_SetString(PyExc_ValueError, "operation parameter must be str"); PyErr_SetString(PyExc_ValueError, "operation parameter must be str");
return NULL; goto error;
} }
if (PyIter_Check(second_argument)) { if (PyIter_Check(second_argument)) {
@ -486,23 +492,23 @@ PyObject* _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject*
/* sequence */ /* sequence */
parameters_iter = PyObject_GetIter(second_argument); parameters_iter = PyObject_GetIter(second_argument);
if (!parameters_iter) { if (!parameters_iter) {
return NULL; goto error;
} }
} }
} else { } else {
/* execute() */ /* execute() */
if (!PyArg_ParseTuple(args, "O|O", &operation, &second_argument)) { if (!PyArg_ParseTuple(args, "O|O", &operation, &second_argument)) {
return NULL; goto error;
} }
if (!PyUnicode_Check(operation)) { if (!PyUnicode_Check(operation)) {
PyErr_SetString(PyExc_ValueError, "operation parameter must be str"); PyErr_SetString(PyExc_ValueError, "operation parameter must be str");
return NULL; goto error;
} }
parameters_list = PyList_New(0); parameters_list = PyList_New(0);
if (!parameters_list) { if (!parameters_list) {
return NULL; goto error;
} }
if (second_argument == NULL) { if (second_argument == NULL) {
@ -742,7 +748,8 @@ error:
* ROLLBACK could have happened */ * ROLLBACK could have happened */
#ifdef SQLITE_VERSION_NUMBER #ifdef SQLITE_VERSION_NUMBER
#if SQLITE_VERSION_NUMBER >= 3002002 #if SQLITE_VERSION_NUMBER >= 3002002
self->connection->inTransaction = !sqlite3_get_autocommit(self->connection->db); if (self->connection && self->connection->db)
self->connection->inTransaction = !sqlite3_get_autocommit(self->connection->db);
#endif #endif
#endif #endif
@ -750,6 +757,8 @@ error:
Py_XDECREF(parameters_iter); Py_XDECREF(parameters_iter);
Py_XDECREF(parameters_list); Py_XDECREF(parameters_list);
self->locked = 0;
if (PyErr_Occurred()) { if (PyErr_Occurred()) {
self->rowcount = -1L; self->rowcount = -1L;
return NULL; return NULL;

View File

@ -42,6 +42,7 @@ typedef struct
pysqlite_Statement* statement; pysqlite_Statement* statement;
int closed; int closed;
int reset; int reset;
int locked;
int initialized; int initialized;
/* the next row to be returned, NULL if no next row available */ /* the next row to be returned, NULL if no next row available */