Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit df4572c

Browse files
committed
Issue #18408: parser module: fix error handling in node2tuple()
Handle PyLong_FromLong() and PyUnicode_FromString() failures
1 parent 3bd6abd commit df4572c

1 file changed

Lines changed: 52 additions & 28 deletions

File tree

Modules/parsermodule.c

Lines changed: 52 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -83,54 +83,78 @@ node2tuple(node *n, /* node to convert */
8383
int lineno, /* include line numbers? */
8484
int col_offset) /* include column offsets? */
8585
{
86+
PyObject *result = NULL, *w;
87+
8688
if (n == NULL) {
8789
Py_INCREF(Py_None);
88-
return (Py_None);
90+
return Py_None;
8991
}
92+
9093
if (ISNONTERMINAL(TYPE(n))) {
9194
int i;
92-
PyObject *v;
93-
PyObject *w;
9495

95-
v = mkseq(1 + NCH(n) + (TYPE(n) == encoding_decl));
96-
if (v == NULL)
97-
return (v);
96+
result = mkseq(1 + NCH(n) + (TYPE(n) == encoding_decl));
97+
if (result == NULL)
98+
goto error;
99+
98100
w = PyLong_FromLong(TYPE(n));
99-
if (w == NULL) {
100-
Py_DECREF(v);
101-
return ((PyObject*) NULL);
102-
}
103-
(void) addelem(v, 0, w);
101+
if (w == NULL)
102+
goto error;
103+
(void) addelem(result, 0, w);
104+
104105
for (i = 0; i < NCH(n); i++) {
105106
w = node2tuple(CHILD(n, i), mkseq, addelem, lineno, col_offset);
106-
if (w == NULL) {
107-
Py_DECREF(v);
108-
return ((PyObject*) NULL);
109-
}
110-
(void) addelem(v, i+1, w);
107+
if (w == NULL)
108+
goto error;
109+
(void) addelem(result, i+1, w);
111110
}
112111

113-
if (TYPE(n) == encoding_decl)
114-
(void) addelem(v, i+1, PyUnicode_FromString(STR(n)));
115-
return (v);
112+
if (TYPE(n) == encoding_decl) {
113+
w = PyUnicode_FromString(STR(n));
114+
if (w == NULL)
115+
goto error;
116+
(void) addelem(result, i+1, w);
117+
}
116118
}
117119
else if (ISTERMINAL(TYPE(n))) {
118-
PyObject *result = mkseq(2 + lineno + col_offset);
119-
if (result != NULL) {
120-
(void) addelem(result, 0, PyLong_FromLong(TYPE(n)));
121-
(void) addelem(result, 1, PyUnicode_FromString(STR(n)));
122-
if (lineno == 1)
123-
(void) addelem(result, 2, PyLong_FromLong(n->n_lineno));
124-
if (col_offset == 1)
125-
(void) addelem(result, 3, PyLong_FromLong(n->n_col_offset));
120+
result = mkseq(2 + lineno + col_offset);
121+
if (result == NULL)
122+
goto error;
123+
124+
w = PyLong_FromLong(TYPE(n));
125+
if (w == NULL)
126+
goto error;
127+
(void) addelem(result, 0, w);
128+
129+
w = PyUnicode_FromString(STR(n));
130+
if (w == NULL)
131+
goto error;
132+
(void) addelem(result, 1, w);
133+
134+
if (lineno == 1) {
135+
w = PyLong_FromLong(n->n_lineno);
136+
if (w == NULL)
137+
goto error;
138+
(void) addelem(result, 2, w);
139+
}
140+
141+
if (col_offset == 1) {
142+
w = PyLong_FromLong(n->n_col_offset);
143+
if (w == NULL)
144+
goto error;
145+
(void) addelem(result, 3, w);
126146
}
127-
return (result);
128147
}
129148
else {
130149
PyErr_SetString(PyExc_SystemError,
131150
"unrecognized parse tree node type");
132151
return ((PyObject*) NULL);
133152
}
153+
return result;
154+
155+
error:
156+
Py_XDECREF(result);
157+
return NULL;
134158
}
135159
/*
136160
* End of material copyrighted by Stichting Mathematisch Centrum.

0 commit comments

Comments
 (0)