Skip to content

Commit c183444

Browse files
vstinnermatrixise
andauthored
bpo-36301: Fix Py_Main() memory leaks (GH-12420)
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>
1 parent a10d426 commit c183444

File tree

3 files changed

+12
-2
lines changed

3 files changed

+12
-2
lines changed

Modules/main.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -888,13 +888,13 @@ pymain_main(_PyArgv *args)
888888
PyInterpreterState *interp;
889889
err = pymain_init(args, &interp);
890890
if (_Py_INIT_FAILED(err)) {
891-
_Py_ExitInitError(err);
891+
goto exit_init_error;
892892
}
893893

894894
int exitcode = 0;
895895
err = pymain_run_python(interp, &exitcode);
896896
if (_Py_INIT_FAILED(err)) {
897-
_Py_ExitInitError(err);
897+
goto exit_init_error;
898898
}
899899

900900
if (Py_FinalizeEx() < 0) {
@@ -910,6 +910,10 @@ pymain_main(_PyArgv *args)
910910
}
911911

912912
return exitcode;
913+
914+
exit_init_error:
915+
pymain_free();
916+
_Py_ExitInitError(err);
913917
}
914918

915919

Python/pathconfig.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,12 @@ _PyPathConfig_SetGlobal(const _PyPathConfig *config)
149149
void
150150
_PyPathConfig_ClearGlobal(void)
151151
{
152+
PyMemAllocatorEx old_alloc;
153+
_PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
154+
152155
_PyPathConfig_Clear(&_Py_path_config);
156+
157+
PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
153158
}
154159

155160

Python/preconfig.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -758,6 +758,7 @@ _PyPreConfig_ReadFromArgv(_PyPreConfig *config, const _PyArgv *args)
758758
done:
759759
if (init_ctype_locale != NULL) {
760760
setlocale(LC_CTYPE, init_ctype_locale);
761+
PyMem_RawFree(init_ctype_locale);
761762
}
762763
_PyPreConfig_Clear(&save_config);
763764
Py_UTF8Mode = init_utf8_mode ;

0 commit comments

Comments
 (0)