[Python-checkins] cpython (merge 3.3 -> default): Issue #18368: PyOS_StdioReadline() no longer leaks memory when realloc() fails.
christian.heimes
python-checkins at python.org
Tue Aug 6 16:03:48 CEST 2013
http://hg.python.org/cpython/rev/6dbc4d6ff31e
changeset: 85053:6dbc4d6ff31e
parent: 85050:80e9cb6163b4
parent: 85052:5859a3ec5b7e
user: Christian Heimes <christian at cheimes.de>
date: Tue Aug 06 16:03:33 2013 +0200
summary:
Issue #18368: PyOS_StdioReadline() no longer leaks memory when realloc() fails.
files:
Misc/NEWS | 3 +++
Parser/myreadline.c | 24 ++++++++++++++++++------
2 files changed, 21 insertions(+), 6 deletions(-)
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -10,6 +10,9 @@
Core and Builtins
-----------------
+- Issue #18368: PyOS_StdioReadline() no longer leaks memory when realloc()
+ fail
+
- Issue #17934: Add a clear() method to frame objects, to help clean up
expensive details (local variables) and break reference cycles.
diff --git a/Parser/myreadline.c b/Parser/myreadline.c
--- a/Parser/myreadline.c
+++ b/Parser/myreadline.c
@@ -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;
}
--
Repository URL: http://hg.python.org/cpython
More information about the Python-checkins
mailing list