diff options
author | Christian Heimes <christian@cheimes.de> | 2013-08-06 14:03:33 (GMT) |
---|---|---|
committer | Christian Heimes <christian@cheimes.de> | 2013-08-06 14:03:33 (GMT) |
commit | 73207e03adba704d783997f6261b810ccc4231e4 (patch) | |
tree | be5a45d1491092c6f0599a585cc58fb659cbb016 /Parser/myreadline.c | |
parent | 9b855de8d2808f725566e5e74a4134fd366689ea (diff) | |
parent | 9ae513caa74a05970458dee17fb995ea49965bb5 (diff) | |
download | cpython-73207e03adba704d783997f6261b810ccc4231e4.zip cpython-73207e03adba704d783997f6261b810ccc4231e4.tar.gz cpython-73207e03adba704d783997f6261b810ccc4231e4.tar.bz2 |
Issue #18368: PyOS_StdioReadline() no longer leaks memory when realloc() fails.
Diffstat (limited to 'Parser/myreadline.c')
-rw-r--r-- | Parser/myreadline.c | 22 |
1 files changed, 17 insertions, 5 deletions
diff --git a/Parser/myreadline.c b/Parser/myreadline.c index 8b27045..3cf334d 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; } |