]> git.proxmox.com Git - mirror_edk2.git/commitdiff
MdePkg/BasePrintLib: Fix error in Precision position calculation
authorJian J Wang <jian.j.wang@intel.com>
Mon, 25 Dec 2017 02:07:39 +0000 (10:07 +0800)
committerLiming Gao <liming.gao@intel.com>
Thu, 28 Dec 2017 03:12:07 +0000 (11:12 +0800)
Due to a potential hole in the stop condition of loop, the two continuous
access to ArgumentString (index, index+1) inside the loop might cause the
string ending character ('\0') and the byte after it to be read.

Cc: Michael D Kinney <michael.d.kinney@intel.com>
Cc: Liming Gao <liming.gao@intel.com>
Cc: Jiewen Yao <jiewen.yao@intel.com>
Cc: Star Zeng <star.zeng@intel.com>
Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Jian J Wang <jian.j.wang@intel.com>
Reviewed-by: Liming Gao <liming.gao@intel.com>
MdePkg/Library/BasePrintLib/PrintLibInternal.c

index 28d946472f2a2ef087e9555bc27a3273eac7ce28..fc57255068485222fce76ea5b94b6c80d2556943 100644 (file)
@@ -1107,7 +1107,10 @@ BasePrintLibSPrintMarker (
       // Compute the number of characters in ArgumentString and store it in Count\r
       // ArgumentString is either null-terminated, or it contains Precision characters\r
       //\r
       // Compute the number of characters in ArgumentString and store it in Count\r
       // ArgumentString is either null-terminated, or it contains Precision characters\r
       //\r
-      for (Count = 0; Count < Precision || ((Flags & PRECISION) == 0); Count++) {\r
+      for (Count = 0;\r
+            ArgumentString[Count * BytesPerArgumentCharacter] != '\0' &&\r
+            (Count < Precision || ((Flags & PRECISION) == 0));\r
+              Count++) {\r
         ArgumentCharacter = ((ArgumentString[Count * BytesPerArgumentCharacter] & 0xff) | ((ArgumentString[Count * BytesPerArgumentCharacter + 1]) << 8)) & ArgumentMask;\r
         if (ArgumentCharacter == 0) {\r
           break;\r
         ArgumentCharacter = ((ArgumentString[Count * BytesPerArgumentCharacter] & 0xff) | ((ArgumentString[Count * BytesPerArgumentCharacter + 1]) << 8)) & ArgumentMask;\r
         if (ArgumentCharacter == 0) {\r
           break;\r
@@ -1164,7 +1167,7 @@ BasePrintLibSPrintMarker (
     //\r
     // Copy the string into the output buffer performing the required type conversions\r
     //\r
     //\r
     // Copy the string into the output buffer performing the required type conversions\r
     //\r
-    while (Index < Count) {\r
+    while (Index < Count && (*ArgumentString) != '\0') {\r
       ArgumentCharacter = ((*ArgumentString & 0xff) | (((UINT8)*(ArgumentString + 1)) << 8)) & ArgumentMask;\r
 \r
       LengthToReturn += (1 * BytesPerOutputCharacter);\r
       ArgumentCharacter = ((*ArgumentString & 0xff) | (((UINT8)*(ArgumentString + 1)) << 8)) & ArgumentMask;\r
 \r
       LengthToReturn += (1 * BytesPerOutputCharacter);\r