Skip to content

Commit 6c2761d

Browse files
committed
Fix some memory leaks
1 parent 72638a2 commit 6c2761d

3 files changed

Lines changed: 29 additions & 40 deletions

File tree

docs/source/conf.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
# https://www.sphinx-doc.org/en/master/usage/configuration.html
55
import sys
66
import pathlib
7-
import setuptools_scm
87

98
from caterpillar import options
109

@@ -14,7 +13,7 @@
1413
# This option will enable easy documentation of struct classes
1514
options.set_struct_flags(options.S_REPLACE_TYPES)
1615

17-
# Next, we can import out module and all rypes on the struct classes will be
16+
# Next, we can import out module and all types on the struct classes will be
1817
# replaced by their runtime types
1918
try:
2019
import icspacket

src/icspacket/include/skeletons/py_application.h

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -871,33 +871,21 @@ static PyObject* PyCompat_Encode(enum asn_transfer_syntax ats,
871871
return __VA_ARGS__; \
872872
}
873873

874-
#define PY_IMPL_SEQ_REF_ATTR_TOPY(typeName, attrName, targetTypeName) \
875-
static inline PyObject* PyAsn##typeName##__##attrName##_ToPython( \
876-
typeName##_t* src, PyObject* parent) { \
877-
void* target = (void*)&src->attrName; \
878-
PyAsn##targetTypeName##Object* targetObj = \
879-
PyCompatAsnType_New(targetTypeName); \
880-
if (!targetObj) { \
881-
return NULL; \
882-
} \
883-
targetObj->ob_value = (targetTypeName##_t*)target; \
884-
targetObj->ob_parent = Py_NewRef(parent); \
885-
targetObj->s_valid = 1; \
886-
return (PyObject*)targetObj; \
874+
#define PY_IMPL_SEQ_REF_ATTR_TOPY(typeName, attrName, targetTypeName) \
875+
static inline PyObject* PyAsn##typeName##__##attrName##_ToPython( \
876+
typeName##_t* src, PyObject* parent) { \
877+
void* target = (void*)&src->attrName; \
878+
return PyCompatAsnType_FromParent(&PyAsn##targetTypeName##_Type, \
879+
parent, (void*)src); \
887880
}
888881

889882
#define PY_IMPL_SEQ_REF_ATTR_INDIRECT_TOPY(typeName, attrName, targetTypeName) \
890883
static inline PyObject* PyAsn##typeName##__##attrName##_ToPython( \
891884
typeName##_t* src, PyObject* parent) { \
892885
void* target = (void*)src->attrName; \
893-
PyAsn##targetTypeName##Object* targetObj = NULL; \
894886
if (target == NULL) Py_RETURN_NONE; \
895-
targetObj = PyCompatAsnType_New(targetTypeName); \
896-
if (targetObj == NULL) return NULL; \
897-
targetObj->ob_value = (targetTypeName##_t*)target; \
898-
targetObj->ob_parent = Py_NewRef(parent); \
899-
targetObj->s_valid = 1; \
900-
return (PyObject*)targetObj; \
887+
return PyCompatAsnType_FromParent(&PyAsn##targetTypeName##_Type, \
888+
parent, (void*)src); \
901889
}
902890

903891
#define PY_IMPL_SEQ_OPT_GETATTR(typeName, attrName) \

src/icspacket/include/skeletons/py_convert.h

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
#include <Python.h>
1111
#include <py_application.h>
1212

13-
typedef long _senum_t;
14-
typedef unsigned long _uenum_t;
13+
typedef long _asn1c_senum_t;
14+
typedef unsigned long _asn1c_uenum_t;
1515

1616
#define PyCompat_ArgCheck(obj, ret) \
1717
if (!obj) { \
@@ -144,8 +144,8 @@ static inline int _PyCompatBytes_ToStringAndSize(PyObject *pObj, char **str,
144144

145145
if (PyObject_GetBuffer(pObj, &view, PyBUF_FULL_RO) < 0) return -1;
146146

147-
if (*str) {
148-
PyMem_Free(*str);
147+
if ((*str) != NULL) {
148+
PY_IMPL_FREE(*str);
149149
*str = NULL;
150150
}
151151
*size = view.len;
@@ -189,19 +189,16 @@ static inline int _PyCompatUnicode_AsUTF8(PyObject *pObj, char **str,
189189
Py_ssize_t *size) {
190190
PyCompatUnicode_Check(pObj, -1);
191191
if (*str) {
192-
PyMem_Free(*str);
192+
PY_IMPL_FREE(*str);
193193
*str = NULL;
194194
}
195195

196196
*str = (char *)_PyCompatUnicode_AsUTF8AndSize(pObj, size);
197-
if (!*str) {
198-
return -1;
199-
}
200197
return *str == NULL ? -1 : 0;
201198
}
202199

203200
static inline PyObject *PyCompatEnum_FromSsize_t(PyObject *pEnumType,
204-
_senum_t value) {
201+
_asn1c_senum_t value) {
205202
PyObject *nValue = NULL, *nResult = NULL;
206203
PyCompat_ArgCheck(pEnumType, NULL);
207204

@@ -215,7 +212,7 @@ static inline PyObject *PyCompatEnum_FromSsize_t(PyObject *pEnumType,
215212
}
216213

217214
static inline PyObject *PyCompatEnum_FromSize_t(PyObject *pEnumType,
218-
_uenum_t value) {
215+
_asn1c_uenum_t value) {
219216
PyObject *nValue = NULL, *nResult = NULL;
220217
if ((nValue = PyLong_FromSize_t(value)) == NULL) {
221218
goto end;
@@ -226,15 +223,15 @@ static inline PyObject *PyCompatEnum_FromSize_t(PyObject *pEnumType,
226223
return nResult;
227224
}
228225

229-
static inline _senum_t PyCompatEnum_AsSsize_t(PyObject *pObj) {
226+
static inline _asn1c_senum_t PyCompatEnum_AsSsize_t(PyObject *pObj) {
230227
PyObject *nValue = NULL;
231228
if (PyLong_Check(pObj)) {
232229
return PyLong_AsSsize_t(pObj);
233230
}
234231

235232
nValue = PyObject_GetAttrString(pObj, "value");
236233
if (nValue != NULL) {
237-
_senum_t result = PyLong_AsLong(nValue);
234+
_asn1c_senum_t result = PyLong_AsLong(nValue);
238235
Py_XDECREF(nValue);
239236
return result;
240237
}
@@ -245,15 +242,15 @@ static inline _senum_t PyCompatEnum_AsSsize_t(PyObject *pObj) {
245242
return -1;
246243
}
247244

248-
static inline _uenum_t PyCompatEnum_AsSize_t(PyObject *pObj) {
245+
static inline _asn1c_uenum_t PyCompatEnum_AsSize_t(PyObject *pObj) {
249246
PyObject *nValue = NULL;
250247
if (PyLong_Check(pObj)) {
251248
return PyLong_AsSize_t(pObj);
252249
}
253250

254251
nValue = PyObject_GetAttrString(pObj, "value");
255252
if (nValue != NULL) {
256-
_uenum_t result = PyLong_AsSize_t(nValue);
253+
_asn1c_uenum_t result = PyLong_AsSize_t(nValue);
257254
Py_XDECREF(nValue);
258255
return result;
259256
}
@@ -267,19 +264,19 @@ static inline _uenum_t PyCompatEnum_AsSize_t(PyObject *pObj) {
267264
static inline int PyCompatEnum_FromObject(PyObject *pObj, void *dst,
268265
int is_signed) {
269266
if (is_signed) {
270-
*(_senum_t *)dst = PyCompatEnum_AsSsize_t(pObj);
267+
*(_asn1c_senum_t *)dst = PyCompatEnum_AsSsize_t(pObj);
271268
} else {
272-
*(_uenum_t *)dst = PyCompatEnum_AsSize_t(pObj);
269+
*(_asn1c_uenum_t *)dst = PyCompatEnum_AsSize_t(pObj);
273270
}
274271
return PyErr_Occurred() != NULL ? -1 : 0;
275272
}
276273

277274
static inline PyObject *PyCompatEnum_AsObject(PyObject *pEnumType, void *src,
278275
int is_signed) {
279276
if (is_signed) {
280-
return PyCompatEnum_FromSsize_t(pEnumType, *(_senum_t *)src);
277+
return PyCompatEnum_FromSsize_t(pEnumType, *(_asn1c_senum_t *)src);
281278
} else {
282-
return PyCompatEnum_FromSize_t(pEnumType, *(_uenum_t *)src);
279+
return PyCompatEnum_FromSize_t(pEnumType, *(_asn1c_uenum_t *)src);
283280
}
284281
}
285282

@@ -311,6 +308,11 @@ static inline PyObject *PyCompatAsnType_FromParent(PyTypeObject *type,
311308
return NULL;
312309
}
313310

311+
if (obj->ob_value != NULL) {
312+
/* the value is uninitialized here, we can simply free it*/
313+
PY_IMPL_FREE(obj->ob_value);
314+
obj->ob_value = NULL;
315+
}
314316
obj->ob_value = value;
315317
obj->ob_parent = Py_NewRef(parent);
316318
obj->s_valid = 1;

0 commit comments

Comments
 (0)