Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3d4b2d4

Browse files
Issue #21975: Fixed crash when using uninitialized sqlite3.Row (in particular
when unpickling pickled sqlite3.Row). sqlite3.Row is now initialized in the __new__() method.
1 parent 9b33872 commit 3d4b2d4

2 files changed

Lines changed: 21 additions & 11 deletions

File tree

Misc/NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ Core and Builtins
2727
Library
2828
-------
2929

30+
- Issue #21975: Fixed crash when using uninitialized sqlite3.Row (in particular
31+
when unpickling pickled sqlite3.Row). sqlite3.Row is now initialized in the
32+
__new__() method.
33+
3034
- Issue #21580: Now Tkinter correctly handles bytes arguments passed to Tk.
3135
In particular this allows to initialize images from binary data.
3236

Modules/_sqlite/row.c

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,35 +32,41 @@ void pysqlite_row_dealloc(pysqlite_Row* self)
3232
Py_TYPE(self)->tp_free((PyObject*)self);
3333
}
3434

35-
int pysqlite_row_init(pysqlite_Row* self, PyObject* args, PyObject* kwargs)
35+
static PyObject *
36+
pysqlite_row_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
3637
{
38+
pysqlite_Row *self;
3739
PyObject* data;
3840
pysqlite_Cursor* cursor;
3941

40-
self->data = 0;
41-
self->description = 0;
42+
assert(type != NULL && type->tp_alloc != NULL);
4243

43-
if (!PyArg_ParseTuple(args, "OO", &cursor, &data)) {
44-
return -1;
45-
}
44+
if (!_PyArg_NoKeywords("Row()", kwargs))
45+
return NULL;
46+
if (!PyArg_ParseTuple(args, "OO", &cursor, &data))
47+
return NULL;
4648

4749
if (!PyObject_IsInstance((PyObject*)cursor, (PyObject*)&pysqlite_CursorType)) {
4850
PyErr_SetString(PyExc_TypeError, "instance of cursor required for first argument");
49-
return -1;
51+
return NULL;
5052
}
5153

5254
if (!PyTuple_Check(data)) {
5355
PyErr_SetString(PyExc_TypeError, "tuple required for second argument");
54-
return -1;
56+
return NULL;
5557
}
5658

59+
self = (pysqlite_Row *) type->tp_alloc(type, 0);
60+
if (self == NULL)
61+
return NULL;
62+
5763
Py_INCREF(data);
5864
self->data = data;
5965

6066
Py_INCREF(cursor->description);
6167
self->description = cursor->description;
6268

63-
return 0;
69+
return (PyObject *) self;
6470
}
6571

6672
PyObject* pysqlite_row_item(pysqlite_Row* self, Py_ssize_t idx)
@@ -260,15 +266,15 @@ PyTypeObject pysqlite_RowType = {
260266
0, /* tp_descr_get */
261267
0, /* tp_descr_set */
262268
0, /* tp_dictoffset */
263-
(initproc)pysqlite_row_init, /* tp_init */
269+
0, /* tp_init */
264270
0, /* tp_alloc */
265271
0, /* tp_new */
266272
0 /* tp_free */
267273
};
268274

269275
extern int pysqlite_row_setup_types(void)
270276
{
271-
pysqlite_RowType.tp_new = PyType_GenericNew;
277+
pysqlite_RowType.tp_new = pysqlite_row_new;
272278
pysqlite_RowType.tp_as_mapping = &pysqlite_row_as_mapping;
273279
pysqlite_RowType.tp_as_sequence = &pysqlite_row_as_sequence;
274280
return PyType_Ready(&pysqlite_RowType);

0 commit comments

Comments
 (0)