From 463b82a3efe8a6a9f3924a5b37482e961dffe3b8 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Wed, 1 May 2019 01:36:13 +0200 Subject: [PATCH] bpo-36763: Fix Py_SetStandardStreamEncoding() (GH-13028) Fix memory leak in Py_SetStandardStreamEncoding(): release memory if the function is called twice. --- .../next/C API/2019-05-01-00-42-08.bpo-36763.vghb86.rst | 2 ++ Python/coreconfig.c | 7 ++++--- 2 files changed, 6 insertions(+), 3 deletions(-) create mode 100644 Misc/NEWS.d/next/C API/2019-05-01-00-42-08.bpo-36763.vghb86.rst diff --git a/Misc/NEWS.d/next/C API/2019-05-01-00-42-08.bpo-36763.vghb86.rst b/Misc/NEWS.d/next/C API/2019-05-01-00-42-08.bpo-36763.vghb86.rst new file mode 100644 index 0000000000..1c34920827 --- /dev/null +++ b/Misc/NEWS.d/next/C API/2019-05-01-00-42-08.bpo-36763.vghb86.rst @@ -0,0 +1,2 @@ +Fix memory leak in :c:func:`Py_SetStandardStreamEncoding`: release memory if +the function is called twice. diff --git a/Python/coreconfig.c b/Python/coreconfig.c index d05beef2aa..471d5126f8 100644 --- a/Python/coreconfig.c +++ b/Python/coreconfig.c @@ -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; } -- 2.40.0