[DISKPART] Improve the conversion of numeric command parameters

This commit is contained in:
Eric Kohl 2022-03-28 00:04:24 +02:00
parent 72087c1e3a
commit 8476ae6ed5
4 changed files with 64 additions and 29 deletions

View file

@ -286,6 +286,10 @@ BOOL list_main(INT argc, LPWSTR *argv);
BOOL merge_main(INT argc, LPWSTR *argv); BOOL merge_main(INT argc, LPWSTR *argv);
/* misc.c */ /* misc.c */
BOOL
IsDecString(
_In_ PWSTR pszDecString);
BOOL BOOL
IsHexString( IsHexString(
_In_ PWSTR pszHexString); _In_ PWSTR pszHexString);

View file

@ -10,6 +10,28 @@
/* FUNCTIONS ******************************************************************/ /* FUNCTIONS ******************************************************************/
BOOL
IsDecString(
_In_ PWSTR pszDecString)
{
PWSTR ptr;
if ((pszDecString == NULL) || (*pszDecString == UNICODE_NULL))
return FALSE;
ptr = pszDecString;
while (*ptr != UNICODE_NULL)
{
if (!iswdigit(*ptr))
return FALSE;
ptr++;
}
return TRUE;
}
BOOL BOOL
IsHexString( IsHexString(
_In_ PWSTR pszHexString) _In_ PWSTR pszHexString)

View file

@ -21,8 +21,7 @@ SelectDisk(
{ {
PLIST_ENTRY Entry; PLIST_ENTRY Entry;
PDISKENTRY DiskEntry; PDISKENTRY DiskEntry;
LONG lValue; ULONG ulValue;
LPWSTR endptr = NULL;
DPRINT("Select Disk()\n"); DPRINT("Select Disk()\n");
@ -41,9 +40,14 @@ SelectDisk(
return; return;
} }
lValue = wcstol(argv[2], &endptr, 10); if (!IsDecString(argv[2]))
if (((lValue == 0) && (endptr == argv[2])) || {
(lValue < 0)) ConResPuts(StdErr, IDS_ERROR_INVALID_ARGS);
return;
}
ulValue = wcstoul(argv[2], NULL, 10);
if ((ulValue == 0) && (errno == ERANGE))
{ {
ConResPuts(StdErr, IDS_ERROR_INVALID_ARGS); ConResPuts(StdErr, IDS_ERROR_INVALID_ARGS);
return; return;
@ -56,7 +60,7 @@ SelectDisk(
{ {
DiskEntry = CONTAINING_RECORD(Entry, DISKENTRY, ListEntry); DiskEntry = CONTAINING_RECORD(Entry, DISKENTRY, ListEntry);
if (DiskEntry->DiskNumber == (ULONG)lValue) if (DiskEntry->DiskNumber == ulValue)
{ {
CurrentDisk = DiskEntry; CurrentDisk = DiskEntry;
CurrentPartition = NULL; CurrentPartition = NULL;
@ -79,8 +83,7 @@ SelectPartition(
{ {
PLIST_ENTRY Entry; PLIST_ENTRY Entry;
PPARTENTRY PartEntry; PPARTENTRY PartEntry;
LONG lValue; ULONG ulValue;
LPWSTR endptr = NULL;
ULONG PartNumber = 1; ULONG PartNumber = 1;
DPRINT("Select Partition()\n"); DPRINT("Select Partition()\n");
@ -106,9 +109,14 @@ SelectPartition(
return; return;
} }
lValue = wcstol(argv[2], &endptr, 10); if (!IsDecString(argv[2]))
if (((lValue == 0) && (endptr == argv[2])) || {
(lValue < 0)) ConResPuts(StdErr, IDS_ERROR_INVALID_ARGS);
return;
}
ulValue = wcstoul(argv[2], NULL, 10);
if ((ulValue == 0) && (errno == ERANGE))
{ {
ConResPuts(StdErr, IDS_ERROR_INVALID_ARGS); ConResPuts(StdErr, IDS_ERROR_INVALID_ARGS);
return; return;
@ -121,7 +129,7 @@ SelectPartition(
if (PartEntry->PartitionType != 0) if (PartEntry->PartitionType != 0)
{ {
if (PartNumber == (ULONG)lValue) if (PartNumber == ulValue)
{ {
CurrentPartition = PartEntry; CurrentPartition = PartEntry;
ConResPrintf(StdOut, IDS_SELECT_PARTITION, PartNumber); ConResPrintf(StdOut, IDS_SELECT_PARTITION, PartNumber);
@ -141,7 +149,7 @@ SelectPartition(
if (PartEntry->PartitionType != 0) if (PartEntry->PartitionType != 0)
{ {
if (PartNumber == (ULONG)lValue) if (PartNumber == ulValue)
{ {
CurrentPartition = PartEntry; CurrentPartition = PartEntry;
ConResPrintf(StdOut, IDS_SELECT_PARTITION, PartNumber); ConResPrintf(StdOut, IDS_SELECT_PARTITION, PartNumber);
@ -165,8 +173,7 @@ SelectVolume(
{ {
PLIST_ENTRY Entry; PLIST_ENTRY Entry;
PVOLENTRY VolumeEntry; PVOLENTRY VolumeEntry;
LONG lValue; ULONG ulValue;
LPWSTR endptr = NULL;
DPRINT("SelectVolume()\n"); DPRINT("SelectVolume()\n");
@ -185,9 +192,14 @@ SelectVolume(
return; return;
} }
lValue = wcstol(argv[2], &endptr, 10); if (!IsDecString(argv[2]))
if (((lValue == 0) && (endptr == argv[2])) || {
(lValue < 0)) ConResPuts(StdErr, IDS_ERROR_INVALID_ARGS);
return;
}
ulValue = wcstoul(argv[2], NULL, 10);
if ((ulValue == 0) && (errno == ERANGE))
{ {
ConResPuts(StdErr, IDS_ERROR_INVALID_ARGS); ConResPuts(StdErr, IDS_ERROR_INVALID_ARGS);
return; return;
@ -200,7 +212,7 @@ SelectVolume(
{ {
VolumeEntry = CONTAINING_RECORD(Entry, VOLENTRY, ListEntry); VolumeEntry = CONTAINING_RECORD(Entry, VOLENTRY, ListEntry);
if (VolumeEntry->VolumeNumber == (ULONG)lValue) if (VolumeEntry->VolumeNumber == ulValue)
{ {
CurrentVolume = VolumeEntry; CurrentVolume = VolumeEntry;
ConResPrintf(StdOut, IDS_SELECT_VOLUME, CurrentVolume->VolumeNumber); ConResPrintf(StdOut, IDS_SELECT_VOLUME, CurrentVolume->VolumeNumber);

View file

@ -19,9 +19,7 @@ UniqueIdDisk(
_In_ INT argc, _In_ INT argc,
_In_ LPWSTR *argv) _In_ LPWSTR *argv)
{ {
ULONG ulLength; ULONG ulLength, ulValue;
ULONG ulValue;
PWSTR startptr = NULL, endptr = NULL;
if (CurrentDisk == NULL) if (CurrentDisk == NULL)
{ {
@ -39,31 +37,30 @@ UniqueIdDisk(
if (argc != 3) if (argc != 3)
{ {
ConResPuts(StdOut, IDS_ERROR_INVALID_ARGS); ConResPuts(StdErr, IDS_ERROR_INVALID_ARGS);
return; return;
} }
ulLength = wcslen(argv[2]); ulLength = wcslen(argv[2]);
if ((ulLength <= 3) || (ulLength > 11)) if ((ulLength <= 3) || (ulLength > 11))
{ {
ConResPuts(StdOut, IDS_ERROR_INVALID_ARGS); ConResPuts(StdErr, IDS_ERROR_INVALID_ARGS);
return; return;
} }
if (!HasPrefix(argv[2], L"ID=")) if (!HasPrefix(argv[2], L"ID="))
{ {
ConResPuts(StdOut, IDS_ERROR_INVALID_ARGS); ConResPuts(StdErr, IDS_ERROR_INVALID_ARGS);
return; return;
} }
startptr = &argv[2][3]; if (!IsHexString(&argv[2][3]))
if (!IsHexString(startptr))
{ {
ConResPuts(StdOut, IDS_ERROR_INVALID_ARGS); ConResPuts(StdErr, IDS_ERROR_INVALID_ARGS);
return; return;
} }
ulValue = wcstoul(startptr, &endptr, 16); ulValue = wcstoul(&argv[2][3], NULL, 16);
if ((ulValue == 0) && (errno == ERANGE)) if ((ulValue == 0) && (errno == ERANGE))
{ {
ConResPuts(StdErr, IDS_ERROR_INVALID_ARGS); ConResPuts(StdErr, IDS_ERROR_INVALID_ARGS);