Skip to content

Commit fe64f78

Browse files
committed
Fix refcount handling for None object
Py_None is a singleton, but its refcount has to be managed like a regular object, that, it needs to be Py_INCREF'd before returning it or assigning it to a structure and it needs to be Py_DECREF'd when the containing object goes away.
1 parent 683e443 commit fe64f78

File tree

3 files changed

+32
-9
lines changed

3 files changed

+32
-9
lines changed

context_util.c

+7-2
Original file line numberDiff line numberDiff line change
@@ -87,23 +87,28 @@ callback_shim(struct getdns_context *context,
8787
}
8888
if (type == GETDNS_CALLBACK_CANCEL) {
8989
py_result = Py_None;
90+
Py_INCREF(Py_None);
9091
py_tid = Py_None;
92+
Py_INCREF(Py_None);
9193
py_userarg = Py_None;
94+
Py_INCREF(Py_None);
9295
} else {
9396
py_result = result_create(response);
9497
#if PY_MAJOR_VERSION >= 3
9598
py_tid = PyLong_FromLong((long)tid);
9699
#else
97100
py_tid = PyInt_FromLong((long)tid);
98101
#endif
99-
if (u->userarg)
102+
if (u->userarg) {
100103
#if PY_MAJOR_VERSION >= 3
101104
py_userarg = PyUnicode_FromString(u->userarg);
102105
#else
103106
py_userarg = PyString_FromString(u->userarg);
104107
#endif
105-
else
108+
} else {
106109
py_userarg = Py_None;
110+
Py_INCREF(Py_None);
111+
}
107112
}
108113
PyObject_CallFunctionObjArgs(u->callback_func, py_callback_type, py_result, py_userarg, py_tid, NULL);
109114
}

getdns.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -370,8 +370,8 @@ get_errorstr_by_id(PyObject *self, PyObject *args, PyObject *keywds)
370370
PyErr_SetString(getdns_error, GETDNS_RETURN_INVALID_PARAMETER_TEXT);
371371
return NULL;
372372
}
373-
if ((errstr = (char *)getdns_get_errorstr_by_id((uint16_t)id)) == 0)
374-
return Py_None;
373+
if ((errstr = (char *)getdns_get_errorstr_by_id((uint16_t)id)) == 0)
374+
Py_RETURN_NONE;
375375
else
376376
#if PY_MAJOR_VERSION >= 3
377377
return PyUnicode_FromString(errstr);

result.c

+23-5
Original file line numberDiff line numberDiff line change
@@ -85,25 +85,34 @@ result_init(getdns_ResultObject *self, PyObject *args, PyObject *keywds)
8585
#else
8686
self->answer_type = PyInt_FromLong((long)answer_type);
8787
#endif
88-
if ((canonical_name = get_canonical_name(result_dict)) == 0)
88+
if ((canonical_name = get_canonical_name(result_dict)) == 0) {
8989
self->canonical_name = Py_None;
90-
else
90+
Py_INCREF(Py_None);
91+
} else {
9192
#if PY_MAJOR_VERSION >= 3
9293
self->canonical_name = PyUnicode_FromString(canonical_name);
9394
#else
9495
self->canonical_name = PyString_FromString(canonical_name);
9596
#endif
97+
}
9698
if ((self->just_address_answers = get_just_address_answers(result_dict)) == NULL) {
9799
self->just_address_answers = Py_None;
100+
Py_INCREF(Py_None);
98101
}
99-
if ((self->validation_chain = get_validation_chain(result_dict)) == NULL)
102+
if ((self->validation_chain = get_validation_chain(result_dict)) == NULL) {
100103
self->validation_chain = Py_None;
104+
Py_INCREF(Py_None);
105+
}
101106
#if GETDNS_NUMERIC_VERSION < 0x00090000
102-
if ((self->call_debugging = get_call_debugging(result_dict)) == NULL)
107+
if ((self->call_debugging = get_call_debugging(result_dict)) == NULL) {
103108
self->call_debugging = Py_None;
109+
Py_INCREF(Py_None);
110+
}
104111
#else
105-
if ((self->call_reporting = get_call_reporting(result_dict)) == NULL)
112+
if ((self->call_reporting = get_call_reporting(result_dict)) == NULL) {
106113
self->call_reporting = Py_None;
114+
Py_INCREF(Py_None);
115+
}
107116
#endif
108117
return 0;
109118
}
@@ -117,16 +126,25 @@ result_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
117126
self = (getdns_ResultObject *)type->tp_alloc(type, 0);
118127
if (self != NULL) {
119128
self->just_address_answers = Py_None;
129+
Py_INCREF(Py_None);
120130
self->answer_type = Py_None;
131+
Py_INCREF(Py_None);
121132
self->status = Py_None;
133+
Py_INCREF(Py_None);
122134
self->replies_tree = Py_None;
135+
Py_INCREF(Py_None);
123136
self->canonical_name = Py_None;
137+
Py_INCREF(Py_None);
124138
self->replies_full = Py_None;
139+
Py_INCREF(Py_None);
125140
self->validation_chain = Py_None;
141+
Py_INCREF(Py_None);
126142
#if GETDNS_NUMERIC_VERSION < 0x00090000
127143
self->call_debugging = Py_None;
144+
Py_INCREF(Py_None);
128145
#else
129146
self->call_reporting = Py_None;
147+
Py_INCREF(Py_None);
130148
#endif
131149
}
132150
return (PyObject *)self;

0 commit comments

Comments
 (0)