Skip to content

Commit 8ea5d0c

Browse files
committed
Fix segfault initialising Column object manually
Close psycopg#1252
1 parent 391386c commit 8ea5d0c

File tree

3 files changed

+34
-4
lines changed

3 files changed

+34
-4
lines changed

NEWS

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ What's new in psycopg 2.9
1313
What's new in psycopg 2.8.7
1414
^^^^^^^^^^^^^^^^^^^^^^^^^^^
1515

16-
Accept empty params as `~psycopg2.connect()` (:ticket:`#1250).
16+
- Accept empty params as `~psycopg2.connect()` (:ticket:`#1250).
17+
- Fix attributes refcount in `Column` initialisation (:ticket:`#1252`).
1718

1819

1920
What's new in psycopg 2.8.6

psycopg/column_type.c

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,17 +97,36 @@ column_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
9797
static int
9898
column_init(columnObject *self, PyObject *args, PyObject *kwargs)
9999
{
100+
PyObject *name = NULL;
101+
PyObject *type_code = NULL;
102+
PyObject *display_size = NULL;
103+
PyObject *internal_size = NULL;
104+
PyObject *precision = NULL;
105+
PyObject *scale = NULL;
106+
PyObject *null_ok = NULL;
107+
PyObject *table_oid = NULL;
108+
PyObject *table_column = NULL;
109+
100110
static char *kwlist[] = {
101111
"name", "type_code", "display_size", "internal_size",
102112
"precision", "scale", "null_ok", "table_oid", "table_column", NULL};
103113

104114
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|OOOOOOOOO", kwlist,
105-
&self->name, &self->type_code, &self->display_size,
106-
&self->internal_size, &self->precision, &self->scale,
107-
&self->null_ok, &self->table_oid, &self->table_column)) {
115+
&name, &type_code, &display_size, &internal_size, &precision,
116+
&scale, &null_ok, &table_oid, &table_column)) {
108117
return -1;
109118
}
110119

120+
Py_XINCREF(name); self->name = name;
121+
Py_XINCREF(type_code); self->type_code = type_code;
122+
Py_XINCREF(display_size); self->display_size = display_size;
123+
Py_XINCREF(internal_size); self->internal_size = internal_size;
124+
Py_XINCREF(precision); self->precision = precision;
125+
Py_XINCREF(scale); self->scale = scale;
126+
Py_XINCREF(null_ok); self->null_ok = null_ok;
127+
Py_XINCREF(table_oid); self->table_oid = table_oid;
128+
Py_XINCREF(table_column); self->table_column = table_column;
129+
111130
return 0;
112131
}
113132

tests/test_cursor.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,16 @@ def test_pickle_description(self):
250250

251251
self.assertEqual(description, unpickled)
252252

253+
def test_column_refcount(self):
254+
# Reproduce crash describe in ticket #1252
255+
from psycopg2.extensions import Column
256+
257+
def do_stuff():
258+
_ = Column(name='my_column')
259+
260+
for _ in range(1000):
261+
do_stuff()
262+
253263
def test_bad_subclass(self):
254264
# check that we get an error message instead of a segfault
255265
# for badly written subclasses.

0 commit comments

Comments
 (0)