Skip to content
Closed
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
11 changes: 11 additions & 0 deletions Lib/sqlite3/test/regression.py
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,17 @@ def callback(*args):
del ref
support.gc_collect()

def CheckBpo34695(self):
"""
The interpreter shouldn't crash in case Cache.__init__() is not
called.
"""
cache = sqlite.Cache.__new__(sqlite.Cache, lambda x: x)
self.assertRaisesRegex(sqlite.ProgrammingError, r'Cache\.__init__',
cache.get, 42)
self.assertRaisesRegex(sqlite.ProgrammingError, r'Cache\.__init__',
cache.display)


def suite():
regression_suite = unittest.makeSuite(RegressionTests, "Check")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix crash if :meth:`sqlite3.Cache.get` is called before ``__init__``.
18 changes: 18 additions & 0 deletions Modules/_sqlite/cache.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
*/

#include "cache.h"
#include "module.h"
#include <limits.h>

/* only used internally */
Expand Down Expand Up @@ -112,13 +113,26 @@ void pysqlite_cache_dealloc(pysqlite_Cache* self)
Py_TYPE(self)->tp_free((PyObject*)self);
}

static int pysqlite_check_cache(pysqlite_Cache *self) {
if (self->factory == NULL) {
PyErr_SetString(pysqlite_ProgrammingError,
"Cache.__init__ was not called or failed.");
return 0;
}
return 1;
}

PyObject* pysqlite_cache_get(pysqlite_Cache* self, PyObject* args)
{
PyObject* key = args;
pysqlite_Node* node;
pysqlite_Node* ptr;
PyObject* data;

if (!pysqlite_check_cache(self)) {
return NULL;
}

node = (pysqlite_Node*)PyDict_GetItem(self->mapping, key);
if (node) {
/* an entry for this key already exists in the cache */
Expand Down Expand Up @@ -218,6 +232,10 @@ PyObject* pysqlite_cache_display(pysqlite_Cache* self, PyObject* args)
PyObject* nextkey;
PyObject* display_str;

if (!pysqlite_check_cache(self)) {
return NULL;
}

ptr = self->first;

while (ptr) {
Expand Down