]> git.proxmox.com Git - mirror_edk2.git/commitdiff
MdePkg/BasePrintLib: Avoid reading content beyond the format string
authorHao Wu <hao.a.wu@intel.com>
Mon, 22 May 2017 06:49:11 +0000 (14:49 +0800)
committerHao Wu <hao.a.wu@intel.com>
Thu, 1 Jun 2017 00:46:31 +0000 (08:46 +0800)
https://bugzilla.tianocore.org/show_bug.cgi?id=567

In function BasePrintLibSPrintMarker(), when processing ASCII format
strings, if the format string walker pointer 'Format' is pointing at the
end of the format string (i.e. '\0'), the following expression:
*(Format + 1)
will read an undefined value.

Though this value won't affect the functionality, since it will be masked
by variable 'FormatMask':
(*(Format + 1) << 8)) & FormatMask
(FormatMask is 0xff for ASCII format string)

This commit adds additional logic to avoid reading undefined content.

Contributed-under: TianoCore Contribution Agreement 1.0
Signed-off-by: Hao Wu <hao.a.wu@intel.com>
Reviewed-by: Liming Gao <liming.gao@intel.com>
MdePkg/Library/BasePrintLib/PrintLibInternal.c

index 9b15a07ac066378750bde3af31b0b6872611c824..cec5b3bc994b20101efb9b68d20759100e940227 100644 (file)
@@ -653,7 +653,7 @@ BasePrintLibSPrintMarker (
   //\r
   // Get the first character from the format string\r
   //\r
   //\r
   // Get the first character from the format string\r
   //\r
-  FormatCharacter = ((*Format & 0xff) | (*(Format + 1) << 8)) & FormatMask;\r
+  FormatCharacter = ((*Format & 0xff) | ((BytesPerFormatCharacter == 1) ? 0 : (*(Format + 1) << 8))) & FormatMask;\r
 \r
   //\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
@@ -685,7 +685,7 @@ BasePrintLibSPrintMarker (
       //\r
       for (Done = FALSE; !Done; ) {\r
         Format += BytesPerFormatCharacter;\r
       //\r
       for (Done = FALSE; !Done; ) {\r
         Format += BytesPerFormatCharacter;\r
-        FormatCharacter = ((*Format & 0xff) | (*(Format + 1) << 8)) & FormatMask;\r
+        FormatCharacter = ((*Format & 0xff) | ((BytesPerFormatCharacter == 1) ? 0 : (*(Format + 1) << 8))) & FormatMask;\r
         switch (FormatCharacter) {\r
         case '.': \r
           Flags |= PRECISION; \r
         switch (FormatCharacter) {\r
         case '.': \r
           Flags |= PRECISION; \r
@@ -738,7 +738,7 @@ BasePrintLibSPrintMarker (
           for (Count = 0; ((FormatCharacter >= '0') &&  (FormatCharacter <= '9')); ){\r
             Count = (Count * 10) + FormatCharacter - '0';\r
             Format += BytesPerFormatCharacter;\r
           for (Count = 0; ((FormatCharacter >= '0') &&  (FormatCharacter <= '9')); ){\r
             Count = (Count * 10) + FormatCharacter - '0';\r
             Format += BytesPerFormatCharacter;\r
-            FormatCharacter = ((*Format & 0xff) | (*(Format + 1) << 8)) & FormatMask;\r
+            FormatCharacter = ((*Format & 0xff) | ((BytesPerFormatCharacter == 1) ? 0 : (*(Format + 1) << 8))) & FormatMask;\r
           }\r
           Format -= BytesPerFormatCharacter;\r
           if ((Flags & PRECISION) == 0) {\r
           }\r
           Format -= BytesPerFormatCharacter;\r
           if ((Flags & PRECISION) == 0) {\r
@@ -1017,7 +1017,7 @@ BasePrintLibSPrintMarker (
 \r
       case '\r':\r
         Format += BytesPerFormatCharacter;\r
 \r
       case '\r':\r
         Format += BytesPerFormatCharacter;\r
-        FormatCharacter = ((*Format & 0xff) | (*(Format + 1) << 8)) & FormatMask;\r
+        FormatCharacter = ((*Format & 0xff) | ((BytesPerFormatCharacter == 1) ? 0 : (*(Format + 1) << 8))) & FormatMask;\r
         if (FormatCharacter == '\n') {\r
           //\r
           // Translate '\r\n' to '\r\n'\r
         if (FormatCharacter == '\n') {\r
           //\r
           // Translate '\r\n' to '\r\n'\r
@@ -1038,7 +1038,7 @@ BasePrintLibSPrintMarker (
         //\r
         ArgumentString = "\r\n";\r
         Format += BytesPerFormatCharacter;\r
         //\r
         ArgumentString = "\r\n";\r
         Format += BytesPerFormatCharacter;\r
-        FormatCharacter = ((*Format & 0xff) | (*(Format + 1) << 8)) & FormatMask;\r
+        FormatCharacter = ((*Format & 0xff) | ((BytesPerFormatCharacter == 1) ? 0 : (*(Format + 1) << 8))) & FormatMask;\r
         if (FormatCharacter != '\r') {\r
           Format   -= BytesPerFormatCharacter;\r
         }\r
         if (FormatCharacter != '\r') {\r
           Format   -= BytesPerFormatCharacter;\r
         }\r
@@ -1057,7 +1057,7 @@ BasePrintLibSPrintMarker (
  \r
     case '\r':\r
       Format += BytesPerFormatCharacter;\r
  \r
     case '\r':\r
       Format += BytesPerFormatCharacter;\r
-      FormatCharacter = ((*Format & 0xff) | (*(Format + 1) << 8)) & FormatMask;\r
+      FormatCharacter = ((*Format & 0xff) | ((BytesPerFormatCharacter == 1) ? 0 : (*(Format + 1) << 8))) & FormatMask;\r
       if (FormatCharacter == '\n') {\r
         //\r
         // Translate '\r\n' to '\r\n'\r
       if (FormatCharacter == '\n') {\r
         //\r
         // Translate '\r\n' to '\r\n'\r
@@ -1078,7 +1078,7 @@ BasePrintLibSPrintMarker (
       //\r
       ArgumentString = "\r\n";\r
       Format += BytesPerFormatCharacter;\r
       //\r
       ArgumentString = "\r\n";\r
       Format += BytesPerFormatCharacter;\r
-      FormatCharacter = ((*Format & 0xff) | (*(Format + 1) << 8)) & FormatMask;\r
+      FormatCharacter = ((*Format & 0xff) | ((BytesPerFormatCharacter == 1) ? 0 : (*(Format + 1) << 8))) & FormatMask;\r
       if (FormatCharacter != '\r') {\r
         Format   -= BytesPerFormatCharacter;\r
       }\r
       if (FormatCharacter != '\r') {\r
         Format   -= BytesPerFormatCharacter;\r
       }\r
@@ -1206,7 +1206,7 @@ BasePrintLibSPrintMarker (
     //\r
     // Get the next character from the format string\r
     //\r
     //\r
     // Get the next character from the format string\r
     //\r
-    FormatCharacter = ((*Format & 0xff) | (*(Format + 1) << 8)) & FormatMask;\r
+    FormatCharacter = ((*Format & 0xff) | ((BytesPerFormatCharacter == 1) ? 0 : (*(Format + 1) << 8))) & FormatMask;\r
   }\r
 \r
   if ((Flags & COUNT_ONLY_NO_PRINT) != 0) {\r
   }\r
 \r
   if ((Flags & COUNT_ONLY_NO_PRINT) != 0) {\r