From 70d083bfb2c5dfb5537c5803aef5c8627c8cb551 Mon Sep 17 00:00:00 2001 From: Mounir IDRASSI Date: Sun, 11 Jun 2017 14:38:30 +0200 Subject: Windows: Add checks on IOCTL_DISK_GET_DRIVE_LAYOUT_EX response to make Coverity happy. --- src/Common/Dlgcode.c | 92 +++++++++++++++++++++++++++------------------------- 1 file changed, 48 insertions(+), 44 deletions(-) (limited to 'src/Common') diff --git a/src/Common/Dlgcode.c b/src/Common/Dlgcode.c index fbeb5675..af3bec0b 100644 --- a/src/Common/Dlgcode.c +++ b/src/Common/Dlgcode.c @@ -11995,62 +11995,66 @@ void UpdateMountableHostDeviceList () (LPVOID) buffer.data(), (DWORD) buffer.size(), (LPDWORD) &bytesReturned, - NULL)) + NULL) && (bytesReturned >= sizeof (DRIVE_LAYOUT_INFORMATION_EX))) { PDRIVE_LAYOUT_INFORMATION_EX layout = (PDRIVE_LAYOUT_INFORMATION_EX) buffer.data(); - for (DWORD i = 0; i < layout->PartitionCount; i++) + // sanity checks + if (layout->PartitionCount <= 256) { - if (layout->PartitionEntry[i].PartitionStyle == PARTITION_STYLE_MBR) + for (DWORD i = 0; i < layout->PartitionCount; i++) { - if (layout->PartitionEntry[i].Mbr.PartitionType == 0) - continue; + if (layout->PartitionEntry[i].PartitionStyle == PARTITION_STYLE_MBR) + { + if (layout->PartitionEntry[i].Mbr.PartitionType == 0) + continue; - bHasPartition = true; + bHasPartition = true; - /* skip dynamic volume */ - if (layout->PartitionEntry[i].Mbr.PartitionType == PARTITION_LDM) - { - bIsDynamic = true; - /* remove any partition that may have been added */ - while (!mountableDevices.empty() && (mountableDevices.back().SystemNumber == It->SystemNumber)) - mountableDevices.pop_back (); - break; + /* skip dynamic volume */ + if (layout->PartitionEntry[i].Mbr.PartitionType == PARTITION_LDM) + { + bIsDynamic = true; + /* remove any partition that may have been added */ + while (!mountableDevices.empty() && (mountableDevices.back().SystemNumber == It->SystemNumber)) + mountableDevices.pop_back (); + break; + } } - } - if (layout->PartitionEntry[i].PartitionStyle == PARTITION_STYLE_GPT) - { - if (IsEqualGUID(layout->PartitionEntry[i].Gpt.PartitionType, PARTITION_ENTRY_UNUSED_GUID)) - continue; + if (layout->PartitionEntry[i].PartitionStyle == PARTITION_STYLE_GPT) + { + if (IsEqualGUID(layout->PartitionEntry[i].Gpt.PartitionType, PARTITION_ENTRY_UNUSED_GUID)) + continue; - bHasPartition = true; + bHasPartition = true; - /* skip dynamic volume */ - if ( IsEqualGUID(layout->PartitionEntry[i].Gpt.PartitionType, PARTITION_LDM_METADATA_GUID) - || IsEqualGUID(layout->PartitionEntry[i].Gpt.PartitionType, PARTITION_LDM_DATA_GUID) - ) - { - bIsDynamic = true; - /* remove any partition that may have been added */ - while (!mountableDevices.empty() && (mountableDevices.back().SystemNumber == It->SystemNumber)) - mountableDevices.pop_back (); - break; + /* skip dynamic volume */ + if ( IsEqualGUID(layout->PartitionEntry[i].Gpt.PartitionType, PARTITION_LDM_METADATA_GUID) + || IsEqualGUID(layout->PartitionEntry[i].Gpt.PartitionType, PARTITION_LDM_DATA_GUID) + ) + { + bIsDynamic = true; + /* remove any partition that may have been added */ + while (!mountableDevices.empty() && (mountableDevices.back().SystemNumber == It->SystemNumber)) + mountableDevices.pop_back (); + break; + } } - } - WCHAR path[MAX_PATH]; - StringCbPrintfW (path, sizeof(path), L"\\\\?\\GLOBALROOT\\Device\\Harddisk%d\\Partition%d", It->SystemNumber, layout->PartitionEntry[i].PartitionNumber); - HANDLE handle = CreateFile( path, - 0, - FILE_SHARE_READ | FILE_SHARE_WRITE, - NULL, - OPEN_EXISTING, - 0, - NULL ); - if (handle != INVALID_HANDLE_VALUE) - { - AddDeviceToList (mountableDevices, It->SystemNumber, layout->PartitionEntry[i].PartitionNumber); - CloseHandle (handle); + WCHAR path[MAX_PATH]; + StringCbPrintfW (path, sizeof(path), L"\\\\?\\GLOBALROOT\\Device\\Harddisk%d\\Partition%d", It->SystemNumber, layout->PartitionEntry[i].PartitionNumber); + HANDLE handle = CreateFile( path, + 0, + FILE_SHARE_READ | FILE_SHARE_WRITE, + NULL, + OPEN_EXISTING, + 0, + NULL ); + if (handle != INVALID_HANDLE_VALUE) + { + AddDeviceToList (mountableDevices, It->SystemNumber, layout->PartitionEntry[i].PartitionNumber); + CloseHandle (handle); + } } } } -- cgit v1.2.3