Skip to content

Commit 469b0a5

Browse files
authored
bpo-36367: Free buffer if realloc fails in tokenize.c (GH-12442) (GH-12470)
(cherry picked from commit cb90c89)
1 parent 07b8018 commit 469b0a5

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
@@ -656,9 +656,14 @@ translate_newlines(const char *s, int exec_input, struct tok_state *tok) {
656656
}
657657
*current = '\0';
658658
final_length = current - buf + 1;
659-
if (final_length < needed_length && final_length)
659+
if (final_length < needed_length && final_length) {
660660
/* should never fail */
661-
buf = PyMem_REALLOC(buf, final_length);
661+
char* result = PyMem_REALLOC(buf, final_length);
662+
if (result == NULL) {
663+
PyMem_FREE(buf);
664+
}
665+
buf = result;
666+
}
662667
return buf;
663668
}
664669

@@ -974,6 +979,7 @@ tok_nextc(register struct tok_state *tok)
974979
newbuf = (char *)PyMem_REALLOC(newbuf,
975980
newsize);
976981
if (newbuf == NULL) {
982+
PyMem_FREE(tok->buf);
977983
tok->done = E_NOMEM;
978984
tok->cur = tok->inp;
979985
return EOF;

0 commit comments

Comments
 (0)