Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit ce9ace1

Browse files
committedNov 2, 2022
pythongh-98978: Fix use-after-free in apps that call pathconfig global functions multiple times
1 parent c76db37 commit ce9ace1

File tree

2 files changed

+6
-9
lines changed

2 files changed

+6
-9
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fixed a potential use-after-free in applications that call configuration
2+
functions :c:func:`Py_SetPythonHome`, :c:func:`Py_SetProgramName` or
3+
(private) ``_Py_SetProgramFullPath`` multiple times.

‎Python/pathconfig.c

+3-9
Original file line numberDiff line numberDiff line change
@@ -261,9 +261,7 @@ Py_SetPythonHome(const wchar_t *home)
261261
_PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
262262

263263
PyMem_RawFree(_Py_path_config.home);
264-
if (has_value) {
265-
_Py_path_config.home = _PyMem_RawWcsdup(home);
266-
}
264+
_Py_path_config.home = has_value ? _PyMem_RawWcsdup(home) : NULL;
267265

268266
PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
269267

@@ -282,9 +280,7 @@ Py_SetProgramName(const wchar_t *program_name)
282280
_PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
283281

284282
PyMem_RawFree(_Py_path_config.program_name);
285-
if (has_value) {
286-
_Py_path_config.program_name = _PyMem_RawWcsdup(program_name);
287-
}
283+
_Py_path_config.program_name = has_value ? _PyMem_RawWcsdup(program_name) : NULL;
288284

289285
PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
290286

@@ -302,9 +298,7 @@ _Py_SetProgramFullPath(const wchar_t *program_full_path)
302298
_PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
303299

304300
PyMem_RawFree(_Py_path_config.program_full_path);
305-
if (has_value) {
306-
_Py_path_config.program_full_path = _PyMem_RawWcsdup(program_full_path);
307-
}
301+
_Py_path_config.program_full_path = has_value ? _PyMem_RawWcsdup(program_full_path) : NULL;
308302

309303
PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
310304

0 commit comments

Comments
 (0)
Please sign in to comment.