Fix segfault initialising Column object manually

Close #1252
This commit is contained in:
Daniele Varrazzo 2021-05-20 13:01:00 +02:00
parent 56dab8398d
commit 24ef5c60dc
3 changed files with 34 additions and 4 deletions

3
NEWS
View File

@ -4,7 +4,8 @@ Current release
What's new in psycopg 2.8.7 What's new in psycopg 2.8.7
^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^
Accept empty params as `~psycopg2.connect()` (:ticket:`#1250). - Accept empty params as `~psycopg2.connect()` (:ticket:`#1250).
- Fix attributes refcount in `Column` initialisation (:ticket:`#1252`).
What's new in psycopg 2.8.6 What's new in psycopg 2.8.6

View File

@ -97,17 +97,36 @@ column_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
static int static int
column_init(columnObject *self, PyObject *args, PyObject *kwargs) column_init(columnObject *self, PyObject *args, PyObject *kwargs)
{ {
PyObject *name = NULL;
PyObject *type_code = NULL;
PyObject *display_size = NULL;
PyObject *internal_size = NULL;
PyObject *precision = NULL;
PyObject *scale = NULL;
PyObject *null_ok = NULL;
PyObject *table_oid = NULL;
PyObject *table_column = NULL;
static char *kwlist[] = { static char *kwlist[] = {
"name", "type_code", "display_size", "internal_size", "name", "type_code", "display_size", "internal_size",
"precision", "scale", "null_ok", "table_oid", "table_column", NULL}; "precision", "scale", "null_ok", "table_oid", "table_column", NULL};
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|OOOOOOOOO", kwlist, if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|OOOOOOOOO", kwlist,
&self->name, &self->type_code, &self->display_size, &name, &type_code, &display_size, &internal_size, &precision,
&self->internal_size, &self->precision, &self->scale, &scale, &null_ok, &table_oid, &table_column)) {
&self->null_ok, &self->table_oid, &self->table_column)) {
return -1; return -1;
} }
Py_XINCREF(name); self->name = name;
Py_XINCREF(type_code); self->type_code = type_code;
Py_XINCREF(display_size); self->display_size = display_size;
Py_XINCREF(internal_size); self->internal_size = internal_size;
Py_XINCREF(precision); self->precision = precision;
Py_XINCREF(scale); self->scale = scale;
Py_XINCREF(null_ok); self->null_ok = null_ok;
Py_XINCREF(table_oid); self->table_oid = table_oid;
Py_XINCREF(table_column); self->table_column = table_column;
return 0; return 0;
} }

View File

@ -250,6 +250,16 @@ class CursorTests(ConnectingTestCase):
self.assertEqual(description, unpickled) self.assertEqual(description, unpickled)
def test_column_refcount(self):
# Reproduce crash describe in ticket #1252
from psycopg2.extensions import Column
def do_stuff():
_ = Column(name='my_column')
for _ in range(1000):
do_stuff()
def test_bad_subclass(self): def test_bad_subclass(self):
# check that we get an error message instead of a segfault # check that we get an error message instead of a segfault
# for badly written subclasses. # for badly written subclasses.