Skip to content

Commit 65b9849

Browse files
authored
bpo-36367: Free buffer if realloc fails in tokenize.c (GH-12442) (GH-12471)
1 parent 756cfd8 commit 65b9849

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
@@ -733,9 +733,14 @@ translate_newlines(const char *s, int exec_input, struct tok_state *tok) {
733733
}
734734
*current = '\0';
735735
final_length = current - buf + 1;
736-
if (final_length < needed_length && final_length)
736+
if (final_length < needed_length && final_length) {
737737
/* should never fail */
738-
buf = PyMem_REALLOC(buf, final_length);
738+
char* result = PyMem_REALLOC(buf, final_length);
739+
if (result == NULL) {
740+
PyMem_FREE(buf);
741+
}
742+
buf = result;
743+
}
739744
return buf;
740745
}
741746

@@ -1050,6 +1055,7 @@ tok_nextc(struct tok_state *tok)
10501055
newbuf = (char *)PyMem_REALLOC(newbuf,
10511056
newsize);
10521057
if (newbuf == NULL) {
1058+
PyMem_FREE(tok->buf);
10531059
tok->done = E_NOMEM;
10541060
tok->cur = tok->inp;
10551061
return EOF;

0 commit comments

Comments
 (0)