GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — bpo-36073: Raise ProgrammingError on recursive usage of cursors in sqlite converters by sir-sigurd · Pull Request #11984 · python/cpython

bpo-36073: Raise ProgrammingError on recursive usage of cursors in sqlite converters #11984

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.

Sign up for GitHub

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

Closed
sir-sigurd wants to merge 1 commit into python:main from sir-sigurd:sqlite-converter-segfault

Uh oh!

There was an error while loading. Please reload this page.

Closed

bpo-36073: Raise ProgrammingError on recursive usage of cursors in sqlite converters #11984

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
1 commit
Select commit
c79e784
bpo-36073: Raise ProgrammingError on recursive usage of cursors in sq…
sir-sigurd Feb 12, 2019
File filter
Filter by extension

Clear filters
Conversations
Failed to load comments.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Jump to
Jump to file
Failed to load files.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Diff view
Diff view

There are no files selected for viewing

37 changes: 36 additions & 1 deletion 37 Lib/sqlite3/test/regression.py
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -415,9 +415,44 @@ def test_return_empty_bytestring(self):
self.assertEqual(val, b'')


class ConverterProgrammingErrorTestCase(unittest.TestCase):
def setUp(self):
self.con = sqlite.connect(':memory:', detect_types=sqlite.PARSE_COLNAMES)
self.cur = self.con.cursor()
self.cur.execute('create table test(x foo)')

sqlite.converters['CURSOR_INIT'] = lambda x: self.cur.__init__(self.con)
sqlite.converters['CURSOR_CLOSE'] = lambda x: self.cur.close()
sqlite.converters['CURSOR_ITER'] = lambda x, l=[]: self.cur.fetchone() if l else l.append(None)

def tearDown(self):
del sqlite.converters['CURSOR_INIT']
del sqlite.converters['CURSOR_CLOSE']
del sqlite.converters['CURSOR_ITER']
self.cur.close()
self.con.close()

def test_cursor_init(self):
self.cur.execute('insert into test(x) values (?)', ('foo',))
with self.assertRaises(sqlite.ProgrammingError):
self.cur.execute('select x as "x [CURSOR_INIT]", x from test')

def test_cursor_close(self):
self.cur.execute('insert into test(x) values (?)', ('foo',))
with self.assertRaises(sqlite.ProgrammingError):
self.cur.execute('select x as "x [CURSOR_CLOSE]", x from test')

def test_cursor_iter(self):
self.cur.executemany('insert into test(x) values (?)', (('foo',),) * 2)
self.cur.execute('select x as "x [CURSOR_ITER]", x from test')
with self.assertRaises(sqlite.ProgrammingError):
self.cur.fetchone()


def suite():
tests = [
RegressionTests
RegressionTests,
ConverterProgrammingErrorTestCase,
]
return unittest.TestSuite(
[unittest.TestLoader().loadTestsFromTestCase(t) for t in tests]
Expand Down
2 changes: 2 additions & 0 deletions 2 Misc/NEWS.d/next/Library/2019-06-22-11-01-45.bpo-36073.ED8mB9.rst
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Raise :exc:`~sqlite3.ProgrammingError` on recursive usage of cursors in
:mod:`sqlite3` converters. Patch by Sergey Fedoseev.
32 changes: 26 additions & 6 deletions 32 Modules/_sqlite/cursor.c
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,19 @@
#include "util.h"
#include "clinic/cursor.c.h"

static 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"
Expand All @@ -47,6 +60,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);
Expand Down Expand Up @@ -384,12 +401,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 check_cursor_locked(cur) &&
pysqlite_check_thread(cur->connection) &&
pysqlite_check_connection(cur->connection);
}

static PyObject *
Expand Down Expand Up @@ -811,7 +825,9 @@ pysqlite_cursor_iternext(pysqlite_Cursor *self)
}

if (rc == SQLITE_ROW) {
self->locked = 1;
self->next_row = _pysqlite_fetch_one_row(self);
self->locked = 0;
if (self->next_row == NULL) {
(void)pysqlite_statement_reset(self->statement);
return NULL;
Expand Down Expand Up @@ -962,6 +978,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.");
Expand Down
Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.