]> granicus.if.org Git - python/commitdiff
Issue #18368: PyOS_StdioReadline() no longer leaks memory when realloc() fails.
authorChristian Heimes <christian@cheimes.de>
Tue, 6 Aug 2013 13:59:16 +0000 (15:59 +0200)
committerChristian Heimes <christian@cheimes.de>
Tue, 6 Aug 2013 13:59:16 +0000 (15:59 +0200)
Misc/NEWS
Parser/myreadline.c

index 12c5020a63064c3298bc92c39bd618adf48332c5..35e1ae528953693b86aed42b992f750efc74054e 100644 (file)
--- 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().
index d864623f1a2a37081cde8bf1638e4317ad5d23a1..9f1fc1eb507b1513bbe13f6c7910b2b896bd3fa8 100644 (file)
@@ -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;
 }