Skip to content

[3.9] bpo-43853: Handle sqlite3_value_text() errors (GH-25422). #27627

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 1 commit into from
Aug 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
12 changes: 7 additions & 5 deletions Lib/sqlite3/test/userfunctions.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,9 +236,11 @@ def CheckFuncException(self):

def CheckParamString(self):
cur = self.con.cursor()
cur.execute("select isstring(?)", ("foo",))
val = cur.fetchone()[0]
self.assertEqual(val, 1)
for text in ["foo", str()]:
with self.subTest(text=text):
cur.execute("select isstring(?)", (text,))
val = cur.fetchone()[0]
self.assertEqual(val, 1)

def CheckParamInt(self):
cur = self.con.cursor()
Expand Down Expand Up @@ -387,9 +389,9 @@ def CheckAggrExceptionInFinalize(self):

def CheckAggrCheckParamStr(self):
cur = self.con.cursor()
cur.execute("select checkType('str', ?)", ("foo",))
cur.execute("select checkTypes('str', ?, ?)", ("foo", str()))
val = cur.fetchone()[0]
self.assertEqual(val, 1)
self.assertEqual(val, 2)

def CheckAggrCheckParamInt(self):
cur = self.con.cursor()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Improve :mod:`sqlite3` error handling: ``sqlite3_value_text()`` errors that
set ``SQLITE_NOMEM`` now raise :exc:`MemoryError`. Patch by Erlend E.
Aasland.
27 changes: 16 additions & 11 deletions Modules/_sqlite/connection.c
Original file line number Diff line number Diff line change
Expand Up @@ -546,7 +546,6 @@ PyObject* _pysqlite_build_py_params(sqlite3_context *context, int argc, sqlite3_
int i;
sqlite3_value* cur_value;
PyObject* cur_py_value;
const char* val_str;
Py_ssize_t buflen;

args = PyTuple_New(argc);
Expand All @@ -563,16 +562,19 @@ PyObject* _pysqlite_build_py_params(sqlite3_context *context, int argc, sqlite3_
case SQLITE_FLOAT:
cur_py_value = PyFloat_FromDouble(sqlite3_value_double(cur_value));
break;
case SQLITE_TEXT:
val_str = (const char*)sqlite3_value_text(cur_value);
cur_py_value = PyUnicode_FromString(val_str);
/* TODO: have a way to show errors here */
if (!cur_py_value) {
PyErr_Clear();
Py_INCREF(Py_None);
cur_py_value = Py_None;
case SQLITE_TEXT: {
sqlite3 *db = sqlite3_context_db_handle(context);
const char *text = (const char *)sqlite3_value_text(cur_value);

if (text == NULL && sqlite3_errcode(db) == SQLITE_NOMEM) {
PyErr_NoMemory();
goto error;
}

Py_ssize_t size = sqlite3_value_bytes(cur_value);
cur_py_value = PyUnicode_FromStringAndSize(text, size);
break;
}
case SQLITE_BLOB:
buflen = sqlite3_value_bytes(cur_value);
cur_py_value = PyBytes_FromStringAndSize(
Expand All @@ -585,15 +587,18 @@ PyObject* _pysqlite_build_py_params(sqlite3_context *context, int argc, sqlite3_
}

if (!cur_py_value) {
Py_DECREF(args);
return NULL;
goto error;
}

PyTuple_SetItem(args, i, cur_py_value);

}

return args;

error:
Py_DECREF(args);
return NULL;
}

void _pysqlite_func_callback(sqlite3_context* context, int argc, sqlite3_value** argv)
Expand Down