Skip to content

Commit

Permalink
Don't leak when PyDict_SetItemString called
Browse files Browse the repository at this point in the history
  • Loading branch information
coretl committed Feb 26, 2021
1 parent b685765 commit 31fd611
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions softioc/extension.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,16 @@
#define PyInt_FromLong(value) PyLong_FromLong(value)
#endif

/* Reference stealing version of PyDict_SetItemString */
static void set_dict_item_steal(PyObject *dict, const char *name, PyObject *py_value)
{
PyDict_SetItemString(dict, name, py_value);
Py_DECREF(py_value);
}

/* Helper for function below. */
#define ADD_ENUM(dict, name) \
PyDict_SetItemString(dict, #name, PyInt_FromLong(name))
set_dict_item_steal(dict, #name, PyInt_FromLong(name))

/* Alas, EPICS has changed the numerical assignments of the DBF_ enums between
* versions, so to avoid unpleasant surprises, we compute thes values here in C
Expand Down Expand Up @@ -76,7 +83,7 @@ static PyObject *get_field_offsets(PyObject *self, PyObject *args)
dbentry.pflddes->offset,
dbentry.pflddes->size,
dbentry.pflddes->field_type);
PyDict_SetItemString(dict, field_name, ost);
set_dict_item_steal(dict, field_name, ost);
status = dbNextField(&dbentry, 0);
}

Expand Down

0 comments on commit 31fd611

Please sign in to comment.