Skip to content

Commit ca678c3

Browse files
committed
Fix CHOICE init
1 parent 3cb08c4 commit ca678c3

3 files changed

Lines changed: 25 additions & 26 deletions

File tree

libasn1compiler/asn1c_Py.c

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,7 @@ int asn1c_lang_Py_type_CHOICE(arg_t *arg) {
371371
INDENT(+1);
372372
TQ_FOR (v, &(expr->members), next) {
373373
if (v->expr_type == A1TC_EXTENSIBLE) continue;
374-
OUT("PY_IMPL_SEQ_INIT_ATTR(%s, %s);\n", type_name, MKID_safe(v));
374+
OUT("PY_IMPL_CHOICE_INIT_ATTR(%s, %s, %s);\n", type_name, constr_struct_name, MKID_safe(v));
375375
}
376376
INDENT(-1);
377377
OUT(");\n");
@@ -1761,11 +1761,13 @@ int asn1c_lang_Py_stubs_SEQUENCE(arg_t *arg) {
17611761

17621762
int saved_target;
17631763
int saved_indent;
1764+
int is_set;
17641765

17651766
saved_target = arg->target->target;
17661767
cn = c_name(arg);
17671768
type_name = strdup(cn.as_member);
17681769
saved_indent = INDENT_LEVEL;
1770+
is_set = TYPE_IS_SET(arg->expr);
17691771

17701772
if (TYPE_IS_IMPORTED(arg, arg->expr)) {
17711773
PY_OUTER(OT_PY_STUBS_IMPORTS,
@@ -1777,9 +1779,10 @@ int asn1c_lang_Py_stubs_SEQUENCE(arg_t *arg) {
17771779
INDENT_LEVEL = arg->embed;
17781780
if (arg->embed) {
17791781
PY_GEN_LF;
1780-
OUT("class %s_TYPE(_Asn1Type):\n", type_name);
1782+
OUT("class %s_TYPE(_Asn1Type): # %s\n", type_name,
1783+
is_set ? "SET" : "SEQUENCE");
17811784
} else {
1782-
OUT("class %s(_Asn1Type):\n", type_name);
1785+
OUT("class %s(_Asn1Type): # %s\n", type_name, is_set ? "SET" : "SEQUENCE");
17831786
}
17841787
PY_GEN_STUBS_END;
17851788

@@ -1818,9 +1821,9 @@ int asn1c_lang_Py_stubs_CHOICE(arg_t *arg) {
18181821
INDENT_LEVEL = arg->embed;
18191822
if (arg->embed) {
18201823
PY_GEN_LF;
1821-
OUT("class %s_TYPE(_Asn1Type):\n", type_name);
1824+
OUT("class %s_TYPE(_Asn1Type): # CHOICE\n", type_name);
18221825
} else {
1823-
OUT("class %s(_Asn1Type):\n", type_name);
1826+
OUT("class %s(_Asn1Type): # CHOICE\n", type_name);
18241827
}
18251828
INDENT(+1);
18261829
OUT("class PRESENT(EXT_IntEnum):\n");

skeletons/py_application.h

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -231,9 +231,7 @@ static PyObject* PyCompat_Encode(enum asn_transfer_syntax ats,
231231

232232
#define PY_IMPL_DEALLOC(name, type_DEF) \
233233
static void PyAsn##name##__dealloc(PyAsn##name##Object* self) { \
234-
ASN_DEBUG("Freeing " #name " at %p (parent: %p)", self->ob_value, \
235-
self->ob_parent); \
236-
if (self->ob_parent != NULL) { \
234+
if (self->ob_parent != NULL) { \
237235
if (Py_REFCNT(self->ob_parent) < 1) { \
238236
PyErr_SetString(PyExc_MemoryError, \
239237
"UAF: parent object already deleted!"); \
@@ -672,16 +670,21 @@ static PyObject* PyCompat_Encode(enum asn_transfer_syntax ats,
672670
Py_XDECREF(tmp); \
673671
return result;
674672

675-
#define PY_IMPL_CHOICE_INIT_ATTR(typeName, attrName, srcObj, tmpValue) \
676-
PyCompat_GenericGetAttr((srcObj), attrName, (tmpValue)); \
677-
if ((tmpValue)) { \
678-
if (PyAsn##typeName##__##attrName##_FromPython((tmpValue), (dst)) < \
679-
0) { \
680-
Py_DECREF((tmpValue)); \
681-
return -1; \
682-
} \
683-
} else \
684-
PyErr_Clear();
673+
#define PY_IMPL_CHOICE_INIT_ATTR(typeName, enumTypeName, attrName) \
674+
if (result == 0) { \
675+
PyCompat_GenericGetAttr(pObj, attrName, tmp); \
676+
if (tmp != NULL) { \
677+
if (PyAsn##typeName##__##attrName##_FromPython(tmp, pDst) < 0) { \
678+
result = -1; \
679+
} else { \
680+
pDst->present = enumTypeName##_PR_##attrName; \
681+
Py_CLEAR(tmp); \
682+
return 0; \
683+
} \
684+
} else { \
685+
PyErr_Clear(); \
686+
} \
687+
}
685688

686689
#define PY_IMPL_CHOICE_INIT(typeName) \
687690
PY_IMPL_CHOICE_INIT_GENERIC(typeName, typeName)
@@ -1125,8 +1128,6 @@ static PyObject* PyCompat_Encode(enum asn_transfer_syntax ats,
11251128

11261129
#define PY_IMPL_SEQ_OF_DEALLOC(typeName, EMPTY_FUNC) \
11271130
static void PyAsn##typeName##__dealloc(PyAsn##typeName##Object* self) { \
1128-
ASN_DEBUG("Freeing " #typeName " object (value=%p, parent=%p)", \
1129-
self->ob_value, self->ob_parent); \
11301131
if (self->ob_parent) { \
11311132
if (Py_REFCNT(self->ob_parent) < 1) { \
11321133
PyErr_SetString(PyExc_MemoryError, \
@@ -1138,10 +1139,6 @@ static PyObject* PyCompat_Encode(enum asn_transfer_syntax ats,
11381139
} else { \
11391140
if (self->ob_value != NULL) { \
11401141
if (self->ob_value->list.count > 0) { \
1141-
ASN_DEBUG("Freeing elements of " #typeName \
1142-
" (count=%d, list=%p)", \
1143-
self->ob_value->list.count, \
1144-
self->ob_value->list.array); \
11451142
EMPTY_FUNC((void*)&self->ob_value->list); \
11461143
} \
11471144
PyMem_RawFree(self->ob_value); \
@@ -1171,6 +1168,7 @@ static PyObject* PyCompat_Encode(enum asn_transfer_syntax ats,
11711168
if (item_value == NULL) { \
11721169
goto end; \
11731170
} \
1171+
memset(item_value, 0, sizeof(memberTypeName)); \
11741172
if (PyAsn##typeName##__component_FromPython(item, item_value) < \
11751173
0) { \
11761174
goto end; \

skeletons/py_convert_OBJECT_IDENTIFIER.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,6 @@ static int PyCompatOID_FromUnicode(PyObject *unicode,
107107
PyCompatUnicode_Check(unicode, -1);
108108
oid_text = PyUnicode_AsUTF8AndSize(unicode, &oid_text_len);
109109
if (oid_text_len < 0) goto end;
110-
ASN_DEBUG("PyCompatOID_FromUnicode: Converting %s to an OID", oid_text);
111110

112111
arc_count = OBJECT_IDENTIFIER_parse_arcs(oid_text, oid_text_len, arcs,
113112
arc_slots, NULL);
@@ -130,7 +129,6 @@ static int PyCompatOID_FromUnicode(PyObject *unicode,
130129
if (result < 0) goto error;
131130
end:
132131
if (arcs != fixed_arcs && arcs) PyMem_RawFree(arcs);
133-
ASN_DEBUG("PyCompatOID_FromUnicode: Result: %d", result);
134132
return result;
135133

136134
error:

0 commit comments

Comments
 (0)