Skip to content

Commit cb90c89

Browse files
authored
bpo-36367: Free buffer if realloc fails in tokenize.c (GH-12442)
1 parent dcf6171 commit cb90c89

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

Parser/tokenizer.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -649,9 +649,14 @@ translate_newlines(const char *s, int exec_input, struct tok_state *tok) {
649649
}
650650
*current = '\0';
651651
final_length = current - buf + 1;
652-
if (final_length < needed_length && final_length)
652+
if (final_length < needed_length && final_length) {
653653
/* should never fail */
654-
buf = PyMem_REALLOC(buf, final_length);
654+
char* result = PyMem_REALLOC(buf, final_length);
655+
if (result == NULL) {
656+
PyMem_FREE(buf);
657+
}
658+
buf = result;
659+
}
655660
return buf;
656661
}
657662

@@ -958,6 +963,7 @@ tok_nextc(struct tok_state *tok)
958963
newbuf = (char *)PyMem_REALLOC(newbuf,
959964
newsize);
960965
if (newbuf == NULL) {
966+
PyMem_FREE(tok->buf);
961967
tok->done = E_NOMEM;
962968
tok->cur = tok->inp;
963969
return EOF;

0 commit comments

Comments
 (0)