[Python-checkins] cpython (merge 3.4 -> default): Issue #21975: Fixed crash when using uninitialized sqlite3.Row (in particular

serhiy.storchaka python-checkins at python.org
Wed Aug 6 16:55:51 CEST 2014


http://hg.python.org/cpython/rev/9244ed41057a
changeset: 92023:9244ed41057a
parent: 92020:bb02acd9e9e9
parent: 92022:c1ca1c4c131b
user: Serhiy Storchaka <storchaka at gmail.com>
date: Wed Aug 06 17:51:58 2014 +0300
summary:
 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.
files:
 Misc/NEWS | 4 ++++
 Modules/_sqlite/row.c | 28 +++++++++++++++++-----------
 2 files changed, 21 insertions(+), 11 deletions(-)
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -112,6 +112,10 @@
 Library
 -------
 
+- 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.
+
 - Issue #20170: Convert posixmodule to use Argument Clinic.
 
 - Issue #21539: Add a *exists_ok* argument to `Pathlib.mkdir()` to mimic
diff --git a/Modules/_sqlite/row.c b/Modules/_sqlite/row.c
--- a/Modules/_sqlite/row.c
+++ b/Modules/_sqlite/row.c
@@ -32,35 +32,41 @@
 Py_TYPE(self)->tp_free((PyObject*)self);
 }
 
-int pysqlite_row_init(pysqlite_Row* self, PyObject* args, PyObject* kwargs)
+static PyObject *
+pysqlite_row_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
 {
+ pysqlite_Row *self;
 PyObject* data;
 pysqlite_Cursor* cursor;
 
- self->data = 0;
- self->description = 0;
+ assert(type != NULL && type->tp_alloc != NULL);
 
- if (!PyArg_ParseTuple(args, "OO", &cursor, &data)) {
- return -1;
- }
+ if (!_PyArg_NoKeywords("Row()", kwargs))
+ return NULL;
+ if (!PyArg_ParseTuple(args, "OO", &cursor, &data))
+ return NULL;
 
 if (!PyObject_IsInstance((PyObject*)cursor, (PyObject*)&pysqlite_CursorType)) {
 PyErr_SetString(PyExc_TypeError, "instance of cursor required for first argument");
- return -1;
+ return NULL;
 }
 
 if (!PyTuple_Check(data)) {
 PyErr_SetString(PyExc_TypeError, "tuple required for second argument");
- return -1;
+ return NULL;
 }
 
+ self = (pysqlite_Row *) type->tp_alloc(type, 0);
+ if (self == NULL)
+ return NULL;
+
 Py_INCREF(data);
 self->data = data;
 
 Py_INCREF(cursor->description);
 self->description = cursor->description;
 
- return 0;
+ return (PyObject *) self;
 }
 
 PyObject* pysqlite_row_item(pysqlite_Row* self, Py_ssize_t idx)
@@ -260,7 +266,7 @@
 0, /* tp_descr_get */
 0, /* tp_descr_set */
 0, /* tp_dictoffset */
- (initproc)pysqlite_row_init, /* tp_init */
+ 0, /* tp_init */
 0, /* tp_alloc */
 0, /* tp_new */
 0 /* tp_free */
@@ -268,7 +274,7 @@
 
 extern int pysqlite_row_setup_types(void)
 {
- pysqlite_RowType.tp_new = PyType_GenericNew;
+ pysqlite_RowType.tp_new = pysqlite_row_new;
 pysqlite_RowType.tp_as_mapping = &pysqlite_row_as_mapping;
 pysqlite_RowType.tp_as_sequence = &pysqlite_row_as_sequence;
 return PyType_Ready(&pysqlite_RowType);
-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list

AltStyle によって変換されたページ (->オリジナル) /