diff --git a/Misc/NEWS.d/next/Core and Builtins/2018-07-11-11-20-12.bpo-34080.8t7PtO.rst b/Misc/NEWS.d/next/Core and Builtins/2018-07-11-11-20-12.bpo-34080.8t7PtO.rst new file mode 100644 index 00000000000000..cfc53cca487907 --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2018-07-11-11-20-12.bpo-34080.8t7PtO.rst @@ -0,0 +1,2 @@ +Fixed a memory leak in the compiler when it raised some uncommon errors +during tokenizing. diff --git a/Parser/parsetok.c b/Parser/parsetok.c index 1f467d63c41e24..33301c47c2f726 100644 --- a/Parser/parsetok.c +++ b/Parser/parsetok.c @@ -246,8 +246,6 @@ parsetok(struct tok_state *tok, grammar *g, int start, perrdetail *err_ret, else if ((ps->p_flags & CO_FUTURE_BARRY_AS_BDFL) && strcmp(str, "<>")) { PyObject_FREE(str); - err_ret->text = "with Barry as BDFL, use '<>' " - "instead of '!='"; err_ret->error = E_SYNTAX; break; } @@ -322,7 +320,10 @@ parsetok(struct tok_state *tok, grammar *g, int start, perrdetail *err_ret, assert(tok->cur - tok->buf < INT_MAX); err_ret->offset = (int)(tok->cur - tok->buf); len = tok->inp - tok->buf; - err_ret->text = (char *) PyObject_MALLOC(len + 1); + /* make sure that we don't leak memory if text has been + set previously */ + assert(err_ret->text == NULL); + err_ret->text = (char *) PyObject_Malloc(len + 1); if (err_ret->text != NULL) { if (len > 0) strncpy(err_ret->text, tok->buf, len); diff --git a/Python/pythonrun.c b/Python/pythonrun.c index bcd1ca931d03d3..73376ec763e376 100644 --- a/Python/pythonrun.c +++ b/Python/pythonrun.c @@ -63,7 +63,7 @@ static PyObject *run_mod(mod_ty, PyObject *, PyObject *, PyObject *, PyCompilerFlags *, PyArena *); static PyObject *run_pyc_file(FILE *, const char *, PyObject *, PyObject *, PyCompilerFlags *); -static void err_input(perrdetail *); +static void err_input(const perrdetail *); static void err_free(perrdetail *); static int PyRun_InteractiveOneObjectEx(FILE *, PyObject *, PyCompilerFlags *); @@ -1324,12 +1324,14 @@ static void err_free(perrdetail *err) { Py_CLEAR(err->filename); + PyObject_Free(err->text); + err->text = NULL; } /* Set the error appropriate to the given input error code (see errcode.h) */ static void -err_input(perrdetail *err) +err_input(const perrdetail *err) { PyObject *v, *w, *errtype, *errtext; PyObject *msg_obj = NULL; @@ -1445,10 +1447,6 @@ err_input(perrdetail *err) Py_XDECREF(w); cleanup: Py_XDECREF(msg_obj); - if (err->text != NULL) { - PyObject_FREE(err->text); - err->text = NULL; - } }