Skip to content

[Windows] issue with GetVolumeInformation() #2697

@gvanem

Description

@gvanem

There's an issue in disk.c line 280:

        ret = GetVolumeInformation(
            (LPCTSTR)drive_letter,
            NULL,
            _ARRAYSIZE(drive_letter),
...

Using clang-cl, it warns:

psutil/arch/windows/disk.c(280,13): warning: 'sizeof (drive_letter)' will return the size of the pointer, not the array itself
      [-Wsizeof-pointer-div]
  280 |             _ARRAYSIZE(drive_letter),
      |             ^~~~~~~~~~~~~~~~~~~~~~~~

And Co-Pilot concurs:

Highlighted Snippet

ret = GetVolumeInformation(
    (LPCTSTR)drive_letter, [<- start of snippet]
    NULL,
    _ARRAYSIZE(drive_letter),
    NULL,
    &lpMaximumComponentLength,
    &pflags,
    (LPTSTR)fs_type,
    _ARRAYSIZE(fs_type)
);
[<- end of snippet]

1. Code Smells / Antipatterns

a. Potential Misuse of _ARRAYSIZE with Pointer

  • _ARRAYSIZE(drive_letter) is being used, but drive_letter is a pointer, not an array. This macro works as intended only for actual arrays, not pointers, leading to incorrect buffer sizes.

...

A simple fix would be:

--- a/psutil/arch/windows/disk.c 2025-12-17 11:32:53
+++ psutil/arch/windows/disk.c 2025-12-17 12:39:37
@@ -277,7 +277,7 @@
         ret = GetVolumeInformation(
             (LPCTSTR)drive_letter,
             NULL,
-            _ARRAYSIZE(drive_letter),
+            _ARRAYSIZE(drive_strings),
             NULL,
             &lpMaximumComponentLength,
             &pflags,

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions