Skip to content

Commit 3d60322

Browse files
authored
Address NULL access (windows.plugin) (#21112)
address_null: Change NULL to valid address trying to avoid reported issue
1 parent b2bfb77 commit 3d60322

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

src/collectors/windows.plugin/perflib-storage.c

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -175,26 +175,27 @@ static STRING *getFileSystemType(struct logical_disk *d, const char *diskName)
175175
return NULL;
176176

177177
char fileSystemNameBuffer[128] = {0}; // Buffer for file system name
178-
char pathBuffer[256] = {0}; // Path buffer to accommodate different formats
178+
char pathBuffer[260] = {0}; // Path buffer to accommodate different formats
179+
char volumeName[260] = {0};
179180
DWORD serialNumber = 0;
180181
DWORD maxComponentLength = 0;
181182
DWORD fileSystemFlags = 0;
182183
BOOL success;
183184

184185
// Check if the input is likely a drive letter (e.g., "C:")
185186
if (isalpha((uint8_t)diskName[0]) && diskName[1] == ':' && diskName[2] == '\0')
186-
snprintf(pathBuffer, sizeof(pathBuffer), "%s\\", diskName); // Format as "C:\"
187+
snprintfz(pathBuffer, sizeof(pathBuffer) - 1, "%s\\", diskName); // Format as "C:\"
187188
else
188189
// Assume it's a Volume GUID path or a device path
189-
snprintf(pathBuffer, sizeof(pathBuffer), "\\\\.\\%s\\", diskName); // Format as "\\.\HarddiskVolume1\"
190+
snprintfz(pathBuffer, sizeof(pathBuffer) - 1, "\\\\.\\%s\\", diskName); // Format as "\\.\HarddiskVolume1\"
190191

191192
d->DriveType = GetDriveTypeA(pathBuffer);
192193

193194
// Attempt to get the volume information
194195
success = GetVolumeInformationA(
195196
pathBuffer, // Path to the disk
196-
NULL, // We don't need the volume name
197-
0, // Size of volume name buffer is 0
197+
volumeName, // Volume name buffer
198+
259, // Size of volume name bufferr
198199
&serialNumber, // Volume serial number
199200
&maxComponentLength, // Maximum component length
200201
&fileSystemFlags, // File system flags
@@ -246,6 +247,7 @@ static inline void netdata_set_hd_usage(PERF_DATA_BLOCK *pDataBlock,
246247
{
247248
ULARGE_INTEGER totalNumberOfBytes;
248249
ULARGE_INTEGER totalNumberOfFreeBytes;
250+
ULARGE_INTEGER totalAvailableToCaller;
249251

250252
// https://learn.microsoft.com/en-us/windows/win32/fileio/maximum-file-path-limitation?tabs=registry
251253
#define MAX_DRIVE_LENGTH 255
@@ -256,7 +258,7 @@ static inline void netdata_set_hd_usage(PERF_DATA_BLOCK *pDataBlock,
256258
// https://devblogs.microsoft.com/oldnewthing/20071101-00/?p=24613
257259
// We are using the variable that should not be affected by qyota ()
258260
if ((GetDriveTypeA(path) == DRIVE_UNKNOWN) || !GetDiskFreeSpaceExA(path,
259-
NULL,
261+
&totalAvailableToCaller,
260262
&totalNumberOfBytes,
261263
&totalNumberOfFreeBytes)) {
262264
perflibGetInstanceCounter(pDataBlock, pObjectType, pi, &d->percentDiskFree);

0 commit comments

Comments
 (0)