https://git.reactos.org/?p=reactos.git;a=commitdiff;h=8476ae6ed511b694ca85b…
commit 8476ae6ed511b694ca85bcebdc70fff007cc1911
Author: Eric Kohl <eric.kohl(a)reactos.org>
AuthorDate: Mon Mar 28 00:04:24 2022 +0200
Commit: Eric Kohl <eric.kohl(a)reactos.org>
CommitDate: Mon Mar 28 00:04:24 2022 +0200
[DISKPART] Improve the conversion of numeric command parameters
---
base/system/diskpart/diskpart.h | 4 ++++
base/system/diskpart/misc.c | 22 ++++++++++++++++++
base/system/diskpart/select.c | 50 +++++++++++++++++++++++++----------------
base/system/diskpart/uniqueid.c | 17 ++++++--------
4 files changed, 64 insertions(+), 29 deletions(-)
diff --git a/base/system/diskpart/diskpart.h b/base/system/diskpart/diskpart.h
index 39aae2f8a94..877f0063351 100644
--- a/base/system/diskpart/diskpart.h
+++ b/base/system/diskpart/diskpart.h
@@ -286,6 +286,10 @@ BOOL list_main(INT argc, LPWSTR *argv);
BOOL merge_main(INT argc, LPWSTR *argv);
/* misc.c */
+BOOL
+IsDecString(
+ _In_ PWSTR pszDecString);
+
BOOL
IsHexString(
_In_ PWSTR pszHexString);
diff --git a/base/system/diskpart/misc.c b/base/system/diskpart/misc.c
index 2487530d405..65ba3a0ddb0 100644
--- a/base/system/diskpart/misc.c
+++ b/base/system/diskpart/misc.c
@@ -10,6 +10,28 @@
/* 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
IsHexString(
_In_ PWSTR pszHexString)
diff --git a/base/system/diskpart/select.c b/base/system/diskpart/select.c
index 4a1804ebbfd..aa2432484ae 100644
--- a/base/system/diskpart/select.c
+++ b/base/system/diskpart/select.c
@@ -21,8 +21,7 @@ SelectDisk(
{
PLIST_ENTRY Entry;
PDISKENTRY DiskEntry;
- LONG lValue;
- LPWSTR endptr = NULL;
+ ULONG ulValue;
DPRINT("Select Disk()\n");
@@ -41,9 +40,14 @@ SelectDisk(
return;
}
- lValue = wcstol(argv[2], &endptr, 10);
- if (((lValue == 0) && (endptr == argv[2])) ||
- (lValue < 0))
+ if (!IsDecString(argv[2]))
+ {
+ ConResPuts(StdErr, IDS_ERROR_INVALID_ARGS);
+ return;
+ }
+
+ ulValue = wcstoul(argv[2], NULL, 10);
+ if ((ulValue == 0) && (errno == ERANGE))
{
ConResPuts(StdErr, IDS_ERROR_INVALID_ARGS);
return;
@@ -56,7 +60,7 @@ SelectDisk(
{
DiskEntry = CONTAINING_RECORD(Entry, DISKENTRY, ListEntry);
- if (DiskEntry->DiskNumber == (ULONG)lValue)
+ if (DiskEntry->DiskNumber == ulValue)
{
CurrentDisk = DiskEntry;
CurrentPartition = NULL;
@@ -79,8 +83,7 @@ SelectPartition(
{
PLIST_ENTRY Entry;
PPARTENTRY PartEntry;
- LONG lValue;
- LPWSTR endptr = NULL;
+ ULONG ulValue;
ULONG PartNumber = 1;
DPRINT("Select Partition()\n");
@@ -106,9 +109,14 @@ SelectPartition(
return;
}
- lValue = wcstol(argv[2], &endptr, 10);
- if (((lValue == 0) && (endptr == argv[2])) ||
- (lValue < 0))
+ if (!IsDecString(argv[2]))
+ {
+ ConResPuts(StdErr, IDS_ERROR_INVALID_ARGS);
+ return;
+ }
+
+ ulValue = wcstoul(argv[2], NULL, 10);
+ if ((ulValue == 0) && (errno == ERANGE))
{
ConResPuts(StdErr, IDS_ERROR_INVALID_ARGS);
return;
@@ -121,7 +129,7 @@ SelectPartition(
if (PartEntry->PartitionType != 0)
{
- if (PartNumber == (ULONG)lValue)
+ if (PartNumber == ulValue)
{
CurrentPartition = PartEntry;
ConResPrintf(StdOut, IDS_SELECT_PARTITION, PartNumber);
@@ -141,7 +149,7 @@ SelectPartition(
if (PartEntry->PartitionType != 0)
{
- if (PartNumber == (ULONG)lValue)
+ if (PartNumber == ulValue)
{
CurrentPartition = PartEntry;
ConResPrintf(StdOut, IDS_SELECT_PARTITION, PartNumber);
@@ -165,8 +173,7 @@ SelectVolume(
{
PLIST_ENTRY Entry;
PVOLENTRY VolumeEntry;
- LONG lValue;
- LPWSTR endptr = NULL;
+ ULONG ulValue;
DPRINT("SelectVolume()\n");
@@ -185,9 +192,14 @@ SelectVolume(
return;
}
- lValue = wcstol(argv[2], &endptr, 10);
- if (((lValue == 0) && (endptr == argv[2])) ||
- (lValue < 0))
+ if (!IsDecString(argv[2]))
+ {
+ ConResPuts(StdErr, IDS_ERROR_INVALID_ARGS);
+ return;
+ }
+
+ ulValue = wcstoul(argv[2], NULL, 10);
+ if ((ulValue == 0) && (errno == ERANGE))
{
ConResPuts(StdErr, IDS_ERROR_INVALID_ARGS);
return;
@@ -200,7 +212,7 @@ SelectVolume(
{
VolumeEntry = CONTAINING_RECORD(Entry, VOLENTRY, ListEntry);
- if (VolumeEntry->VolumeNumber == (ULONG)lValue)
+ if (VolumeEntry->VolumeNumber == ulValue)
{
CurrentVolume = VolumeEntry;
ConResPrintf(StdOut, IDS_SELECT_VOLUME, CurrentVolume->VolumeNumber);
diff --git a/base/system/diskpart/uniqueid.c b/base/system/diskpart/uniqueid.c
index df7d3bf6696..28c206ec65d 100644
--- a/base/system/diskpart/uniqueid.c
+++ b/base/system/diskpart/uniqueid.c
@@ -19,9 +19,7 @@ UniqueIdDisk(
_In_ INT argc,
_In_ LPWSTR *argv)
{
- ULONG ulLength;
- ULONG ulValue;
- PWSTR startptr = NULL, endptr = NULL;
+ ULONG ulLength, ulValue;
if (CurrentDisk == NULL)
{
@@ -39,31 +37,30 @@ UniqueIdDisk(
if (argc != 3)
{
- ConResPuts(StdOut, IDS_ERROR_INVALID_ARGS);
+ ConResPuts(StdErr, IDS_ERROR_INVALID_ARGS);
return;
}
ulLength = wcslen(argv[2]);
if ((ulLength <= 3) || (ulLength > 11))
{
- ConResPuts(StdOut, IDS_ERROR_INVALID_ARGS);
+ ConResPuts(StdErr, IDS_ERROR_INVALID_ARGS);
return;
}
if (!HasPrefix(argv[2], L"ID="))
{
- ConResPuts(StdOut, IDS_ERROR_INVALID_ARGS);
+ ConResPuts(StdErr, IDS_ERROR_INVALID_ARGS);
return;
}
- startptr = &argv[2][3];
- if (!IsHexString(startptr))
+ if (!IsHexString(&argv[2][3]))
{
- ConResPuts(StdOut, IDS_ERROR_INVALID_ARGS);
+ ConResPuts(StdErr, IDS_ERROR_INVALID_ARGS);
return;
}
- ulValue = wcstoul(startptr, &endptr, 16);
+ ulValue = wcstoul(&argv[2][3], NULL, 16);
if ((ulValue == 0) && (errno == ERANGE))
{
ConResPuts(StdErr, IDS_ERROR_INVALID_ARGS);