bpo-36367: Free buffer if realloc fails in tokenize.c (GH-12442) (GH-… · python/cpython@65b9849 (original) (raw)

Original file line number Diff line number Diff line change
@@ -733,9 +733,14 @@ translate_newlines(const char *s, int exec_input, struct tok_state *tok) {
733 733 }
734 734 *current = '\0';
735 735 final_length = current - buf + 1;
736 -if (final_length < needed_length && final_length)
736 +if (final_length < needed_length && final_length) {
737 737 /* 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 + }
739 744 return buf;
740 745 }
741 746
@@ -1050,6 +1055,7 @@ tok_nextc(struct tok_state *tok)
1050 1055 newbuf = (char *)PyMem_REALLOC(newbuf,
1051 1056 newsize);
1052 1057 if (newbuf == NULL) {
1058 +PyMem_FREE(tok->buf);
1053 1059 tok->done = E_NOMEM;
1054 1060 tok->cur = tok->inp;
1055 1061 return EOF;