mirror of
https://github.com/python/cpython.git
synced 2025-08-03 08:34:29 +00:00
Close #16742: Fix misuse of memory allocations in PyOS_Readline()
The GIL must be held to call PyMem_Malloc(), whereas PyOS_Readline() releases the GIL to read input. The result of the C callback PyOS_ReadlineFunctionPointer must now be a string allocated by PyMem_RawMalloc() or PyMem_RawRealloc() (or NULL if an error occurred), instead of a string allocated by PyMem_Malloc() or PyMem_Realloc(). Fixing this issue was required to setup a hook on PyMem_Malloc(), for example using the tracemalloc module. PyOS_Readline() copies the result of PyOS_ReadlineFunctionPointer() into a new buffer allocated by PyMem_Malloc(). So the public API of PyOS_Readline() does not change.
This commit is contained in:
parent
6cf185dc06
commit
2fe9bac4dc
5 changed files with 41 additions and 8 deletions
|
@ -1176,7 +1176,7 @@ call_readline(FILE *sys_stdin, FILE *sys_stdout, char *prompt)
|
|||
|
||||
/* We got an EOF, return a empty string. */
|
||||
if (p == NULL) {
|
||||
p = PyMem_Malloc(1);
|
||||
p = PyMem_RawMalloc(1);
|
||||
if (p != NULL)
|
||||
*p = '\0';
|
||||
RESTORE_LOCALE(saved_locale)
|
||||
|
@ -1204,7 +1204,7 @@ call_readline(FILE *sys_stdin, FILE *sys_stdout, char *prompt)
|
|||
/* Copy the malloc'ed buffer into a PyMem_Malloc'ed one and
|
||||
release the original. */
|
||||
q = p;
|
||||
p = PyMem_Malloc(n+2);
|
||||
p = PyMem_RawMalloc(n+2);
|
||||
if (p != NULL) {
|
||||
strncpy(p, q, n);
|
||||
p[n] = '\n';
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue