bpo-36367: Free buffer if realloc fails in tokenize.c (GH-12442) (GH-… · python/cpython@469b0a5

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;