summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorChristian Heimes <christian@cheimes.de>2013-08-06 13:59:16 (GMT)
committerChristian Heimes <christian@cheimes.de>2013-08-06 13:59:16 (GMT)
commit9ae513caa74a05970458dee17fb995ea49965bb5 (patch)
tree518d27a821237b4ccefe52bfbf155263df40995a
parentfb29a164dfe81a6879536fddc3a9f1336a03670e (diff)
downloadcpython-9ae513caa74a05970458dee17fb995ea49965bb5.zip
cpython-9ae513caa74a05970458dee17fb995ea49965bb5.tar.gz
cpython-9ae513caa74a05970458dee17fb995ea49965bb5.tar.bz2
Issue #18368: PyOS_StdioReadline() no longer leaks memory when realloc() fails.
-rw-r--r--Misc/NEWS3
-rw-r--r--Parser/myreadline.c22
2 files changed, 20 insertions, 5 deletions
diff --git a/Misc/NEWS b/Misc/NEWS
index 12c5020..35e1ae5 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -12,6 +12,9 @@ What's New in Python 3.3.3 release candidate 1?
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 --git a/Parser/myreadline.c b/Parser/myreadline.c
index d864623..9f1fc1e 100644
--- a/Parser/myreadline.c
+++ b/Parser/myreadline.c
@@ -112,7 +112,7 @@ char *
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 @@ PyOS_StdioReadline(FILE *sys_stdin, FILE *sys_stdout, char *prompt)
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)
- return NULL;
if (incr > INT_MAX) {
+ PyMem_FREE(p);
PyErr_SetString(PyExc_OverflowError, "input line too long");
+ return NULL;
+ }
+ 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;
}