You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Browse filesBrowse the repository at this point in the historyBrowse files
authored
[Windows] disk_partitions: use wide-char APIs to fix UnicodeDecodeError (#1959) (#2975)
Fixes#1959.
On Windows, `psutil.disk_partitions()` crashed with `UnicodeDecodeError`
on systems whose ANSI code page is not UTF-8 (e.g. cp1251): the ANSI
variants of `GetLogicalDriveStrings`, `FindFirstVolumeMountPoint` and
`GetVolumeInformation` return bytes in the system ANSI code page, and
the `(ssss)` tuple format decodes them as strict UTF-8.
Following the maintainer's direction in the issue (use
`GetVolumeInformationW`), this switches the whole function family to
wide-char APIs: `GetLogicalDriveStringsW`, `GetDriveTypeW`,
`GetVolumeInformationW`,
`FindFirstVolumeMountPointW`/`FindNextVolumeMountPointW`, building
strings via `PyUnicode_FromWideChar`. Widening `GetVolumeInformation`
alone would not have been enough: the actual crash vector in the issue
(byte 0xc5 in position 4 of a mount-point path under cp1251) comes from
`FindFirstVolumeMountPoint`. `psutil_QueryDosDevice` is converted to
`QueryDosDeviceW` as well — it had the same class of mismatch in
reverse: the input path was UTF-8-decoded while `QueryDosDevice` returns
ANSI bytes, so device targets with non-ASCII characters could never
match.
Adds a regression test asserting the unicode contract of all four tuple
fields (the original crash requires a live non-UTF8 ANSI code page and
cannot be reproduced from Python on en-US CI).
---------
Signed-off-by: Alex Chen <l46983284@gmail.com>
Co-authored-by: Giampaolo Rodola <g.rodola@gmail.com>
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
0 commit comments