]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-36301: Fix Py_Main() memory leaks (GH-12420)
authorVictor Stinner <vstinner@redhat.com>
Mon, 18 Mar 2019 21:24:28 +0000 (22:24 +0100)
committerGitHub <noreply@github.com>
Mon, 18 Mar 2019 21:24:28 +0000 (22:24 +0100)
bpo-36301, bpo-36333:

* Fix memory allocator used by _PyPathConfig_ClearGlobal():
  force the default allocator.
* _PyPreConfig_ReadFromArgv(): free init_ctype_locale memory.
* pymain_main(): call pymain_free() on init error

Co-Authored-By: Stéphane Wirtel <stephane@wirtel.be>
Modules/main.c
Python/pathconfig.c
Python/preconfig.c

index 5c7f7e45673a122c9a7237ad2cdbc4a4a9bf2fa0..50fecc9103d926ec3a727c0736585db5f0e617b4 100644 (file)
@@ -888,13 +888,13 @@ pymain_main(_PyArgv *args)
     PyInterpreterState *interp;
     err = pymain_init(args, &interp);
     if (_Py_INIT_FAILED(err)) {
-        _Py_ExitInitError(err);
+        goto exit_init_error;
     }
 
     int exitcode = 0;
     err = pymain_run_python(interp, &exitcode);
     if (_Py_INIT_FAILED(err)) {
-        _Py_ExitInitError(err);
+        goto exit_init_error;
     }
 
     if (Py_FinalizeEx() < 0) {
@@ -910,6 +910,10 @@ pymain_main(_PyArgv *args)
     }
 
     return exitcode;
+
+exit_init_error:
+    pymain_free();
+    _Py_ExitInitError(err);
 }
 
 
index fb2d19e2797a7d2ba1b5ed97fbd6ffa92c3d74a3..0ee87c42525f49bc5ecd6934b4f44902e9e112c8 100644 (file)
@@ -149,7 +149,12 @@ done:
 void
 _PyPathConfig_ClearGlobal(void)
 {
+    PyMemAllocatorEx old_alloc;
+    _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
+
     _PyPathConfig_Clear(&_Py_path_config);
+
+    PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
 }
 
 
index a86ece57cfced938d9a7d7bc1a64cdc5fa1e52cb..1efc7ee5c56ed1ad7f0b8d78e27531d8c761cd1d 100644 (file)
@@ -758,6 +758,7 @@ _PyPreConfig_ReadFromArgv(_PyPreConfig *config, const _PyArgv *args)
 done:
     if (init_ctype_locale != NULL) {
         setlocale(LC_CTYPE, init_ctype_locale);
+        PyMem_RawFree(init_ctype_locale);
     }
     _PyPreConfig_Clear(&save_config);
     Py_UTF8Mode = init_utf8_mode ;