]> git.proxmox.com Git - mirror_edk2.git/commitdiff
BasePrintLib: Fix Buffer Overflow issue.
authorqhuang8 <qhuang8@6f19259b-4bc3-4df7-8a09-765794883524>
Thu, 13 Jul 2006 01:53:27 +0000 (01:53 +0000)
committerqhuang8 <qhuang8@6f19259b-4bc3-4df7-8a09-765794883524>
Thu, 13 Jul 2006 01:53:27 +0000 (01:53 +0000)
BaseMemoryLib: Fix error in CopyMem.S for BaseMemoryLibMmx & BaseMemoryLibRepStr instance.

git-svn-id: https://edk2.svn.sourceforge.net/svnroot/edk2/trunk/edk2@938 6f19259b-4bc3-4df7-8a09-765794883524

MdePkg/Include/Library/PrintLib.h
MdePkg/Library/BaseMemoryLibMmx/Ia32/CopyMem.S
MdePkg/Library/BaseMemoryLibRepStr/Ia32/CopyMem.S
MdePkg/Library/BasePrintLib/PrintLib.c
MdePkg/Library/BasePrintLib/PrintLibInternal.c
MdePkg/Library/BasePrintLib/PrintLibInternal.h

index e1b3cb37bba051bdb62606ff6351089b1b9cf674..004bc9f926bf0cef882f48b2a377fbbdaf2da390 100644 (file)
@@ -394,7 +394,8 @@ AsciiSPrintUnicodeFormat (
                   Unicode string.\r
   @param  Flags   The bitmask of flags that specify left justification, zero pad, and commas.\r
   @param  Value   The 64-bit signed value to convert to a string.\r
                   Unicode string.\r
   @param  Flags   The bitmask of flags that specify left justification, zero pad, and commas.\r
   @param  Value   The 64-bit signed value to convert to a string.\r
-  @param  Width   The maximum number of Unicode characters to place in Buffer.\r
+  @param  Width   The maximum number of Unicode characters to place in Buffer, not including\r
+                  the Null-terminator.\r
   \r
   @return The number of Unicode characters in Buffer not including the Null-terminator.\r
 \r
   \r
   @return The number of Unicode characters in Buffer not including the Null-terminator.\r
 \r
@@ -438,7 +439,8 @@ UnicodeValueToString (
                   ASCII string.\r
   @param  Flags   The bitmask of flags that specify left justification, zero pad, and commas.\r
   @param  Value   The 64-bit signed value to convert to a string.\r
                   ASCII string.\r
   @param  Flags   The bitmask of flags that specify left justification, zero pad, and commas.\r
   @param  Value   The 64-bit signed value to convert to a string.\r
-  @param  Width   The maximum number of ASCII characters to place in Buffer.\r
+  @param  Width   The maximum number of ASCII characters to place in Buffer, not including\r
+                  the Null-terminator.\r
   \r
   @return The number of ASCII characters in Buffer not including the Null-terminator.\r
 \r
   \r
   @return The number of ASCII characters in Buffer not including the Null-terminator.\r
 \r
index 56788cb9817338d648394fe15040deadd70fbc26..3c00c2a81eaa5ef964502c22f989e62048036974 100644 (file)
@@ -85,6 +85,6 @@ L2:
     movsb
     cld
     movl    12(%esp), %eax
     movsb
     cld
     movl    12(%esp), %eax
-    push    %esi
-    push    %edi
+    pop    %esi
+    pop    %edi
     ret
     ret
index cce9836833d48fcd1fa52fd7bd95103da654b4fb..4215c203935bb44975f31a71565851ca4e4a5baf 100644 (file)
@@ -53,6 +53,6 @@ L0:
     movsb                               # Copy bytes backward
     cld
     movl    12(%esp),%eax               # eax <- Destination as return value
     movsb                               # Copy bytes backward
     cld
     movl    12(%esp),%eax               # eax <- Destination as return value
-    push    %edi
-    push    %esi
+    pop    %edi
+    pop    %esi
     ret
     ret
index 33da6cb6b07795e7f56ec7a56d0a868b2c495c61..6b4f1fad4a49f6a561357d674b064d52f32af4dc 100644 (file)
@@ -80,6 +80,7 @@ BasePrintLibVSPrint (
   )\r
 {\r
   CHAR8           *OriginalBuffer;\r
   )\r
 {\r
   CHAR8           *OriginalBuffer;\r
+  CHAR8           *EndBuffer;\r
   CHAR8           ValueBuffer[MAXIMUM_VALUE_CHARACTERS];\r
   UINTN           BytesPerOutputCharacter;\r
   UINTN           BytesPerFormatCharacter;\r
   CHAR8           ValueBuffer[MAXIMUM_VALUE_CHARACTERS];\r
   UINTN           BytesPerOutputCharacter;\r
   UINTN           BytesPerFormatCharacter;\r
@@ -110,13 +111,22 @@ BasePrintLibVSPrint (
   }\r
   ASSERT (Buffer != NULL);\r
 \r
   }\r
   ASSERT (Buffer != NULL);\r
 \r
-  OriginalBuffer = Buffer;\r
-\r
   if ((Flags & OUTPUT_UNICODE) != 0) {\r
     BytesPerOutputCharacter = 2;\r
   } else {\r
     BytesPerOutputCharacter = 1;\r
   }\r
   if ((Flags & OUTPUT_UNICODE) != 0) {\r
     BytesPerOutputCharacter = 2;\r
   } else {\r
     BytesPerOutputCharacter = 1;\r
   }\r
+\r
+  //\r
+  // Reserve space for the Null terminator.\r
+  //\r
+  BufferSize--;\r
+  OriginalBuffer = Buffer;\r
+  //\r
+  // Set the tag for the end of the input Buffer.\r
+  //\r
+  EndBuffer      = Buffer + BufferSize * BytesPerOutputCharacter;\r
+\r
   if ((Flags & FORMAT_UNICODE) != 0) {\r
     //\r
     // Make sure format string cannot contain more than PcdMaximumUnicodeStringLength\r
   if ((Flags & FORMAT_UNICODE) != 0) {\r
     //\r
     // Make sure format string cannot contain more than PcdMaximumUnicodeStringLength\r
@@ -135,10 +145,7 @@ BasePrintLibVSPrint (
     FormatMask = 0xff;\r
   }\r
 \r
     FormatMask = 0xff;\r
   }\r
 \r
-  //\r
-  // Reserve space for the Null terminator.\r
-  //\r
-  BufferSize--;\r
+\r
 \r
   //\r
   // Get the first character from the format string\r
 \r
   //\r
   // Get the first character from the format string\r
@@ -148,7 +155,7 @@ BasePrintLibVSPrint (
   //\r
   // Loop until the end of the format string is reached or the output buffer is full\r
   //\r
   //\r
   // Loop until the end of the format string is reached or the output buffer is full\r
   //\r
-  while (FormatCharacter != 0 && BufferSize > 0) {\r
+  while (FormatCharacter != 0 && Buffer < EndBuffer) {\r
     //\r
     // Clear all the flag bits except those that may have been passed in\r
     //\r
     //\r
     // Clear all the flag bits except those that may have been passed in\r
     //\r
@@ -244,13 +251,6 @@ BasePrintLibVSPrint (
         }\r
       } \r
 \r
         }\r
       } \r
 \r
-      //\r
-      // Limit the maximum field width to the remaining characters in the output buffer\r
-      //\r
-      if (Width > BufferSize) {\r
-        Width = BufferSize;\r
-      }\r
-\r
       //\r
       // Handle each argument type\r
       //\r
       //\r
       // Handle each argument type\r
       //\r
@@ -477,12 +477,6 @@ BasePrintLibVSPrint (
       }\r
     }\r
 \r
       }\r
     }\r
 \r
-    //\r
-    // Limit the length of the string to append to the remaining characters in the output buffer\r
-    //\r
-    if (Count > BufferSize) {\r
-      Count = BufferSize;\r
-    }\r
     if (Precision < Count) {\r
       Precision = Count;\r
     }\r
     if (Precision < Count) {\r
       Precision = Count;\r
     }\r
@@ -491,18 +485,18 @@ BasePrintLibVSPrint (
     // Pad before the string\r
     //\r
     if ((Flags & (PAD_TO_WIDTH | LEFT_JUSTIFY)) == (PAD_TO_WIDTH)) {\r
     // Pad before the string\r
     //\r
     if ((Flags & (PAD_TO_WIDTH | LEFT_JUSTIFY)) == (PAD_TO_WIDTH)) {\r
-      Buffer = BasePrintLibFillBuffer (Buffer, Width - Precision, ' ', BytesPerOutputCharacter);\r
+      Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, Width - Precision, ' ', BytesPerOutputCharacter);\r
     }\r
 \r
     if (ZeroPad) {\r
       if (Prefix != 0) {\r
     }\r
 \r
     if (ZeroPad) {\r
       if (Prefix != 0) {\r
-        Buffer = BasePrintLibFillBuffer (Buffer, 1, Prefix, BytesPerOutputCharacter);\r
+        Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, 1, Prefix, BytesPerOutputCharacter);\r
       }\r
       }\r
-      Buffer = BasePrintLibFillBuffer (Buffer, Precision - Count, '0', BytesPerOutputCharacter);\r
+      Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, Precision - Count, '0', BytesPerOutputCharacter);\r
     } else {\r
     } else {\r
-      Buffer = BasePrintLibFillBuffer (Buffer, Precision - Count, ' ', BytesPerOutputCharacter);\r
+      Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, Precision - Count, ' ', BytesPerOutputCharacter);\r
       if (Prefix != 0) {\r
       if (Prefix != 0) {\r
-        Buffer = BasePrintLibFillBuffer (Buffer, 1, Prefix, BytesPerOutputCharacter);\r
+        Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, 1, Prefix, BytesPerOutputCharacter);\r
       }\r
     }\r
 \r
       }\r
     }\r
 \r
@@ -520,7 +514,7 @@ BasePrintLibVSPrint (
     while (Index < Count) {\r
       ArgumentCharacter = ((*ArgumentString & 0xff) | (*(ArgumentString + 1) << 8)) & ArgumentMask;\r
 \r
     while (Index < Count) {\r
       ArgumentCharacter = ((*ArgumentString & 0xff) | (*(ArgumentString + 1) << 8)) & ArgumentMask;\r
 \r
-      Buffer = BasePrintLibFillBuffer (Buffer, 1, ArgumentCharacter, BytesPerOutputCharacter);\r
+      Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, 1, ArgumentCharacter, BytesPerOutputCharacter);\r
       ArgumentString    += BytesPerArgumentCharacter;\r
       Index++;\r
       if (Comma) {\r
       ArgumentString    += BytesPerArgumentCharacter;\r
       Index++;\r
       if (Comma) {\r
@@ -529,7 +523,7 @@ BasePrintLibVSPrint (
           Digits = 0;\r
           Index++;\r
           if (Index < Count) {\r
           Digits = 0;\r
           Index++;\r
           if (Index < Count) {\r
-            Buffer = BasePrintLibFillBuffer (Buffer, 1, ',', BytesPerOutputCharacter);\r
+            Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, 1, ',', BytesPerOutputCharacter);\r
           }\r
         }\r
       }\r
           }\r
         }\r
       }\r
@@ -539,14 +533,9 @@ BasePrintLibVSPrint (
     // Pad after the string\r
     //\r
     if ((Flags & (PAD_TO_WIDTH | LEFT_JUSTIFY)) == (PAD_TO_WIDTH | LEFT_JUSTIFY)) {\r
     // Pad after the string\r
     //\r
     if ((Flags & (PAD_TO_WIDTH | LEFT_JUSTIFY)) == (PAD_TO_WIDTH | LEFT_JUSTIFY)) {\r
-      Buffer = BasePrintLibFillBuffer (Buffer, Width - Precision, ' ', BytesPerOutputCharacter);\r
+      Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, Width - Precision, ' ', BytesPerOutputCharacter);\r
     }\r
 \r
     }\r
 \r
-    //\r
-    // Reduce the number of characters\r
-    //\r
-    BufferSize -= Count;\r
-\r
     //\r
     // Get the next character from the format string\r
     //\r
     //\r
     // Get the next character from the format string\r
     //\r
@@ -561,7 +550,7 @@ BasePrintLibVSPrint (
   //\r
   // Null terminate the Unicode or ASCII string\r
   //\r
   //\r
   // Null terminate the Unicode or ASCII string\r
   //\r
-  BasePrintLibFillBuffer (Buffer, 1, 0, BytesPerOutputCharacter);\r
+  BasePrintLibFillBuffer (Buffer, EndBuffer, 1, 0, BytesPerOutputCharacter);\r
   //\r
   // Make sure output buffer cannot contain more than PcdMaximumUnicodeStringLength\r
   // Unicode characters if PcdMaximumUnicodeStringLength is not zero. \r
   //\r
   // Make sure output buffer cannot contain more than PcdMaximumUnicodeStringLength\r
   // Unicode characters if PcdMaximumUnicodeStringLength is not zero. \r
@@ -999,7 +988,8 @@ AsciiSPrintUnicodeFormat (
                   Unicode string.\r
   @param  Flags   The bitmask of flags that specify left justification, zero pad, and commas.\r
   @param  Value   The 64-bit signed value to convert to a string.\r
                   Unicode string.\r
   @param  Flags   The bitmask of flags that specify left justification, zero pad, and commas.\r
   @param  Value   The 64-bit signed value to convert to a string.\r
-  @param  Width   The maximum number of Unicode characters to place in Buffer.\r
+  @param  Width   The maximum number of Unicode characters to place in Buffer, not including\r
+                  the Null-terminator.\r
   \r
   @return The number of Unicode characters in Buffer not including the Null-terminator.\r
 \r
   \r
   @return The number of Unicode characters in Buffer not including the Null-terminator.\r
 \r
@@ -1046,7 +1036,8 @@ UnicodeValueToString (
                   ASCII string.\r
   @param  Flags   The bitmask of flags that specify left justification, zero pad, and commas.\r
   @param  Value   The 64-bit signed value to convert to a string.\r
                   ASCII string.\r
   @param  Flags   The bitmask of flags that specify left justification, zero pad, and commas.\r
   @param  Value   The 64-bit signed value to convert to a string.\r
-  @param  Width   The maximum number of ASCII characters to place in Buffer.\r
+  @param  Width   The maximum number of ASCII characters to place in Buffer, not including\r
+                  the Null-terminator.\r
   \r
   @return The number of ASCII characters in Buffer not including the Null-terminator.\r
 \r
   \r
   @return The number of ASCII characters in Buffer not including the Null-terminator.\r
 \r
index 8f417fbf11489f4c9c18955b75706e087616c0e5..0a75a3c58114e04614d0ec31c3b136602798d6c1 100644 (file)
@@ -25,6 +25,8 @@ static CONST CHAR8 mHexStr[] = {'0','1','2','3','4','5','6','7','8','9','A','B',
   Internal function that places ASCII or Unicode character into the Buffer.\r
 \r
   @param  Buffer      Buffer to place the Unicode or ASCII string.\r
   Internal function that places ASCII or Unicode character into the Buffer.\r
 \r
   @param  Buffer      Buffer to place the Unicode or ASCII string.\r
+  @param  EndBuffer   The end of the input Buffer. No characters will be\r
+                      placed after that. \r
   @param  Length      Count of character to be placed into Buffer.\r
   @param  Character   Character to be placed into Buffer.\r
   @param  Increment   Character increment in Buffer.\r
   @param  Length      Count of character to be placed into Buffer.\r
   @param  Character   Character to be placed into Buffer.\r
   @param  Increment   Character increment in Buffer.\r
@@ -35,6 +37,7 @@ static CONST CHAR8 mHexStr[] = {'0','1','2','3','4','5','6','7','8','9','A','B',
 CHAR8 *\r
 BasePrintLibFillBuffer (\r
   CHAR8   *Buffer,\r
 CHAR8 *\r
 BasePrintLibFillBuffer (\r
   CHAR8   *Buffer,\r
+  CHAR8   *EndBuffer,\r
   INTN    Length,\r
   UINTN   Character,\r
   INTN    Increment\r
   INTN    Length,\r
   UINTN   Character,\r
   INTN    Increment\r
@@ -42,7 +45,7 @@ BasePrintLibFillBuffer (
 {\r
   INTN  Index;\r
 \r
 {\r
   INTN  Index;\r
 \r
-  for (Index = 0; Index < Length; Index++) {\r
+  for (Index = 0; Index < Length && Buffer < EndBuffer; Index++) {\r
     *Buffer       =  (CHAR8) Character;\r
     *(Buffer + 1) =  (CHAR8) (Character >> 8);\r
     Buffer        += Increment;\r
     *Buffer       =  (CHAR8) Character;\r
     *(Buffer + 1) =  (CHAR8) (Character >> 8);\r
     Buffer        += Increment;\r
@@ -117,7 +120,8 @@ BasePrintLibValueToString (
   @param  Flags     The bitmask of flags that specify left justification, zero pad,\r
                     and commas.\r
   @param  Value     The 64-bit signed value to convert to a string.\r
   @param  Flags     The bitmask of flags that specify left justification, zero pad,\r
                     and commas.\r
   @param  Value     The 64-bit signed value to convert to a string.\r
-  @param  Width      The maximum number of characters to place in Buffer.\r
+  @param  Width     The maximum number of characters to place in Buffer, not including\r
+                    the Null-terminator.\r
   @param  Increment Character increment in Buffer.\r
   \r
   @return The number of characters in Buffer not including the Null-terminator.\r
   @param  Increment Character increment in Buffer.\r
   \r
   @return The number of characters in Buffer not including the Null-terminator.\r
@@ -133,6 +137,7 @@ BasePrintLibConvertValueToString (
   )\r
 {\r
   CHAR8  *OriginalBuffer;\r
   )\r
 {\r
   CHAR8  *OriginalBuffer;\r
+  CHAR8  *EndBuffer;\r
   CHAR8  ValueBuffer[MAXIMUM_VALUE_CHARACTERS];\r
   UINTN  Count;\r
   UINTN  Digits;\r
   CHAR8  ValueBuffer[MAXIMUM_VALUE_CHARACTERS];\r
   UINTN  Count;\r
   UINTN  Digits;\r
@@ -154,17 +159,21 @@ BasePrintLibConvertValueToString (
   if (Width == 0) {\r
     Width = MAXIMUM_VALUE_CHARACTERS - 1;\r
   }\r
   if (Width == 0) {\r
     Width = MAXIMUM_VALUE_CHARACTERS - 1;\r
   }\r
+  //\r
+  // Set the tag for the end of the input Buffer.\r
+  //\r
+  EndBuffer = Buffer + Width * Increment;\r
 \r
   if (Value < 0) {\r
     Value = -Value;\r
 \r
   if (Value < 0) {\r
     Value = -Value;\r
-    Buffer = BasePrintLibFillBuffer (Buffer, 1, '-', Increment);\r
+    Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, 1, '-', Increment);\r
     Width--;\r
   }\r
 \r
   Count = BasePrintLibValueToString (ValueBuffer, Value, 10);\r
 \r
   if ((Flags & PREFIX_ZERO) != 0) {\r
     Width--;\r
   }\r
 \r
   Count = BasePrintLibValueToString (ValueBuffer, Value, 10);\r
 \r
   if ((Flags & PREFIX_ZERO) != 0) {\r
-    Buffer = BasePrintLibFillBuffer (Buffer, Width - Count, '0', Increment);\r
+    Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, Width - Count, '0', Increment);\r
   }\r
 \r
   Digits = Count % 3;\r
   }\r
 \r
   Digits = Count % 3;\r
@@ -172,19 +181,19 @@ BasePrintLibConvertValueToString (
     Digits = 3 - Digits;\r
   }\r
   for (Index = 0; Index < Count; Index++) {\r
     Digits = 3 - Digits;\r
   }\r
   for (Index = 0; Index < Count; Index++) {\r
-    Buffer = BasePrintLibFillBuffer (Buffer, 1, ValueBuffer[Count - Index], Increment);\r
+    Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, 1, ValueBuffer[Count - Index], Increment);\r
     if ((Flags & COMMA_TYPE) != 0) {\r
       Digits++;\r
       if (Digits == 3) {\r
         Digits = 0;\r
         if ((Index + 1) < Count) {\r
     if ((Flags & COMMA_TYPE) != 0) {\r
       Digits++;\r
       if (Digits == 3) {\r
         Digits = 0;\r
         if ((Index + 1) < Count) {\r
-          Buffer = BasePrintLibFillBuffer (Buffer, 1, ',', Increment);\r
+          Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, 1, ',', Increment);\r
         }\r
       }\r
     }\r
   }\r
 \r
         }\r
       }\r
     }\r
   }\r
 \r
-  BasePrintLibFillBuffer (Buffer, 1, 0, Increment);\r
+  BasePrintLibFillBuffer (Buffer, EndBuffer, 1, 0, Increment);\r
 \r
   return ((Buffer - OriginalBuffer) / Increment);\r
 }\r
 \r
   return ((Buffer - OriginalBuffer) / Increment);\r
 }\r
index b7c95a8e4326e511e867653d09a008f01aef1a8e..e0928b8c8035515d647e7b9d5c4bc184476f775e 100644 (file)
@@ -85,6 +85,8 @@ BasePrintLibSPrint (
   Internal function that places ASCII or Unicode character into the Buffer.\r
 \r
   @param  Buffer      Buffer to place the Unicode or ASCII string.\r
   Internal function that places ASCII or Unicode character into the Buffer.\r
 \r
   @param  Buffer      Buffer to place the Unicode or ASCII string.\r
+  @param  EndBuffer   The end of the input Buffer. No characters will be\r
+                      placed after that. \r
   @param  Length      Count of character to be placed into Buffer.\r
   @param  Character   Character to be placed into Buffer.\r
   @param  Increment   Character increment in Buffer.\r
   @param  Length      Count of character to be placed into Buffer.\r
   @param  Character   Character to be placed into Buffer.\r
   @param  Increment   Character increment in Buffer.\r
@@ -95,6 +97,7 @@ BasePrintLibSPrint (
 CHAR8 *\r
 BasePrintLibFillBuffer (\r
   CHAR8   *Buffer,\r
 CHAR8 *\r
 BasePrintLibFillBuffer (\r
   CHAR8   *Buffer,\r
+  CHAR8   *EndBuffer,\r
   INTN    Length,\r
   UINTN   Character,\r
   INTN    Increment\r
   INTN    Length,\r
   UINTN   Character,\r
   INTN    Increment\r
@@ -151,7 +154,8 @@ BasePrintLibValueToString (
   @param  Flags     The bitmask of flags that specify left justification, zero pad,\r
                     and commas.\r
   @param  Value     The 64-bit signed value to convert to a string.\r
   @param  Flags     The bitmask of flags that specify left justification, zero pad,\r
                     and commas.\r
   @param  Value     The 64-bit signed value to convert to a string.\r
-  @param  Width     The maximum number of characters to place in Buffer.\r
+  @param  Width     The maximum number of characters to place in Buffer, not including\r
+                    the Null-terminator.\r
   @param  Increment Character increment in Buffer.\r
   \r
   @return Total number of characters required to perform the conversion.\r
   @param  Increment Character increment in Buffer.\r
   \r
   @return Total number of characters required to perform the conversion.\r