MdePkg/SafeString.c: Fix code to be more readable
authorRuiyu Ni <ruiyu.ni@intel.com>
Mon, 6 Mar 2017 06:34:29 +0000 (14:34 +0800)
committerRuiyu Ni <ruiyu.ni@intel.com>
Tue, 7 Mar 2017 05:14:17 +0000 (13:14 +0800)
The change doesn't impact the functionality.

Contributed-under: TianoCore Contribution Agreement 1.0
Signed-off-by: Ruiyu Ni <ruiyu.ni@intel.com>
Reviewed-by: Hao A Wu <hao.a.wu@intel.com>
MdePkg/Library/BaseLib/SafeString.c

index b7895ad..249fe47 100644 (file)
@@ -1234,6 +1234,7 @@ StrToIpv6Address (
         //\r
         // Uintn won't exceed MAX_UINT16 if number of hexadecimal digit characters is no more than 4.\r
         //\r
+        ASSERT (AddressIndex + 1 < ARRAY_SIZE (Address->Addr));\r
         LocalAddress.Addr[AddressIndex] = (UINT8) ((UINT16) Uintn >> 8);\r
         LocalAddress.Addr[AddressIndex + 1] = (UINT8) Uintn;\r
         AddressIndex += 2;\r
@@ -1286,11 +1287,13 @@ StrToIpv6Address (
   }\r
   CopyMem (&Address->Addr[0], &LocalAddress.Addr[0], CompressStart);\r
   ZeroMem (&Address->Addr[CompressStart], ARRAY_SIZE (Address->Addr) - AddressIndex);\r
-  CopyMem (\r
-    &Address->Addr[CompressStart + ARRAY_SIZE (Address->Addr) - AddressIndex],\r
-    &LocalAddress.Addr[CompressStart],\r
-    AddressIndex - CompressStart\r
-    );\r
+  if (AddressIndex > CompressStart) {\r
+    CopyMem (\r
+      &Address->Addr[CompressStart + ARRAY_SIZE (Address->Addr) - AddressIndex],\r
+      &LocalAddress.Addr[CompressStart],\r
+      AddressIndex - CompressStart\r
+      );\r
+  }\r
 \r
   if (PrefixLength != NULL) {\r
     *PrefixLength = LocalPrefixLength;\r
@@ -3204,6 +3207,7 @@ AsciiStrToIpv6Address (
         //\r
         // Uintn won't exceed MAX_UINT16 if number of hexadecimal digit characters is no more than 4.\r
         //\r
+        ASSERT (AddressIndex + 1 < ARRAY_SIZE (Address->Addr));\r
         LocalAddress.Addr[AddressIndex] = (UINT8) ((UINT16) Uintn >> 8);\r
         LocalAddress.Addr[AddressIndex + 1] = (UINT8) Uintn;\r
         AddressIndex += 2;\r
@@ -3256,11 +3260,13 @@ AsciiStrToIpv6Address (
   }\r
   CopyMem (&Address->Addr[0], &LocalAddress.Addr[0], CompressStart);\r
   ZeroMem (&Address->Addr[CompressStart], ARRAY_SIZE (Address->Addr) - AddressIndex);\r
-  CopyMem (\r
-    &Address->Addr[CompressStart + ARRAY_SIZE (Address->Addr) - AddressIndex],\r
-    &LocalAddress.Addr[CompressStart],\r
-    AddressIndex - CompressStart\r
-    );\r
+  if (AddressIndex > CompressStart) {\r
+    CopyMem (\r
+      &Address->Addr[CompressStart + ARRAY_SIZE (Address->Addr) - AddressIndex],\r
+      &LocalAddress.Addr[CompressStart],\r
+      AddressIndex - CompressStart\r
+      );\r\r
+  }\r
 \r
   if (PrefixLength != NULL) {\r
     *PrefixLength = LocalPrefixLength;\r