gh-103295: fix stack overwrite on 32-bit in perf map test harness by carljm · Pull Request #104811 · python/cpython (original) (raw)

We can't use K format specifier in PyArg_ParseTuple to write to a void *; it always writes 64 bits, so writes too much on 32-bit systems. Use O instead and an explicit call to PyLong_AsVoidPtr to get the pointer value.

Also fix the error check so it detects either error code (-1 or -2) and raises a more informative error using errno. I verified this error handling by temporarily changing the perf map file path to a nonexistent directory, and the test raised FileNotFoundError as expected.

Also simplify the return value to just use PyLong_FromLong; we don't need to go through Py_BuildValue for such a simple case.