bpo-34695: sqlite3: Fix crash if Cache.get() is called before __init__() by izbyshev · Pull Request #9331 · python/cpython
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
#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