(original) (raw)

changeset: 85052:5859a3ec5b7e branch: 3.3 parent: 85042:791034a0ae1e user: Christian Heimes christian@cheimes.de date: Tue Aug 06 15:59:16 2013 +0200 files: Misc/NEWS Parser/myreadline.c description: Issue #18368: PyOS_StdioReadline() no longer leaks memory when realloc() fails. diff -r 791034a0ae1e -r 5859a3ec5b7e Misc/NEWS --- a/Misc/NEWS Mon Aug 05 17:57:01 2013 +0100 +++ b/Misc/NEWS Tue Aug 06 15:59:16 2013 +0200 @@ -12,6 +12,9 @@ Core and Builtins ----------------- +- Issue #18368: PyOS_StdioReadline() no longer leaks memory when realloc() + fails. + - Issue #16741: Fix an error reporting in int(). - Issue #17899: Fix rare file descriptor leak in os.listdir(). diff -r 791034a0ae1e -r 5859a3ec5b7e Parser/myreadline.c --- a/Parser/myreadline.c Mon Aug 05 17:57:01 2013 +0100 +++ b/Parser/myreadline.c Tue Aug 06 15:59:16 2013 +0200 @@ -112,7 +112,7 @@ PyOS_StdioReadline(FILE *sys_stdin, FILE *sys_stdout, char *prompt) { size_t n; - char *p; + char *p, *pr; n = 100; if ((p = (char *)PyMem_MALLOC(n)) == NULL) return NULL; @@ -135,17 +135,29 @@ n = strlen(p); while (n > 0 && p[n-1] != '\n') { size_t incr = n+2; - p = (char *)PyMem_REALLOC(p, n + incr); - if (p == NULL) + if (incr > INT_MAX) { + PyMem_FREE(p); + PyErr_SetString(PyExc_OverflowError, "input line too long"); return NULL; - if (incr > INT_MAX) { - PyErr_SetString(PyExc_OverflowError, "input line too long"); } + pr = (char *)PyMem_REALLOC(p, n + incr); + if (pr == NULL) { + PyMem_FREE(p); + PyErr_NoMemory(); + return NULL; + } + p = pr; if (my_fgets(p+n, (int)incr, sys_stdin) != 0) break; n += strlen(p+n); } - return (char *)PyMem_REALLOC(p, n+1); + pr = (char *)PyMem_REALLOC(p, n+1); + if (pr == NULL) { + PyMem_FREE(p); + PyErr_NoMemory(); + return NULL; + } + return pr; } /christian@cheimes.de