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

Skip to content

Commit d12bd01

Browse files
committed
Handle more memory allocation failures without crashing.
1 parent 33722ae commit d12bd01

5 files changed

Lines changed: 38 additions & 8 deletions

File tree

Python/ast.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -638,8 +638,10 @@ ast_for_arguments(struct compiling *c, const node *n)
638638
anything other than EQUAL or a comma? */
639639
/* XXX Should NCH(n) check be made a separate check? */
640640
if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) {
641-
asdl_seq_SET(defaults, j++,
642-
ast_for_expr(c, CHILD(n, i + 2)));
641+
expr_ty expression = ast_for_expr(c, CHILD(n, i + 2));
642+
if (!expression)
643+
goto error;
644+
asdl_seq_SET(defaults, j++, expression);
643645
i += 2;
644646
found_default = 1;
645647
}

Python/compile.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1105,8 +1105,17 @@ compiler_enter_scope(struct compiler *c, identifier name, void *key,
11051105
u->u_name = name;
11061106
u->u_varnames = list2dict(u->u_ste->ste_varnames);
11071107
u->u_cellvars = dictbytype(u->u_ste->ste_symbols, CELL, 0, 0);
1108+
if (!u->u_varnames || !u->u_cellvars) {
1109+
compiler_unit_free(u);
1110+
return 0;
1111+
}
1112+
11081113
u->u_freevars = dictbytype(u->u_ste->ste_symbols, FREE, DEF_FREE_CLASS,
11091114
PyDict_Size(u->u_cellvars));
1115+
if (!u->u_freevars) {
1116+
compiler_unit_free(u);
1117+
return 0;
1118+
}
11101119

11111120
u->u_blocks = NULL;
11121121
u->u_tmpname = 0;

Python/pythonrun.c

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1204,8 +1204,12 @@ PyRun_StringFlags(const char *str, int start, PyObject *globals,
12041204
{
12051205
PyObject *ret = NULL;
12061206
PyArena *arena = PyArena_New();
1207-
mod_ty mod = PyParser_ASTFromString(str, "<string>", start, flags,
1208-
arena);
1207+
mod_ty mod;
1208+
1209+
if (arena == NULL)
1210+
return NULL;
1211+
1212+
mod = PyParser_ASTFromString(str, "<string>", start, flags, arena);
12091213
if (mod != NULL)
12101214
ret = run_mod(mod, "<string>", globals, locals, flags, arena);
12111215
PyArena_Free(arena);
@@ -1218,8 +1222,13 @@ PyRun_FileExFlags(FILE *fp, const char *filename, int start, PyObject *globals,
12181222
{
12191223
PyObject *ret;
12201224
PyArena *arena = PyArena_New();
1221-
mod_ty mod = PyParser_ASTFromFile(fp, filename, start, 0, 0,
1222-
flags, NULL, arena);
1225+
mod_ty mod;
1226+
1227+
if (arena == NULL)
1228+
return NULL;
1229+
1230+
mod = PyParser_ASTFromFile(fp, filename, start, 0, 0,
1231+
flags, NULL, arena);
12231232
if (mod == NULL) {
12241233
PyArena_Free(arena);
12251234
return NULL;

Python/symtable.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -221,8 +221,12 @@ PySymtable_Build(mod_ty mod, const char *filename, PyFutureFeatures *future)
221221
return st;
222222
st->st_filename = filename;
223223
st->st_future = future;
224-
symtable_enter_block(st, GET_IDENTIFIER(top), ModuleBlock,
225-
(void *)mod, 0);
224+
if (!symtable_enter_block(st, GET_IDENTIFIER(top), ModuleBlock,
225+
(void *)mod, 0)) {
226+
PySymtable_Free(st);
227+
return NULL;
228+
}
229+
226230
st->st_top = st->st_cur;
227231
st->st_cur->ste_unoptimized = OPT_TOPLEVEL;
228232
/* Any other top-level initialization? */
@@ -728,6 +732,8 @@ symtable_exit_block(struct symtable *st, void *ast)
728732
if (end >= 0) {
729733
st->st_cur = (PySTEntryObject *)PyList_GET_ITEM(st->st_stack,
730734
end);
735+
if (st->st_cur == NULL)
736+
return 0;
731737
Py_INCREF(st->st_cur);
732738
if (PySequence_DelItem(st->st_stack, end) < 0)
733739
return 0;
@@ -749,6 +755,8 @@ symtable_enter_block(struct symtable *st, identifier name, _Py_block_ty block,
749755
Py_DECREF(st->st_cur);
750756
}
751757
st->st_cur = PySTEntry_New(st, name, block, ast, lineno);
758+
if (st->st_cur == NULL)
759+
return 0;
752760
if (name == GET_IDENTIFIER(top))
753761
st->st_global = st->st_cur->ste_symbols;
754762
if (prev) {

Python/thread.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,8 @@ find_key(int key, void *value)
267267
struct key *p;
268268
long id = PyThread_get_thread_ident();
269269

270+
if (!keymutex)
271+
return NULL;
270272
PyThread_acquire_lock(keymutex, 1);
271273
for (p = keyhead; p != NULL; p = p->next) {
272274
if (p->id == id && p->key == key)

0 commit comments

Comments
 (0)