mirror of https://github.com/python/cpython
Issue #18368: PyOS_StdioReadline() no longer leaks memory when realloc() fails.
This commit is contained in:
commit
73207e03ad
|
@ -10,6 +10,9 @@ Projected Release date: 2013-09-08
|
||||||
Core and Builtins
|
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
|
- Issue #17934: Add a clear() method to frame objects, to help clean up
|
||||||
expensive details (local variables) and break reference cycles.
|
expensive details (local variables) and break reference cycles.
|
||||||
|
|
||||||
|
|
|
@ -112,7 +112,7 @@ char *
|
||||||
PyOS_StdioReadline(FILE *sys_stdin, FILE *sys_stdout, char *prompt)
|
PyOS_StdioReadline(FILE *sys_stdin, FILE *sys_stdout, char *prompt)
|
||||||
{
|
{
|
||||||
size_t n;
|
size_t n;
|
||||||
char *p;
|
char *p, *pr;
|
||||||
n = 100;
|
n = 100;
|
||||||
if ((p = (char *)PyMem_MALLOC(n)) == NULL)
|
if ((p = (char *)PyMem_MALLOC(n)) == NULL)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
@ -135,17 +135,29 @@ PyOS_StdioReadline(FILE *sys_stdin, FILE *sys_stdout, char *prompt)
|
||||||
n = strlen(p);
|
n = strlen(p);
|
||||||
while (n > 0 && p[n-1] != '\n') {
|
while (n > 0 && p[n-1] != '\n') {
|
||||||
size_t incr = n+2;
|
size_t incr = n+2;
|
||||||
p = (char *)PyMem_REALLOC(p, n + incr);
|
|
||||||
if (p == NULL)
|
|
||||||
return NULL;
|
|
||||||
if (incr > INT_MAX) {
|
if (incr > INT_MAX) {
|
||||||
|
PyMem_FREE(p);
|
||||||
PyErr_SetString(PyExc_OverflowError, "input line too long");
|
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)
|
if (my_fgets(p+n, (int)incr, sys_stdin) != 0)
|
||||||
break;
|
break;
|
||||||
n += strlen(p+n);
|
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue