bpo-36763: Fix Py_SetStandardStreamEncoding() (GH-13028)

Fix memory leak in Py_SetStandardStreamEncoding(): release memory
if the function is called twice.
This commit is contained in:
Victor Stinner 2019-05-01 01:36:13 +02:00 committed by GitHub
parent 0df635c7f8
commit 463b82a3ef
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 6 additions and 3 deletions

View File

@ -0,0 +1,2 @@
Fix memory leak in :c:func:`Py_SetStandardStreamEncoding`: release memory if
the function is called twice.

View File

@ -375,6 +375,7 @@ Py_SetStandardStreamEncoding(const char *encoding, const char *errors)
* Py_Initialize hasn't been called yet.
*/
if (encoding) {
PyMem_RawFree(_Py_StandardStreamEncoding);
_Py_StandardStreamEncoding = _PyMem_RawStrdup(encoding);
if (!_Py_StandardStreamEncoding) {
res = -2;
@ -382,11 +383,11 @@ Py_SetStandardStreamEncoding(const char *encoding, const char *errors)
}
}
if (errors) {
PyMem_RawFree(_Py_StandardStreamErrors);
_Py_StandardStreamErrors = _PyMem_RawStrdup(errors);
if (!_Py_StandardStreamErrors) {
if (_Py_StandardStreamEncoding) {
PyMem_RawFree(_Py_StandardStreamEncoding);
}
PyMem_RawFree(_Py_StandardStreamEncoding);
_Py_StandardStreamEncoding = NULL;
res = -3;
goto done;
}