Skip to content

[3.7] bpo-36367: Free buffer if realloc fails in tokenize.c (GH-12442) #12471

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 20, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions Parser/tokenizer.c
Original file line number Diff line number Diff line change
Expand Up @@ -733,9 +733,14 @@ translate_newlines(const char *s, int exec_input, struct tok_state *tok) {
}
*current = '\0';
final_length = current - buf + 1;
if (final_length < needed_length && final_length)
if (final_length < needed_length && final_length) {
/* should never fail */
buf = PyMem_REALLOC(buf, final_length);
char* result = PyMem_REALLOC(buf, final_length);
if (result == NULL) {
PyMem_FREE(buf);
}
buf = result;
}
return buf;
}

Expand Down Expand Up @@ -1050,6 +1055,7 @@ tok_nextc(struct tok_state *tok)
newbuf = (char *)PyMem_REALLOC(newbuf,
newsize);
if (newbuf == NULL) {
PyMem_FREE(tok->buf);
tok->done = E_NOMEM;
tok->cur = tok->inp;
return EOF;
Expand Down