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

Skip to content

Commit 3bd6abd

Browse files
committed
Issue #18408: Fix parser.sequence2st() and parser.tuple2st(): raise MemoryError
on memory allocation failure Instead of ignoring the memory allocation failure and create invalid objects.
1 parent 4202456 commit 3bd6abd

1 file changed

Lines changed: 15 additions & 4 deletions

File tree

Modules/parsermodule.c

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -809,8 +809,13 @@ build_node_children(PyObject *tuple, node *root, int *line_num)
809809
return 0;
810810
}
811811
strn = (char *)PyObject_MALLOC(len + 1);
812-
if (strn != NULL)
813-
(void) memcpy(strn, temp_str, len + 1);
812+
if (strn == NULL) {
813+
Py_DECREF(temp);
814+
Py_XDECREF(elem);
815+
PyErr_NoMemory();
816+
return 0;
817+
}
818+
(void) memcpy(strn, temp_str, len + 1);
814819
Py_DECREF(temp);
815820
}
816821
else if (!ISNONTERMINAL(type)) {
@@ -906,8 +911,14 @@ build_node_tree(PyObject *tuple)
906911
return NULL;
907912
}
908913
res->n_str = (char *)PyObject_MALLOC(len + 1);
909-
if (res->n_str != NULL && temp != NULL)
910-
(void) memcpy(res->n_str, temp, len + 1);
914+
if (res->n_str == NULL) {
915+
Py_DECREF(res);
916+
Py_DECREF(encoding);
917+
Py_DECREF(tuple);
918+
PyErr_NoMemory();
919+
return NULL;
920+
}
921+
(void) memcpy(res->n_str, temp, len + 1);
911922
Py_DECREF(encoding);
912923
Py_DECREF(tuple);
913924
}

0 commit comments

Comments
 (0)