Skip to content

sys_getwindowsversion_impl might potentially swallow errors in sysmodule.c #116326

Closed
@sobolevn

Description

@sobolevn

Bug report

While merging #115321 I've noticed a similar issue to #115320:

cpython/Python/sysmodule.c

Lines 1661 to 1699 in 207030f

PyStructSequence_SET_ITEM(version, pos++, PyLong_FromLong(ver.dwMajorVersion));
PyStructSequence_SET_ITEM(version, pos++, PyLong_FromLong(ver.dwMinorVersion));
PyStructSequence_SET_ITEM(version, pos++, PyLong_FromLong(ver.dwBuildNumber));
PyStructSequence_SET_ITEM(version, pos++, PyLong_FromLong(ver.dwPlatformId));
PyStructSequence_SET_ITEM(version, pos++, PyUnicode_FromWideChar(ver.szCSDVersion, -1));
PyStructSequence_SET_ITEM(version, pos++, PyLong_FromLong(ver.wServicePackMajor));
PyStructSequence_SET_ITEM(version, pos++, PyLong_FromLong(ver.wServicePackMinor));
PyStructSequence_SET_ITEM(version, pos++, PyLong_FromLong(ver.wSuiteMask));
PyStructSequence_SET_ITEM(version, pos++, PyLong_FromLong(ver.wProductType));
// GetVersion will lie if we are running in a compatibility mode.
// We need to read the version info from a system file resource
// to accurately identify the OS version. If we fail for any reason,
// just return whatever GetVersion said.
PyObject *realVersion = _sys_getwindowsversion_from_kernel32();
if (!realVersion) {
PyErr_Clear();
realVersion = Py_BuildValue("(kkk)",
ver.dwMajorVersion,
ver.dwMinorVersion,
ver.dwBuildNumber
);
}
if (realVersion) {
PyStructSequence_SET_ITEM(version, pos++, realVersion);
}
if (PyErr_Occurred()) {
Py_DECREF(version);
return NULL;
}
if (PyObject_SetAttrString(module, "_cached_windows_version", version) < 0) {
Py_DECREF(version);
return NULL;
}
return version;

It can also overwrite errors, I will send a PR similar to #115321 to fix this as well.

Linked PRs

Metadata

Metadata

Assignees

Labels

interpreter-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