]> git.proxmox.com Git - mirror_edk2.git/commitdiff
MdePkg/BaseLib: Enhance the return value for string to uint functions
authorHao Wu <hao.a.wu@intel.com>
Wed, 14 Dec 2016 03:04:48 +0000 (11:04 +0800)
committerHao Wu <hao.a.wu@intel.com>
Mon, 9 Jan 2017 05:59:26 +0000 (13:59 +0800)
For the following 8 APIs in MdePkg/BaseLib:
[Ascii]StrDecimalToUintn
[Ascii]StrDecimalToUint64
[Ascii]StrHexToUintn
[Ascii]StrHexToUint64

They will ASSERT for DEBUG build when the input string exceeds the range
of UINTN/UINT64. However, for RELEASE build, incorrect value will be
returned.

This commit refines those APIs to direcly call their enhanced counterparts
(with trailing 'S' in API names) so as to remove those exceed-range ASSERT
checks and to make those APIs to return MAX_UINTN/MAX_UINT64 instead.

Contributed-under: TianoCore Contribution Agreement 1.0
Signed-off-by: Hao Wu <hao.a.wu@intel.com>
Reviewed-by: Jiewen Yao <jiewen.yao@intel.com>
MdePkg/Include/Library/BaseLib.h
MdePkg/Library/BaseLib/String.c

index 52011ee072796861134fbbffd5ece11eae9e0ac0..abea7b6724aed4c741e6e1d987ef76f39e23a076 100644 (file)
@@ -1395,7 +1395,7 @@ StrStr (
   If String has no pad spaces or valid decimal digits,\r
   then 0 is returned.\r
   If the number represented by String overflows according\r
-  to the range defined by UINTN, then ASSERT().\r
+  to the range defined by UINTN, then MAX_UINTN is returned.\r
 \r
   If PcdMaximumUnicodeStringLength is not zero, and String contains\r
   more than PcdMaximumUnicodeStringLength Unicode characters not including\r
@@ -1435,7 +1435,7 @@ StrDecimalToUintn (
   If String has no pad spaces or valid decimal digits,\r
   then 0 is returned.\r
   If the number represented by String overflows according\r
-  to the range defined by UINT64, then ASSERT().\r
+  to the range defined by UINT64, then MAX_UINT64 is returned.\r
 \r
   If PcdMaximumUnicodeStringLength is not zero, and String contains\r
   more than PcdMaximumUnicodeStringLength Unicode characters not including\r
@@ -1477,7 +1477,7 @@ StrDecimalToUint64 (
   If String has no leading pad spaces, leading zeros or valid hexadecimal digits,\r
   then zero is returned.\r
   If the number represented by String overflows according to the range defined by\r
-  UINTN, then ASSERT().\r
+  UINTN, then MAX_UINTN is returned.\r
 \r
   If PcdMaximumUnicodeStringLength is not zero, and String contains more than\r
   PcdMaximumUnicodeStringLength Unicode characters not including the Null-terminator,\r
@@ -1519,7 +1519,7 @@ StrHexToUintn (
   If String has no leading pad spaces, leading zeros or valid hexadecimal digits,\r
   then zero is returned.\r
   If the number represented by String overflows according to the range defined by\r
-  UINT64, then ASSERT().\r
+  UINT64, then MAX_UINT64 is returned.\r
 \r
   If PcdMaximumUnicodeStringLength is not zero, and String contains more than\r
   PcdMaximumUnicodeStringLength Unicode characters not including the Null-terminator,\r
@@ -1997,7 +1997,7 @@ AsciiStrStr (
   If String has only pad spaces, then 0 is returned.\r
   If String has no pad spaces or valid decimal digits, then 0 is returned.\r
   If the number represented by String overflows according to the range defined by\r
-  UINTN, then ASSERT().\r
+  UINTN, then MAX_UINTN is returned.\r
   If String is NULL, then ASSERT().\r
   If PcdMaximumAsciiStringLength is not zero, and String contains more than\r
   PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator,\r
@@ -2034,7 +2034,7 @@ AsciiStrDecimalToUintn (
   If String has only pad spaces, then 0 is returned.\r
   If String has no pad spaces or valid decimal digits, then 0 is returned.\r
   If the number represented by String overflows according to the range defined by\r
-  UINT64, then ASSERT().\r
+  UINT64, then MAX_UINT64 is returned.\r
   If String is NULL, then ASSERT().\r
   If PcdMaximumAsciiStringLength is not zero, and String contains more than\r
   PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator,\r
@@ -2075,7 +2075,7 @@ AsciiStrDecimalToUint64 (
   0 is returned.\r
 \r
   If the number represented by String overflows according to the range defined by UINTN,\r
-  then ASSERT().\r
+  then MAX_UINTN is returned.\r
   If String is NULL, then ASSERT().\r
   If PcdMaximumAsciiStringLength is not zero,\r
   and String contains more than PcdMaximumAsciiStringLength ASCII characters not including\r
@@ -2116,7 +2116,7 @@ AsciiStrHexToUintn (
   0 is returned.\r
 \r
   If the number represented by String overflows according to the range defined by UINT64,\r
-  then ASSERT().\r
+  then MAX_UINT64 is returned.\r
   If String is NULL, then ASSERT().\r
   If PcdMaximumAsciiStringLength is not zero,\r
   and String contains more than PcdMaximumAsciiStringLength ASCII characters not including\r
index fa96d1c1eec8ceedd3f7bc0da8f31e839ce0b675..e84bf50d7f6220a2d911ce3f73f7cb311930f34d 100644 (file)
@@ -1,7 +1,7 @@
 /** @file\r
   Unicode and ASCII string primitives.\r
 \r
-  Copyright (c) 2006 - 2016, Intel Corporation. All rights reserved.<BR>\r
+  Copyright (c) 2006 - 2017, Intel Corporation. All rights reserved.<BR>\r
   This program and the accompanying materials\r
   are licensed and made available under the terms and conditions of the BSD License\r
   which accompanies this distribution.  The full text of the license may be found at\r
@@ -638,7 +638,7 @@ InternalIsHexaDecimalDigitCharacter (
   If String has no pad spaces or valid decimal digits,\r
   then 0 is returned.\r
   If the number represented by String overflows according\r
-  to the range defined by UINTN, then ASSERT().\r
+  to the range defined by UINTN, then MAX_UINTN is returned.\r
 \r
   If PcdMaximumUnicodeStringLength is not zero, and String contains\r
   more than PcdMaximumUnicodeStringLength Unicode characters, not including\r
@@ -656,40 +656,8 @@ StrDecimalToUintn (
   )\r
 {\r
   UINTN     Result;\r
-  \r
-  //\r
-  // ASSERT String is less long than PcdMaximumUnicodeStringLength.\r
-  // Length tests are performed inside StrLen().\r
-  //\r
-  ASSERT (StrSize (String) != 0);\r
-\r
-  //\r
-  // Ignore the pad spaces (space or tab)\r
-  //\r
-  while ((*String == L' ') || (*String == L'\t')) {\r
-    String++;\r
-  }\r
-\r
-  //\r
-  // Ignore leading Zeros after the spaces\r
-  //\r
-  while (*String == L'0') {\r
-    String++;\r
-  }\r
-\r
-  Result = 0;\r
-\r
-  while (InternalIsDecimalDigitCharacter (*String)) {\r
-    //\r
-    // If the number represented by String overflows according \r
-    // to the range defined by UINTN, then ASSERT().\r
-    //\r
-    ASSERT (Result <= ((((UINTN) ~0) - (*String - L'0')) / 10));\r
 \r
-    Result = Result * 10 + (*String - L'0');\r
-    String++;\r
-  }\r
-  \r
+  StrDecimalToUintnS (String, (CHAR16 **) NULL, &Result);\r
   return Result;\r
 }\r
 \r
@@ -717,7 +685,7 @@ StrDecimalToUintn (
   If String has no pad spaces or valid decimal digits,\r
   then 0 is returned.\r
   If the number represented by String overflows according\r
-  to the range defined by UINT64, then ASSERT().\r
+  to the range defined by UINT64, then MAX_UINT64 is returned.\r
 \r
   If PcdMaximumUnicodeStringLength is not zero, and String contains\r
   more than PcdMaximumUnicodeStringLength Unicode characters, not including\r
@@ -736,39 +704,7 @@ StrDecimalToUint64 (
 {\r
   UINT64     Result;\r
   \r
-  //\r
-  // ASSERT String is less long than PcdMaximumUnicodeStringLength.\r
-  // Length tests are performed inside StrLen().\r
-  //\r
-  ASSERT (StrSize (String) != 0);\r
-\r
-  //\r
-  // Ignore the pad spaces (space or tab)\r
-  //\r
-  while ((*String == L' ') || (*String == L'\t')) {\r
-    String++;\r
-  }\r
-\r
-  //\r
-  // Ignore leading Zeros after the spaces\r
-  //\r
-  while (*String == L'0') {\r
-    String++;\r
-  }\r
-\r
-  Result = 0;\r
-\r
-  while (InternalIsDecimalDigitCharacter (*String)) {\r
-    //\r
-    // If the number represented by String overflows according \r
-    // to the range defined by UINTN, then ASSERT().\r
-    //\r
-    ASSERT (Result <= DivU64x32 (((UINT64) ~0) - (*String - L'0') , 10));\r
-\r
-    Result = MultU64x32 (Result, 10) + (*String - L'0');\r
-    String++;\r
-  }\r
-  \r
+  StrDecimalToUint64S (String, (CHAR16 **) NULL, &Result);\r
   return Result;\r
 }\r
 \r
@@ -796,7 +732,7 @@ StrDecimalToUint64 (
   If String has no leading pad spaces, leading zeros or valid hexadecimal digits,\r
   then zero is returned.\r
   If the number represented by String overflows according to the range defined by\r
-  UINTN, then ASSERT().\r
+  UINTN, then MAX_UINTN is returned.\r
 \r
   If PcdMaximumUnicodeStringLength is not zero, and String contains more than\r
   PcdMaximumUnicodeStringLength Unicode characters, not including the Null-terminator,\r
@@ -815,49 +751,7 @@ StrHexToUintn (
 {\r
   UINTN     Result;\r
 \r
-  //\r
-  // ASSERT String is less long than PcdMaximumUnicodeStringLength.\r
-  // Length tests are performed inside StrLen().\r
-  //\r
-  ASSERT (StrSize (String) != 0);\r
-  \r
-  //\r
-  // Ignore the pad spaces (space or tab) \r
-  //\r
-  while ((*String == L' ') || (*String == L'\t')) {\r
-    String++;\r
-  }\r
-\r
-  //\r
-  // Ignore leading Zeros after the spaces\r
-  //\r
-  while (*String == L'0') {\r
-    String++;\r
-  }\r
-\r
-  if (InternalCharToUpper (*String) == L'X') {\r
-    if (*(String - 1) != L'0') {\r
-      return 0;\r
-    }\r
-    //\r
-    // Skip the 'X'\r
-    //\r
-    String++;\r
-  }\r
-\r
-  Result = 0;\r
-  \r
-  while (InternalIsHexaDecimalDigitCharacter (*String)) {\r
-    //\r
-    // If the Hex Number represented by String overflows according \r
-    // to the range defined by UINTN, then ASSERT().\r
-    //\r
-    ASSERT (Result <= ((((UINTN) ~0) - InternalHexCharToUintn (*String)) >> 4));\r
-\r
-    Result = (Result << 4) + InternalHexCharToUintn (*String);\r
-    String++;\r
-  }\r
-\r
+  StrHexToUintnS (String, (CHAR16 **) NULL, &Result);\r
   return Result;\r
 }\r
 \r
@@ -886,7 +780,7 @@ StrHexToUintn (
   If String has no leading pad spaces, leading zeros or valid hexadecimal digits,\r
   then zero is returned.\r
   If the number represented by String overflows according to the range defined by\r
-  UINT64, then ASSERT().\r
+  UINT64, then MAX_UINT64 is returned.\r
 \r
   If PcdMaximumUnicodeStringLength is not zero, and String contains more than\r
   PcdMaximumUnicodeStringLength Unicode characters, not including the Null-terminator,\r
@@ -905,51 +799,7 @@ StrHexToUint64 (
 {\r
   UINT64    Result;\r
 \r
-  //\r
-  // ASSERT String is less long than PcdMaximumUnicodeStringLength.\r
-  // Length tests are performed inside StrLen().\r
-  //\r
-  ASSERT (StrSize (String) != 0);\r
-  \r
-  //\r
-  // Ignore the pad spaces (space or tab) \r
-  //\r
-  while ((*String == L' ') || (*String == L'\t')) {\r
-    String++;\r
-  }\r
-\r
-  //\r
-  // Ignore leading Zeros after the spaces\r
-  //\r
-  while (*String == L'0') {\r
-    String++;\r
-  }\r
-\r
-  if (InternalCharToUpper (*String) == L'X') {\r
-    ASSERT (*(String - 1) == L'0');\r
-    if (*(String - 1) != L'0') {\r
-      return 0;\r
-    }\r
-    //\r
-    // Skip the 'X'\r
-    //\r
-    String++;\r
-  }\r
-\r
-  Result = 0;\r
-  \r
-  while (InternalIsHexaDecimalDigitCharacter (*String)) {\r
-    //\r
-    // If the Hex Number represented by String overflows according \r
-    // to the range defined by UINTN, then ASSERT().\r
-    //\r
-    ASSERT (Result <= RShiftU64 (((UINT64) ~0) - InternalHexCharToUintn (*String) , 4));\r
-\r
-    Result = LShiftU64 (Result, 4);\r
-    Result = Result + InternalHexCharToUintn (*String);\r
-    String++;\r
-  }\r
-\r
+  StrHexToUint64S (String, (CHAR16 **) NULL, &Result);\r
   return Result;\r
 }\r
 \r
@@ -1681,7 +1531,7 @@ AsciiStrStr (
   If String has only pad spaces, then 0 is returned.\r
   If String has no pad spaces or valid decimal digits, then 0 is returned.\r
   If the number represented by String overflows according to the range defined by\r
-  UINTN, then ASSERT().\r
+  UINTN, then MAX_UINTN is returned.\r
   If String is NULL, then ASSERT().\r
   If PcdMaximumAsciiStringLength is not zero, and String contains more than\r
   PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator,\r
@@ -1700,38 +1550,7 @@ AsciiStrDecimalToUintn (
 {\r
   UINTN     Result;\r
   \r
-  //\r
-  // ASSERT Strings is less long than PcdMaximumAsciiStringLength\r
-  //\r
-  ASSERT (AsciiStrSize (String) != 0);\r
-\r
-  //\r
-  // Ignore the pad spaces (space or tab)\r
-  //\r
-  while ((*String == ' ') || (*String == '\t' )) {\r
-    String++;\r
-  }\r
-\r
-  //\r
-  // Ignore leading Zeros after the spaces\r
-  //\r
-  while (*String == '0') {\r
-    String++;\r
-  }\r
-\r
-  Result = 0;\r
-\r
-  while (InternalAsciiIsDecimalDigitCharacter (*String)) {\r
-    //\r
-    // If the number represented by String overflows according \r
-    // to the range defined by UINTN, then ASSERT().\r
-    //\r
-    ASSERT (Result <= ((((UINTN) ~0) - (*String - L'0')) / 10));\r
-\r
-    Result = Result * 10 + (*String - '0');\r
-    String++;\r
-  }\r
-  \r
+  AsciiStrDecimalToUintnS (String, (CHAR8 **) NULL, &Result);\r
   return Result;\r
 }\r
 \r
@@ -1755,7 +1574,7 @@ AsciiStrDecimalToUintn (
   If String has only pad spaces, then 0 is returned.\r
   If String has no pad spaces or valid decimal digits, then 0 is returned.\r
   If the number represented by String overflows according to the range defined by\r
-  UINT64, then ASSERT().\r
+  UINT64, then MAX_UINT64 is returned.\r
   If String is NULL, then ASSERT().\r
   If PcdMaximumAsciiStringLength is not zero, and String contains more than\r
   PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator,\r
@@ -1774,38 +1593,7 @@ AsciiStrDecimalToUint64 (
 {\r
   UINT64     Result;\r
   \r
-  //\r
-  // ASSERT Strings is less long than PcdMaximumAsciiStringLength\r
-  //\r
-  ASSERT (AsciiStrSize (String) != 0);\r
-\r
-  //\r
-  // Ignore the pad spaces (space or tab)\r
-  //\r
-  while ((*String == ' ') || (*String == '\t' )) {\r
-    String++;\r
-  }\r
-\r
-  //\r
-  // Ignore leading Zeros after the spaces\r
-  //\r
-  while (*String == '0') {\r
-    String++;\r
-  }\r
-\r
-  Result = 0;\r
-\r
-  while (InternalAsciiIsDecimalDigitCharacter (*String)) {\r
-    //\r
-    // If the number represented by String overflows according \r
-    // to the range defined by UINTN, then ASSERT().\r
-    //\r
-    ASSERT (Result <= DivU64x32 (((UINT64) ~0) - (*String - L'0') , 10));\r
-\r
-    Result = MultU64x32 (Result, 10) + (*String - '0');\r
-    String++;\r
-  }\r
-  \r
+  AsciiStrDecimalToUint64S (String, (CHAR8 **) NULL, &Result);\r
   return Result;\r
 }\r
 \r
@@ -1832,7 +1620,7 @@ AsciiStrDecimalToUint64 (
   0 is returned.\r
 \r
   If the number represented by String overflows according to the range defined by UINTN,\r
-  then ASSERT().\r
+  then MAX_UINTN is returned.\r
   If String is NULL, then ASSERT().\r
   If PcdMaximumAsciiStringLength is not zero,\r
   and String contains more than PcdMaximumAsciiStringLength ASCII characters not including\r
@@ -1851,49 +1639,7 @@ AsciiStrHexToUintn (
 {\r
   UINTN     Result;\r
 \r
-  //\r
-  // ASSERT Strings is less long than PcdMaximumAsciiStringLength\r
-  //\r
-  ASSERT (AsciiStrSize (String) != 0);\r
-  \r
-  //\r
-  // Ignore the pad spaces (space or tab) \r
-  //\r
-  while ((*String == ' ') || (*String == '\t' )) {\r
-    String++;\r
-  }\r
-\r
-  //\r
-  // Ignore leading Zeros after the spaces\r
-  //\r
-  while (*String == '0') {\r
-    String++;\r
-  }\r
-\r
-  if (InternalBaseLibAsciiToUpper (*String) == 'X') {\r
-    ASSERT (*(String - 1) == '0');\r
-    if (*(String - 1) != '0') {\r
-      return 0;\r
-    }\r
-    //\r
-    // Skip the 'X'\r
-    //\r
-    String++;\r
-  }\r
-\r
-  Result = 0;\r
-  \r
-  while (InternalAsciiIsHexaDecimalDigitCharacter (*String)) {\r
-    //\r
-    // If the Hex Number represented by String overflows according \r
-    // to the range defined by UINTN, then ASSERT().\r
-    //\r
-    ASSERT (Result <= ((((UINTN) ~0) - InternalHexCharToUintn (*String)) >> 4));\r
-\r
-    Result = (Result << 4) + InternalAsciiHexCharToUintn (*String);\r
-    String++;\r
-  }\r
-\r
+  AsciiStrHexToUintnS (String, (CHAR8 **) NULL, &Result);\r
   return Result;\r
 }\r
 \r
@@ -1921,7 +1667,7 @@ AsciiStrHexToUintn (
   0 is returned.\r
 \r
   If the number represented by String overflows according to the range defined by UINT64,\r
-  then ASSERT().\r
+  then MAX_UINT64 is returned.\r
   If String is NULL, then ASSERT().\r
   If PcdMaximumAsciiStringLength is not zero,\r
   and String contains more than PcdMaximumAsciiStringLength ASCII characters not including\r
@@ -1940,53 +1686,7 @@ AsciiStrHexToUint64 (
 {\r
   UINT64    Result;\r
 \r
-  //\r
-  // ASSERT Strings is less long than PcdMaximumAsciiStringLength\r
-  //\r
-  ASSERT (AsciiStrSize (String) != 0);\r
-  \r
-  //\r
-  // Ignore the pad spaces (space or tab) and leading Zeros\r
-  //\r
-  //\r
-  // Ignore the pad spaces (space or tab) \r
-  //\r
-  while ((*String == ' ') || (*String == '\t' )) {\r
-    String++;\r
-  }\r
-\r
-  //\r
-  // Ignore leading Zeros after the spaces\r
-  //\r
-  while (*String == '0') {\r
-    String++;\r
-  }\r
-\r
-  if (InternalBaseLibAsciiToUpper (*String) == 'X') {\r
-    ASSERT (*(String - 1) == '0');\r
-    if (*(String - 1) != '0') {\r
-      return 0;\r
-    }\r
-    //\r
-    // Skip the 'X'\r
-    //\r
-    String++;\r
-  }\r
-\r
-  Result = 0;\r
-  \r
-  while (InternalAsciiIsHexaDecimalDigitCharacter (*String)) {\r
-    //\r
-    // If the Hex Number represented by String overflows according \r
-    // to the range defined by UINTN, then ASSERT().\r
-    //\r
-    ASSERT (Result <= RShiftU64 (((UINT64) ~0) - InternalHexCharToUintn (*String) , 4));\r
-\r
-    Result = LShiftU64 (Result, 4);\r
-    Result = Result + InternalAsciiHexCharToUintn (*String);\r
-    String++;\r
-  }\r
-\r
+  AsciiStrHexToUint64S (String, (CHAR8 **) NULL, &Result);\r
   return Result;\r
 }\r
 \r