Skip to content

SearchPathW is incorrectly used in PC/launcher.c #149859

@dpc00

Description

@dpc00

Bug report

Bug description:

"assume there is a bug in python.exe search c++ source code and find it. " was my question to Codex.

"I found a concrete bug candidate in CPython’s Windows launcher source, not in this local finance repo. The local workspace and nearby projects don’t contain C++ source for this."

Bug: PC/launcher.c::find_on_path() treats any nonzero SearchPathW() return as success:

len = SearchPathW(NULL, name, NULL, MSGSIZE, path_command.value, NULL);
if (len) {
result = &path_command;
}

But on Windows, SearchPathW() returns the required buffer length if the buffer is too small. If the resolved python.exe path is >= MSGSIZE,
this code still reports success and later launches using a truncated/invalid path_command.value.

Reachability: this is used when a shebang like /usr/bin/env python triggers:

cmd = find_on_path(PYTHON_EXECUTABLE);

So the bug is in the python.exe PATH search path.

Source:
https://github.com/python/cpython/blob/main/PC/launcher.c

Likely fix shape:

if (len && len < MSGSIZE) {
result = &path_command;
}

or better, allocate dynamically when len >= MSGSIZE.
k here, if required



### CPython versions tested on:

3.14

### Operating systems tested on:

Windows

Metadata

Metadata

Assignees

No one assigned

    Labels

    OS-windowsinterpreter-core(Objects, Python, Grammar, and Parser dirs)type-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions