]> git.proxmox.com Git - mirror_edk2.git/commitdiff
MdeModulePkg/PrintLib: Refine the SPrint functions
authorHao Wu <hao.a.wu@intel.com>
Mon, 19 Dec 2016 07:01:22 +0000 (15:01 +0800)
committerHao Wu <hao.a.wu@intel.com>
Tue, 21 Feb 2017 05:55:20 +0000 (13:55 +0800)
For the following 12 APIs in MdeModulePkg/DxePrintLibPrint2Protocol:
UnicodeVSPrint
UnicodeBSPrint
UnicodeSPrint
UnicodeVSPrintAsciiFormat
UnicodeBSPrintAsciiFormat
UnicodeSPrintAsciiFormat
AsciiVSPrint
AsciiBSPrint
AsciiSPrint
AsciiVSPrintUnicodeFormat
AsciiBSPrintUnicodeFormat
AsciiSPrintUnicodeFormat

They will ASSERT when:
1) The input parameter 'StartOfBuffer' is NULL if 'BufferSize' indicates
at least 1 Ascii/Unicode character can be held.
2) The input parameter 'FormatString' is NULL if 'BufferSize' indicates at
least 1 Ascii/Unicode character can be held.
3) The input parameter 'FormatString' contains more than
PcdMaximum[Ascii|Unicode]StringLength Ascii/Unicode characters.
4) The produced string contains more than
PcdMaximum[Ascii|Unicode]StringLength Ascii/Unicode characters.

This commits removes the ASSERT case 4) and add the following new ASSERT
case:
4) The input parameter 'BufferSize' is greater than
(PcdMaximumAsciiStringLength * sizeof (CHAR8)) for Ascii format string or
(PcdMaximumUnicodeStringLength * sizeof (CHAR16) + 1) for Unicode format
string.

And for those ASSERT cases, 0 will be returned by those 12 APIs.

For the following 2 APIs in MdeModulePkg/DxePrintLibPrint2Protocol:
SPrintLength
SPrintLengthAsciiFormat

They will ASSERT when:
1) The input parameter 'FormatString' is NULL.
2) The input parameter 'FormatString' contains more than
PcdMaximum[Ascii|Unicode]StringLength Ascii/Unicode characters.

And for those ASSERT cases, 0 will be returned by those 2 APIs.

Now these APIs in the MdeModulePkg/DxePrintLibPrint2Protocol instance
follow the same rules with MdePkg/BasePrintLib.

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>
MdeModulePkg/Library/DxePrintLibPrint2Protocol/DxePrintLibPrint2Protocol.inf
MdeModulePkg/Library/DxePrintLibPrint2Protocol/PrintLib.c

index 3d09b4be4d410b80e9dd1b5fe3125354c5ebcff2..55ee940a82aaf5d3b80d371edb160c97c36300bc 100644 (file)
@@ -1,7 +1,7 @@
 ## @file\r
 #  Library instance that implements Print Library class based on protocol gEfiPrint2ProtocolGuid.\r
 #\r
-#  Copyright (c) 2009 - 2014, Intel Corporation. All rights reserved.<BR>\r
+#  Copyright (c) 2009 - 2017, Intel Corporation. All rights reserved.<BR>\r
 #\r
 #  This program and the accompanying materials\r
 #  are licensed and made available under the terms and conditions of the BSD License\r
 [LibraryClasses]\r
   BaseLib\r
   DebugLib\r
+  PcdLib\r
 \r
 [Protocols]\r
   gEfiPrint2ProtocolGuid                         ## CONSUMES\r
 \r
+[Pcd]\r
+  gEfiMdePkgTokenSpaceGuid.PcdMaximumAsciiStringLength     ## SOMETIMES_CONSUMES\r
+  gEfiMdePkgTokenSpaceGuid.PcdMaximumUnicodeStringLength   ## SOMETIMES_CONSUMES\r
+\r
 [Depex.common.DXE_DRIVER, Depex.common.DXE_RUNTIME_DRIVER, Depex.common.DXE_SAL_DRIVER, Depex.common.DXE_SMM_DRIVER]\r
   gEfiPrint2ProtocolGuid\r
index 01378687fd58e7303c0c42bfc946eace70ed6cd9..438ac9e84706dcce9a81dc313b776c7596254a8f 100644 (file)
@@ -25,6 +25,23 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
 \r
 #include <Library/BaseLib.h>\r
 #include <Library/DebugLib.h>\r
+#include <Library/PcdLib.h>\r
+\r
+#define ASSERT_UNICODE_BUFFER(Buffer) ASSERT ((((UINTN) (Buffer)) & 0x01) == 0)\r
+\r
+//\r
+// Safe print checks\r
+//\r
+#define RSIZE_MAX             (PcdGet32 (PcdMaximumUnicodeStringLength))\r
+#define ASCII_RSIZE_MAX       (PcdGet32 (PcdMaximumAsciiStringLength))\r
+\r
+#define SAFE_PRINT_CONSTRAINT_CHECK(Expression, RetVal)  \\r
+  do { \\r
+    ASSERT (Expression); \\r
+    if (!(Expression)) { \\r
+      return RetVal; \\r
+    } \\r
+  } while (FALSE)\r
 \r
 EFI_PRINT2_PROTOCOL  *mPrint2Protocol = NULL;\r
 \r
@@ -91,17 +108,21 @@ DxePrintLibPrint2ProtocolVaListToBaseList (
   BOOLEAN    Long;\r
   BOOLEAN    Done;\r
 \r
-  ASSERT (Format         != NULL);\r
   ASSERT (BaseListMarker != NULL);\r
+  SAFE_PRINT_CONSTRAINT_CHECK ((Format != NULL), FALSE);\r
 \r
   BaseListStart = BaseListMarker;\r
 \r
   if (AsciiFormat) {\r
-    ASSERT (AsciiStrSize (Format) != 0);\r
+    if (ASCII_RSIZE_MAX != 0) {\r
+      SAFE_PRINT_CONSTRAINT_CHECK ((AsciiStrnLenS (Format, ASCII_RSIZE_MAX + 1) <= ASCII_RSIZE_MAX), FALSE);\r
+    }\r
     BytesPerFormatCharacter = 1;\r
     FormatMask = 0xff;\r
   } else {\r
-    ASSERT (StrSize ((CHAR16 *) Format) != 0);\r
+    if (RSIZE_MAX != 0) {\r
+      SAFE_PRINT_CONSTRAINT_CHECK ((StrnLenS ((CHAR16 *)Format, RSIZE_MAX + 1) <= RSIZE_MAX), FALSE);\r
+    }\r
     BytesPerFormatCharacter = 2;\r
     FormatMask = 0xffff;\r
   }\r
@@ -224,35 +245,41 @@ DxePrintLibPrint2ProtocolVaListToBaseList (
 }\r
 \r
 /**\r
-  Produces a Null-terminated Unicode string in an output buffer based on \r
-  a Null-terminated Unicode format string and a VA_LIST argument list\r
-  \r
+  Produces a Null-terminated Unicode string in an output buffer based on\r
+  a Null-terminated Unicode format string and a VA_LIST argument list.\r
+\r
+  This function is similar as vsnprintf_s defined in C11.\r
+\r
   Produces a Null-terminated Unicode string in the output buffer specified by StartOfBuffer\r
-  and BufferSize.  \r
-  The Unicode string is produced by parsing the format string specified by FormatString.  \r
-  Arguments are pulled from the variable argument list specified by Marker based on the \r
-  contents of the format string.  \r
+  and BufferSize.\r
+  The Unicode string is produced by parsing the format string specified by FormatString.\r
+  Arguments are pulled from the variable argument list specified by Marker based on the\r
+  contents of the format string.\r
   The number of Unicode characters in the produced output buffer is returned not including\r
   the Null-terminator.\r
-  If BufferSize is 0 or 1, then no output buffer is produced and 0 is returned.\r
 \r
-  If BufferSize > 1 and StartOfBuffer is NULL, then ASSERT().\r
-  If BufferSize > 1 and StartOfBuffer is not aligned on a 16-bit boundary, then ASSERT().\r
-  If BufferSize > 1 and FormatString is NULL, then ASSERT().\r
-  If BufferSize > 1 and FormatString is not aligned on a 16-bit boundary, then ASSERT().\r
-  If PcdMaximumUnicodeStringLength is not zero, and FormatString contains more than \r
+  If StartOfBuffer is not aligned on a 16-bit boundary, then ASSERT().\r
+  If FormatString is not aligned on a 16-bit boundary, then ASSERT().\r
+\r
+  If BufferSize > 1 and StartOfBuffer is NULL, then ASSERT(). Also, the output buffer is\r
+  unmodified and 0 is returned.\r
+  If BufferSize > 1 and FormatString is NULL, then ASSERT(). Also, the output buffer is\r
+  unmodified and 0 is returned.\r
+  If PcdMaximumUnicodeStringLength is not zero, and BufferSize >\r
+  (PcdMaximumUnicodeStringLength * sizeof (CHAR16) + 1), then ASSERT(). Also, the output\r
+  buffer is unmodified and 0 is returned.\r
+  If PcdMaximumUnicodeStringLength is not zero, and FormatString contains more than\r
   PcdMaximumUnicodeStringLength Unicode characters not including the Null-terminator, then\r
-  ASSERT().\r
-  If PcdMaximumUnicodeStringLength is not zero, and produced Null-terminated Unicode string\r
-  contains more than PcdMaximumUnicodeStringLength Unicode characters not including the\r
-  Null-terminator, then ASSERT().\r
+  ASSERT(). Also, the output buffer is unmodified and 0 is returned.\r
+\r
+  If BufferSize is 0 or 1, then the output buffer is unmodified and 0 is returned.\r
 \r
-  @param  StartOfBuffer   A pointer to the output buffer for the produced Null-terminated \r
+  @param  StartOfBuffer   A pointer to the output buffer for the produced Null-terminated\r
                           Unicode string.\r
   @param  BufferSize      The size, in bytes, of the output buffer specified by StartOfBuffer.\r
-  @param  FormatString    Null-terminated Unicode format string.\r
+  @param  FormatString    Null-terminated Unicode format string.\r
   @param  Marker          VA_LIST marker for the variable argument list.\r
-  \r
+\r
   @return The number of Unicode characters in the produced output buffer not including the\r
           Null-terminator.\r
 \r
@@ -269,6 +296,9 @@ UnicodeVSPrint (
   UINT64   BaseListMarker[256 / sizeof (UINT64)];\r
   BOOLEAN  Converted;\r
 \r
+  ASSERT_UNICODE_BUFFER (StartOfBuffer);\r
+  ASSERT_UNICODE_BUFFER (FormatString);\r
+\r
   Converted = DxePrintLibPrint2ProtocolVaListToBaseList (\r
                 FALSE,\r
                 (CHAR8 *)FormatString,\r
@@ -284,35 +314,39 @@ UnicodeVSPrint (
 }\r
 \r
 /**\r
-  Produces a Null-terminated Unicode string in an output buffer based on \r
-  a Null-terminated Unicode format string and a BASE_LIST argument list\r
-  \r
+  Produces a Null-terminated Unicode string in an output buffer based on\r
+  a Null-terminated Unicode format string and a BASE_LIST argument list.\r
+\r
   Produces a Null-terminated Unicode string in the output buffer specified by StartOfBuffer\r
-  and BufferSize.  \r
-  The Unicode string is produced by parsing the format string specified by FormatString.  \r
-  Arguments are pulled from the variable argument list specified by Marker based on the \r
-  contents of the format string.  \r
+  and BufferSize.\r
+  The Unicode string is produced by parsing the format string specified by FormatString.\r
+  Arguments are pulled from the variable argument list specified by Marker based on the\r
+  contents of the format string.\r
   The number of Unicode characters in the produced output buffer is returned not including\r
   the Null-terminator.\r
-  If BufferSize is 0 or 1, then no output buffer is produced and 0 is returned.\r
 \r
-  If BufferSize > 1 and StartOfBuffer is NULL, then ASSERT().\r
-  If BufferSize > 1 and StartOfBuffer is not aligned on a 16-bit boundary, then ASSERT().\r
-  If BufferSize > 1 and FormatString is NULL, then ASSERT().\r
-  If BufferSize > 1 and FormatString is not aligned on a 16-bit boundary, then ASSERT().\r
-  If PcdMaximumUnicodeStringLength is not zero, and FormatString contains more than \r
+  If StartOfBuffer is not aligned on a 16-bit boundary, then ASSERT().\r
+  If FormatString is not aligned on a 16-bit boundary, then ASSERT().\r
+\r
+  If BufferSize > 1 and StartOfBuffer is NULL, then ASSERT(). Also, the output buffer is\r
+  unmodified and 0 is returned.\r
+  If BufferSize > 1 and FormatString is NULL, then ASSERT(). Also, the output buffer is\r
+  unmodified and 0 is returned.\r
+  If PcdMaximumUnicodeStringLength is not zero, and BufferSize >\r
+  (PcdMaximumUnicodeStringLength * sizeof (CHAR16) + 1), then ASSERT(). Also, the output\r
+  buffer is unmodified and 0 is returned.\r
+  If PcdMaximumUnicodeStringLength is not zero, and FormatString contains more than\r
   PcdMaximumUnicodeStringLength Unicode characters not including the Null-terminator, then\r
-  ASSERT().\r
-  If PcdMaximumUnicodeStringLength is not zero, and produced Null-terminated Unicode string\r
-  contains more than PcdMaximumUnicodeStringLength Unicode characters not including the\r
-  Null-terminator, then ASSERT().\r
+  ASSERT(). Also, the output buffer is unmodified and 0 is returned.\r
+\r
+  If BufferSize is 0 or 1, then the output buffer is unmodified and 0 is returned.\r
 \r
-  @param  StartOfBuffer   A pointer to the output buffer for the produced Null-terminated \r
+  @param  StartOfBuffer   A pointer to the output buffer for the produced Null-terminated\r
                           Unicode string.\r
   @param  BufferSize      The size, in bytes, of the output buffer specified by StartOfBuffer.\r
-  @param  FormatString    Null-terminated Unicode format string.\r
+  @param  FormatString    Null-terminated Unicode format string.\r
   @param  Marker          BASE_LIST marker for the variable argument list.\r
-  \r
+\r
   @return The number of Unicode characters in the produced output buffer not including the\r
           Null-terminator.\r
 \r
@@ -326,37 +360,45 @@ UnicodeBSPrint (
   IN  BASE_LIST     Marker\r
   )\r
 {\r
+  ASSERT_UNICODE_BUFFER (StartOfBuffer);\r
+  ASSERT_UNICODE_BUFFER (FormatString);\r
   return mPrint2Protocol->UnicodeBSPrint (StartOfBuffer, BufferSize, FormatString, Marker);\r
 }\r
 \r
 /**\r
-  Produces a Null-terminated Unicode string in an output buffer based on a Null-terminated \r
+  Produces a Null-terminated Unicode string in an output buffer based on a Null-terminated\r
   Unicode format string and variable argument list.\r
-  \r
+\r
+  This function is similar as snprintf_s defined in C11.\r
+\r
   Produces a Null-terminated Unicode string in the output buffer specified by StartOfBuffer\r
   and BufferSize.\r
   The Unicode string is produced by parsing the format string specified by FormatString.\r
   Arguments are pulled from the variable argument list based on the contents of the format string.\r
   The number of Unicode characters in the produced output buffer is returned not including\r
   the Null-terminator.\r
-  If BufferSize is 0 or 1, then no output buffer is produced and 0 is returned.\r
 \r
-  If BufferSize > 1 and StartOfBuffer is NULL, then ASSERT().\r
-  If BufferSize > 1 and StartOfBuffer is not aligned on a 16-bit boundary, then ASSERT().\r
-  If BufferSize > 1 and FormatString is NULL, then ASSERT().\r
-  If BufferSize > 1 and FormatString is not aligned on a 16-bit boundary, then ASSERT().\r
-  If PcdMaximumUnicodeStringLength is not zero, and FormatString contains more than \r
+  If StartOfBuffer is not aligned on a 16-bit boundary, then ASSERT().\r
+  If FormatString is not aligned on a 16-bit boundary, then ASSERT().\r
+\r
+  If BufferSize > 1 and StartOfBuffer is NULL, then ASSERT(). Also, the output buffer is\r
+  unmodified and 0 is returned.\r
+  If BufferSize > 1 and FormatString is NULL, then ASSERT(). Also, the output buffer is\r
+  unmodified and 0 is returned.\r
+  If PcdMaximumUnicodeStringLength is not zero, and BufferSize >\r
+  (PcdMaximumUnicodeStringLength * sizeof (CHAR16) + 1), then ASSERT(). Also, the output\r
+  buffer is unmodified and 0 is returned.\r
+  If PcdMaximumUnicodeStringLength is not zero, and FormatString contains more than\r
   PcdMaximumUnicodeStringLength Unicode characters not including the Null-terminator, then\r
-  ASSERT().\r
-  If PcdMaximumUnicodeStringLength is not zero, and produced Null-terminated Unicode string\r
-  contains more than PcdMaximumUnicodeStringLength Unicode characters not including the\r
-  Null-terminator, then ASSERT().\r
+  ASSERT(). Also, the output buffer is unmodified and 0 is returned.\r
+\r
+  If BufferSize is 0 or 1, then the output buffer is unmodified and 0 is returned.\r
 \r
-  @param  StartOfBuffer   A pointer to the output buffer for the produced Null-terminated \r
+  @param  StartOfBuffer   A pointer to the output buffer for the produced Null-terminated\r
                           Unicode string.\r
   @param  BufferSize      The size, in bytes, of the output buffer specified by StartOfBuffer.\r
-  @param  FormatString    Null-terminated Unicode format string.\r
-  @param  ...             Variable argument list whose contents are accessed based on the \r
+  @param  FormatString    Null-terminated Unicode format string.\r
+  @param  ...             Variable argument list whose contents are accessed based on the\r
                           format string specified by FormatString.\r
 \r
   @return The number of Unicode characters in the produced output buffer not including the\r
@@ -383,33 +425,39 @@ UnicodeSPrint (
 \r
 /**\r
   Produces a Null-terminated Unicode string in an output buffer based on a Null-terminated\r
-  ASCII format string and a VA_LIST argument list\r
-  \r
+  ASCII format string and a VA_LIST argument list.\r
+\r
+  This function is similar as vsnprintf_s defined in C11.\r
+\r
   Produces a Null-terminated Unicode string in the output buffer specified by StartOfBuffer\r
   and BufferSize.\r
   The Unicode string is produced by parsing the format string specified by FormatString.\r
-  Arguments are pulled from the variable argument list specified by Marker based on the \r
+  Arguments are pulled from the variable argument list specified by Marker based on the\r
   contents of the format string.\r
   The number of Unicode characters in the produced output buffer is returned not including\r
   the Null-terminator.\r
-  If BufferSize is 0 or 1, then no output buffer is produced and 0 is returned.\r
 \r
-  If BufferSize > 1 and StartOfBuffer is NULL, then ASSERT().\r
-  If BufferSize > 1 and StartOfBuffer is not aligned on a 16-bit boundary, then ASSERT().\r
-  If BufferSize > 1 and FormatString is NULL, then ASSERT().\r
+  If StartOfBuffer is not aligned on a 16-bit boundary, then ASSERT().\r
+\r
+  If BufferSize > 1 and StartOfBuffer is NULL, then ASSERT(). Also, the output buffer is\r
+  unmodified and 0 is returned.\r
+  If BufferSize > 1 and FormatString is NULL, then ASSERT(). Also, the output buffer is\r
+  unmodified and 0 is returned.\r
+  If PcdMaximumUnicodeStringLength is not zero, and BufferSize >\r
+  (PcdMaximumUnicodeStringLength * sizeof (CHAR16) + 1), then ASSERT(). Also, the output\r
+  buffer is unmodified and 0 is returned.\r
   If PcdMaximumAsciiStringLength is not zero, and FormatString contains more than\r
-  PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator, then\r
-  ASSERT().\r
-  If PcdMaximumUnicodeStringLength is not zero, and produced Null-terminated Unicode string\r
-  contains more than PcdMaximumUnicodeStringLength Unicode characters not including the\r
-  Null-terminator, then ASSERT().\r
+  PcdMaximumAsciiStringLength Ascii characters not including the Null-terminator, then\r
+  ASSERT(). Also, the output buffer is unmodified and 0 is returned.\r
 \r
-  @param  StartOfBuffer   A pointer to the output buffer for the produced Null-terminated \r
+  If BufferSize is 0 or 1, then no output buffer is produced and 0 is returned.\r
+\r
+  @param  StartOfBuffer   A pointer to the output buffer for the produced Null-terminated\r
                           Unicode string.\r
   @param  BufferSize      The size, in bytes, of the output buffer specified by StartOfBuffer.\r
-  @param  FormatString    Null-terminated Unicode format string.\r
+  @param  FormatString    A Null-terminated ASCII format string.\r
   @param  Marker          VA_LIST marker for the variable argument list.\r
-  \r
+\r
   @return The number of Unicode characters in the produced output buffer not including the\r
           Null-terminator.\r
 \r
@@ -426,6 +474,8 @@ UnicodeVSPrintAsciiFormat (
   UINT64   BaseListMarker[256 / sizeof (UINT64)];\r
   BOOLEAN  Converted;\r
 \r
+  ASSERT_UNICODE_BUFFER (StartOfBuffer);\r
+\r
   Converted = DxePrintLibPrint2ProtocolVaListToBaseList (\r
                 TRUE,\r
                 FormatString,\r
@@ -442,33 +492,37 @@ UnicodeVSPrintAsciiFormat (
 \r
 /**\r
   Produces a Null-terminated Unicode string in an output buffer based on a Null-terminated\r
-  ASCII format string and a BASE_LIST argument list\r
-  \r
+  ASCII format string and a BASE_LIST argument list.\r
+\r
   Produces a Null-terminated Unicode string in the output buffer specified by StartOfBuffer\r
   and BufferSize.\r
   The Unicode string is produced by parsing the format string specified by FormatString.\r
-  Arguments are pulled from the variable argument list specified by Marker based on the \r
+  Arguments are pulled from the variable argument list specified by Marker based on the\r
   contents of the format string.\r
   The number of Unicode characters in the produced output buffer is returned not including\r
   the Null-terminator.\r
-  If BufferSize is 0 or 1, then no output buffer is produced and 0 is returned.\r
 \r
-  If BufferSize > 1 and StartOfBuffer is NULL, then ASSERT().\r
-  If BufferSize > 1 and StartOfBuffer is not aligned on a 16-bit boundary, then ASSERT().\r
-  If BufferSize > 1 and FormatString is NULL, then ASSERT().\r
+  If StartOfBuffer is not aligned on a 16-bit boundary, then ASSERT().\r
+\r
+  If BufferSize > 1 and StartOfBuffer is NULL, then ASSERT(). Also, the output buffer is\r
+  unmodified and 0 is returned.\r
+  If BufferSize > 1 and FormatString is NULL, then ASSERT(). Also, the output buffer is\r
+  unmodified and 0 is returned.\r
+  If PcdMaximumUnicodeStringLength is not zero, and BufferSize >\r
+  (PcdMaximumUnicodeStringLength * sizeof (CHAR16) + 1), then ASSERT(). Also, the output\r
+  buffer is unmodified and 0 is returned.\r
   If PcdMaximumAsciiStringLength is not zero, and FormatString contains more than\r
-  PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator, then\r
-  ASSERT().\r
-  If PcdMaximumUnicodeStringLength is not zero, and produced Null-terminated Unicode string\r
-  contains more than PcdMaximumUnicodeStringLength Unicode characters not including the\r
-  Null-terminator, then ASSERT().\r
+  PcdMaximumAsciiStringLength Ascii characters not including the Null-terminator, then\r
+  ASSERT(). Also, the output buffer is unmodified and 0 is returned.\r
+\r
+  If BufferSize is 0 or 1, then no output buffer is produced and 0 is returned.\r
 \r
-  @param  StartOfBuffer   A pointer to the output buffer for the produced Null-terminated \r
+  @param  StartOfBuffer   A pointer to the output buffer for the produced Null-terminated\r
                           Unicode string.\r
   @param  BufferSize      The size, in bytes, of the output buffer specified by StartOfBuffer.\r
-  @param  FormatString    Null-terminated Unicode format string.\r
+  @param  FormatString    A Null-terminated ASCII format string.\r
   @param  Marker          BASE_LIST marker for the variable argument list.\r
-  \r
+\r
   @return The number of Unicode characters in the produced output buffer not including the\r
           Null-terminator.\r
 \r
@@ -482,39 +536,46 @@ UnicodeBSPrintAsciiFormat (
   IN  BASE_LIST    Marker\r
   )\r
 {\r
+  ASSERT_UNICODE_BUFFER (StartOfBuffer);\r
   return mPrint2Protocol->UnicodeBSPrintAsciiFormat (StartOfBuffer, BufferSize, FormatString, Marker);\r
 }\r
 \r
 /**\r
-  Produces a Null-terminated Unicode string in an output buffer based on a Null-terminated \r
+  Produces a Null-terminated Unicode string in an output buffer based on a Null-terminated\r
   ASCII format string and  variable argument list.\r
-  \r
+\r
+  This function is similar as snprintf_s defined in C11.\r
+\r
   Produces a Null-terminated Unicode string in the output buffer specified by StartOfBuffer\r
   and BufferSize.\r
   The Unicode string is produced by parsing the format string specified by FormatString.\r
-  Arguments are pulled from the variable argument list based on the contents of the \r
+  Arguments are pulled from the variable argument list based on the contents of the\r
   format string.\r
   The number of Unicode characters in the produced output buffer is returned not including\r
   the Null-terminator.\r
-  If BufferSize is 0 or 1, then no output buffer is produced and 0 is returned.\r
 \r
-  If BufferSize > 1 and StartOfBuffer is NULL, then ASSERT().\r
-  If BufferSize > 1 and StartOfBuffer is not aligned on a 16-bit boundary, then ASSERT().\r
-  If BufferSize > 1 and FormatString is NULL, then ASSERT().\r
+  If StartOfBuffer is not aligned on a 16-bit boundary, then ASSERT().\r
+\r
+  If BufferSize > 1 and StartOfBuffer is NULL, then ASSERT(). Also, the output buffer is\r
+  unmodified and 0 is returned.\r
+  If BufferSize > 1 and FormatString is NULL, then ASSERT(). Also, the output buffer is\r
+  unmodified and 0 is returned.\r
+  If PcdMaximumUnicodeStringLength is not zero, and BufferSize >\r
+  (PcdMaximumUnicodeStringLength * sizeof (CHAR16) + 1), then ASSERT(). Also, the output\r
+  buffer is unmodified and 0 is returned.\r
   If PcdMaximumAsciiStringLength is not zero, and FormatString contains more than\r
-  PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator, then\r
-  ASSERT().\r
-  If PcdMaximumUnicodeStringLength is not zero, and produced Null-terminated Unicode string\r
-  contains more than PcdMaximumUnicodeStringLength Unicode characters not including the\r
-  Null-terminator, then ASSERT().\r
+  PcdMaximumAsciiStringLength Ascii characters not including the Null-terminator, then\r
+  ASSERT(). Also, the output buffer is unmodified and 0 is returned.\r
+\r
+  If BufferSize is 0 or 1, then no output buffer is produced and 0 is returned.\r
 \r
-  @param  StartOfBuffer   A pointer to the output buffer for the produced Null-terminated \r
+  @param  StartOfBuffer   A pointer to the output buffer for the produced Null-terminated\r
                           Unicode string.\r
   @param  BufferSize      The size, in bytes, of the output buffer specified by StartOfBuffer.\r
-  @param  FormatString    Null-terminated Unicode format string.\r
-  @param  ...             Variable argument list whose contents are accessed based on the \r
+  @param  FormatString    A Null-terminated ASCII format string.\r
+  @param  ...             Variable argument list whose contents are accessed based on the\r
                           format string specified by FormatString.\r
-  \r
+\r
   @return The number of Unicode characters in the produced output buffer not including the\r
           Null-terminator.\r
 \r
@@ -593,31 +654,36 @@ UnicodeValueToString (
 /**\r
   Produces a Null-terminated ASCII string in an output buffer based on a Null-terminated\r
   ASCII format string and a VA_LIST argument list.\r
-  \r
+\r
+  This function is similar as vsnprintf_s defined in C11.\r
+\r
   Produces a Null-terminated ASCII string in the output buffer specified by StartOfBuffer\r
   and BufferSize.\r
   The ASCII string is produced by parsing the format string specified by FormatString.\r
-  Arguments are pulled from the variable argument list specified by Marker based on \r
+  Arguments are pulled from the variable argument list specified by Marker based on\r
   the contents of the format string.\r
   The number of ASCII characters in the produced output buffer is returned not including\r
   the Null-terminator.\r
-  If BufferSize is 0, then no output buffer is produced and 0 is returned.\r
 \r
-  If BufferSize > 0 and StartOfBuffer is NULL, then ASSERT().\r
-  If BufferSize > 0 and FormatString is NULL, then ASSERT().\r
+  If BufferSize > 0 and StartOfBuffer is NULL, then ASSERT(). Also, the output buffer is\r
+  unmodified and 0 is returned.\r
+  If BufferSize > 0 and FormatString is NULL, then ASSERT(). Also, the output buffer is\r
+  unmodified and 0 is returned.\r
+  If PcdMaximumAsciiStringLength is not zero, and BufferSize >\r
+  (PcdMaximumAsciiStringLength * sizeof (CHAR8)), then ASSERT(). Also, the output buffer\r
+  is unmodified and 0 is returned.\r
   If PcdMaximumAsciiStringLength is not zero, and FormatString contains more than\r
-  PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator, then\r
-  ASSERT().\r
-  If PcdMaximumAsciiStringLength is not zero, and produced Null-terminated ASCII string\r
-  contains more than PcdMaximumAsciiStringLength ASCII characters not including the\r
-  Null-terminator, then ASSERT().\r
+  PcdMaximumAsciiStringLength Ascii characters not including the Null-terminator, then\r
+  ASSERT(). Also, the output buffer is unmodified and 0 is returned.\r
+\r
+  If BufferSize is 0, then no output buffer is produced and 0 is returned.\r
 \r
-  @param  StartOfBuffer   A pointer to the output buffer for the produced Null-terminated \r
+  @param  StartOfBuffer   A pointer to the output buffer for the produced Null-terminated\r
                           ASCII string.\r
   @param  BufferSize      The size, in bytes, of the output buffer specified by StartOfBuffer.\r
-  @param  FormatString    Null-terminated Unicode format string.\r
+  @param  FormatString    A Null-terminated ASCII format string.\r
   @param  Marker          VA_LIST marker for the variable argument list.\r
-  \r
+\r
   @return The number of ASCII characters in the produced output buffer not including the\r
           Null-terminator.\r
 \r
@@ -651,31 +717,34 @@ AsciiVSPrint (
 /**\r
   Produces a Null-terminated ASCII string in an output buffer based on a Null-terminated\r
   ASCII format string and a BASE_LIST argument list.\r
-  \r
+\r
   Produces a Null-terminated ASCII string in the output buffer specified by StartOfBuffer\r
   and BufferSize.\r
   The ASCII string is produced by parsing the format string specified by FormatString.\r
-  Arguments are pulled from the variable argument list specified by Marker based on \r
+  Arguments are pulled from the variable argument list specified by Marker based on\r
   the contents of the format string.\r
   The number of ASCII characters in the produced output buffer is returned not including\r
   the Null-terminator.\r
-  If BufferSize is 0, then no output buffer is produced and 0 is returned.\r
 \r
-  If BufferSize > 0 and StartOfBuffer is NULL, then ASSERT().\r
-  If BufferSize > 0 and FormatString is NULL, then ASSERT().\r
+  If BufferSize > 0 and StartOfBuffer is NULL, then ASSERT(). Also, the output buffer is\r
+  unmodified and 0 is returned.\r
+  If BufferSize > 0 and FormatString is NULL, then ASSERT(). Also, the output buffer is\r
+  unmodified and 0 is returned.\r
+  If PcdMaximumAsciiStringLength is not zero, and BufferSize >\r
+  (PcdMaximumAsciiStringLength * sizeof (CHAR8)), then ASSERT(). Also, the output buffer\r
+  is unmodified and 0 is returned.\r
   If PcdMaximumAsciiStringLength is not zero, and FormatString contains more than\r
-  PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator, then\r
-  ASSERT().\r
-  If PcdMaximumAsciiStringLength is not zero, and produced Null-terminated ASCII string\r
-  contains more than PcdMaximumAsciiStringLength ASCII characters not including the\r
-  Null-terminator, then ASSERT().\r
+  PcdMaximumAsciiStringLength Ascii characters not including the Null-terminator, then\r
+  ASSERT(). Also, the output buffer is unmodified and 0 is returned.\r
 \r
-  @param  StartOfBuffer   A pointer to the output buffer for the produced Null-terminated \r
+  If BufferSize is 0, then no output buffer is produced and 0 is returned.\r
+\r
+  @param  StartOfBuffer   A pointer to the output buffer for the produced Null-terminated\r
                           ASCII string.\r
   @param  BufferSize      The size, in bytes, of the output buffer specified by StartOfBuffer.\r
-  @param  FormatString    Null-terminated Unicode format string.\r
+  @param  FormatString    A Null-terminated ASCII format string.\r
   @param  Marker          BASE_LIST marker for the variable argument list.\r
-  \r
+\r
   @return The number of ASCII characters in the produced output buffer not including the\r
           Null-terminator.\r
 \r
@@ -695,30 +764,35 @@ AsciiBSPrint (
 /**\r
   Produces a Null-terminated ASCII string in an output buffer based on a Null-terminated\r
   ASCII format string and  variable argument list.\r
-  \r
+\r
+  This function is similar as snprintf_s defined in C11.\r
+\r
   Produces a Null-terminated ASCII string in the output buffer specified by StartOfBuffer\r
   and BufferSize.\r
   The ASCII string is produced by parsing the format string specified by FormatString.\r
-  Arguments are pulled from the variable argument list based on the contents of the \r
+  Arguments are pulled from the variable argument list based on the contents of the\r
   format string.\r
   The number of ASCII characters in the produced output buffer is returned not including\r
   the Null-terminator.\r
-  If BufferSize is 0, then no output buffer is produced and 0 is returned.\r
 \r
-  If BufferSize > 0 and StartOfBuffer is NULL, then ASSERT().\r
-  If BufferSize > 0 and FormatString is NULL, then ASSERT().\r
+  If BufferSize > 0 and StartOfBuffer is NULL, then ASSERT(). Also, the output buffer is\r
+  unmodified and 0 is returned.\r
+  If BufferSize > 0 and FormatString is NULL, then ASSERT(). Also, the output buffer is\r
+  unmodified and 0 is returned.\r
+  If PcdMaximumAsciiStringLength is not zero, and BufferSize >\r
+  (PcdMaximumAsciiStringLength * sizeof (CHAR8)), then ASSERT(). Also, the output buffer\r
+  is unmodified and 0 is returned.\r
   If PcdMaximumAsciiStringLength is not zero, and FormatString contains more than\r
-  PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator, then\r
-  ASSERT().\r
-  If PcdMaximumAsciiStringLength is not zero, and produced Null-terminated ASCII string\r
-  contains more than PcdMaximumAsciiStringLength ASCII characters not including the\r
-  Null-terminator, then ASSERT().\r
+  PcdMaximumAsciiStringLength Ascii characters not including the Null-terminator, then\r
+  ASSERT(). Also, the output buffer is unmodified and 0 is returned.\r
+\r
+  If BufferSize is 0, then no output buffer is produced and 0 is returned.\r
 \r
-  @param  StartOfBuffer   A pointer to the output buffer for the produced Null-terminated \r
+  @param  StartOfBuffer   A pointer to the output buffer for the produced Null-terminated\r
                           ASCII string.\r
   @param  BufferSize      The size, in bytes, of the output buffer specified by StartOfBuffer.\r
-  @param  FormatString    Null-terminated Unicode format string.\r
-  @param  ...             Variable argument list whose contents are accessed based on the \r
+  @param  FormatString    A Null-terminated ASCII format string.\r
+  @param  ...             Variable argument list whose contents are accessed based on the\r
                           format string specified by FormatString.\r
 \r
   @return The number of ASCII characters in the produced output buffer not including the\r
@@ -745,33 +819,39 @@ AsciiSPrint (
 \r
 /**\r
   Produces a Null-terminated ASCII string in an output buffer based on a Null-terminated\r
-  ASCII format string and a VA_LIST argument list.\r
-  \r
+  Unicode format string and a VA_LIST argument list.\r
+\r
+  This function is similar as vsnprintf_s defined in C11.\r
+\r
   Produces a Null-terminated ASCII string in the output buffer specified by StartOfBuffer\r
   and BufferSize.\r
   The ASCII string is produced by parsing the format string specified by FormatString.\r
-  Arguments are pulled from the variable argument list specified by Marker based on \r
+  Arguments are pulled from the variable argument list specified by Marker based on\r
   the contents of the format string.\r
   The number of ASCII characters in the produced output buffer is returned not including\r
   the Null-terminator.\r
-  If BufferSize is 0, then no output buffer is produced and 0 is returned.\r
 \r
-  If BufferSize > 0 and StartOfBuffer is NULL, then ASSERT().\r
-  If BufferSize > 0 and FormatString is NULL, then ASSERT().\r
-  If BufferSize > 0 and FormatString is not aligned on a 16-bit boundary, then ASSERT().\r
+  If FormatString is not aligned on a 16-bit boundary, then ASSERT().\r
+\r
+  If BufferSize > 0 and StartOfBuffer is NULL, then ASSERT(). Also, the output buffer is\r
+  unmodified and 0 is returned.\r
+  If BufferSize > 0 and FormatString is NULL, then ASSERT(). Also, the output buffer is\r
+  unmodified and 0 is returned.\r
+  If PcdMaximumAsciiStringLength is not zero, and BufferSize >\r
+  (PcdMaximumAsciiStringLength * sizeof (CHAR8)), then ASSERT(). Also, the output buffer\r
+  is unmodified and 0 is returned.\r
   If PcdMaximumUnicodeStringLength is not zero, and FormatString contains more than\r
   PcdMaximumUnicodeStringLength Unicode characters not including the Null-terminator, then\r
-  ASSERT().\r
-  If PcdMaximumAsciiStringLength is not zero, and produced Null-terminated ASCII string\r
-  contains more than PcdMaximumAsciiStringLength ASCII characters not including the\r
-  Null-terminator, then ASSERT().\r
+  ASSERT(). Also, the output buffer is unmodified and 0 is returned.\r
 \r
-  @param  StartOfBuffer   A pointer to the output buffer for the produced Null-terminated \r
+  If BufferSize is 0, then no output buffer is produced and 0 is returned.\r
+\r
+  @param  StartOfBuffer   A pointer to the output buffer for the produced Null-terminated\r
                           ASCII string.\r
   @param  BufferSize      The size, in bytes, of the output buffer specified by StartOfBuffer.\r
-  @param  FormatString    Null-terminated Unicode format string.\r
+  @param  FormatString    Null-terminated Unicode format string.\r
   @param  Marker          VA_LIST marker for the variable argument list.\r
-  \r
+\r
   @return The number of ASCII characters in the produced output buffer not including the\r
           Null-terminator.\r
 \r
@@ -788,6 +868,8 @@ AsciiVSPrintUnicodeFormat (
   UINT64   BaseListMarker[256 / sizeof (UINT64)];\r
   BOOLEAN  Converted;\r
 \r
+  ASSERT_UNICODE_BUFFER (FormatString);\r
+\r
   Converted = DxePrintLibPrint2ProtocolVaListToBaseList (\r
                 FALSE,\r
                 (CHAR8 *)FormatString,\r
@@ -804,33 +886,37 @@ AsciiVSPrintUnicodeFormat (
 \r
 /**\r
   Produces a Null-terminated ASCII string in an output buffer based on a Null-terminated\r
-  ASCII format string and a BASE_LIST argument list.\r
-  \r
+  Unicode format string and a BASE_LIST argument list.\r
+\r
   Produces a Null-terminated ASCII string in the output buffer specified by StartOfBuffer\r
   and BufferSize.\r
   The ASCII string is produced by parsing the format string specified by FormatString.\r
-  Arguments are pulled from the variable argument list specified by Marker based on \r
+  Arguments are pulled from the variable argument list specified by Marker based on\r
   the contents of the format string.\r
   The number of ASCII characters in the produced output buffer is returned not including\r
   the Null-terminator.\r
-  If BufferSize is 0, then no output buffer is produced and 0 is returned.\r
 \r
-  If BufferSize > 0 and StartOfBuffer is NULL, then ASSERT().\r
-  If BufferSize > 0 and FormatString is NULL, then ASSERT().\r
-  If BufferSize > 0 and FormatString is not aligned on a 16-bit boundary, then ASSERT().\r
+  If FormatString is not aligned on a 16-bit boundary, then ASSERT().\r
+\r
+  If BufferSize > 0 and StartOfBuffer is NULL, then ASSERT(). Also, the output buffer is\r
+  unmodified and 0 is returned.\r
+  If BufferSize > 0 and FormatString is NULL, then ASSERT(). Also, the output buffer is\r
+  unmodified and 0 is returned.\r
+  If PcdMaximumAsciiStringLength is not zero, and BufferSize >\r
+  (PcdMaximumAsciiStringLength * sizeof (CHAR8)), then ASSERT(). Also, the output buffer\r
+  is unmodified and 0 is returned.\r
   If PcdMaximumUnicodeStringLength is not zero, and FormatString contains more than\r
   PcdMaximumUnicodeStringLength Unicode characters not including the Null-terminator, then\r
-  ASSERT().\r
-  If PcdMaximumAsciiStringLength is not zero, and produced Null-terminated ASCII string\r
-  contains more than PcdMaximumAsciiStringLength ASCII characters not including the\r
-  Null-terminator, then ASSERT().\r
+  ASSERT(). Also, the output buffer is unmodified and 0 is returned.\r
 \r
-  @param  StartOfBuffer   A pointer to the output buffer for the produced Null-terminated \r
+  If BufferSize is 0, then no output buffer is produced and 0 is returned.\r
+\r
+  @param  StartOfBuffer   A pointer to the output buffer for the produced Null-terminated\r
                           ASCII string.\r
   @param  BufferSize      The size, in bytes, of the output buffer specified by StartOfBuffer.\r
-  @param  FormatString    Null-terminated Unicode format string.\r
+  @param  FormatString    Null-terminated Unicode format string.\r
   @param  Marker          BASE_LIST marker for the variable argument list.\r
-  \r
+\r
   @return The number of ASCII characters in the produced output buffer not including the\r
           Null-terminator.\r
 \r
@@ -844,37 +930,44 @@ AsciiBSPrintUnicodeFormat (
   IN  BASE_LIST     Marker\r
   )\r
 {\r
+  ASSERT_UNICODE_BUFFER (FormatString);\r
   return mPrint2Protocol->AsciiBSPrintUnicodeFormat (StartOfBuffer, BufferSize, FormatString, Marker);\r
 }\r
 \r
 /**\r
   Produces a Null-terminated ASCII string in an output buffer based on a Null-terminated\r
-  ASCII format string and  variable argument list.\r
-  \r
+  Unicode format string and  variable argument list.\r
+\r
+  This function is similar as snprintf_s defined in C11.\r
+\r
   Produces a Null-terminated ASCII string in the output buffer specified by StartOfBuffer\r
   and BufferSize.\r
   The ASCII string is produced by parsing the format string specified by FormatString.\r
-  Arguments are pulled from the variable argument list based on the contents of the \r
+  Arguments are pulled from the variable argument list based on the contents of the\r
   format string.\r
   The number of ASCII characters in the produced output buffer is returned not including\r
   the Null-terminator.\r
-  If BufferSize is 0, then no output buffer is produced and 0 is returned.\r
 \r
-  If BufferSize > 0 and StartOfBuffer is NULL, then ASSERT().\r
-  If BufferSize > 0 and FormatString is NULL, then ASSERT().\r
-  If BufferSize > 0 and FormatString is not aligned on a 16-bit boundary, then ASSERT().\r
+  If FormatString is not aligned on a 16-bit boundary, then ASSERT().\r
+\r
+  If BufferSize > 0 and StartOfBuffer is NULL, then ASSERT(). Also, the output buffer is\r
+  unmodified and 0 is returned.\r
+  If BufferSize > 0 and FormatString is NULL, then ASSERT(). Also, the output buffer is\r
+  unmodified and 0 is returned.\r
+  If PcdMaximumAsciiStringLength is not zero, and BufferSize >\r
+  (PcdMaximumAsciiStringLength * sizeof (CHAR8)), then ASSERT(). Also, the output buffer\r
+  is unmodified and 0 is returned.\r
   If PcdMaximumUnicodeStringLength is not zero, and FormatString contains more than\r
   PcdMaximumUnicodeStringLength Unicode characters not including the Null-terminator, then\r
-  ASSERT().\r
-  If PcdMaximumAsciiStringLength is not zero, and produced Null-terminated ASCII string\r
-  contains more than PcdMaximumAsciiStringLength ASCII characters not including the\r
-  Null-terminator, then ASSERT().\r
+  ASSERT(). Also, the output buffer is unmodified and 0 is returned.\r
 \r
-  @param  StartOfBuffer   A pointer to the output buffer for the produced Null-terminated \r
+  If BufferSize is 0, then no output buffer is produced and 0 is returned.\r
+\r
+  @param  StartOfBuffer   A pointer to the output buffer for the produced Null-terminated\r
                           ASCII string.\r
   @param  BufferSize      The size, in bytes, of the output buffer specified by StartOfBuffer.\r
-  @param  FormatString    Null-terminated Unicode format string.\r
-  @param  ...             Variable argument list whose contents are accessed based on the \r
+  @param  FormatString    Null-terminated Unicode format string.\r
+  @param  ...             Variable argument list whose contents are accessed based on the\r
                           format string specified by FormatString.\r
 \r
   @return The number of ASCII characters in the produced output buffer not including the\r
@@ -1254,6 +1347,56 @@ InternalPrintLibSPrintMarker (
   // DxePrintLibPrint2Protocol (both PrintLib instances).\r
   //\r
 \r
+  //\r
+  // 1. Buffer shall not be a null pointer when both BufferSize > 0 and\r
+  //    COUNT_ONLY_NO_PRINT is not set in Flags.\r
+  //\r
+  if ((BufferSize > 0) && ((Flags & COUNT_ONLY_NO_PRINT) == 0)) {\r
+    SAFE_PRINT_CONSTRAINT_CHECK ((Buffer != NULL), 0);\r
+  }\r
+\r
+  //\r
+  // 2. Format shall not be a null pointer when BufferSize > 0 or when\r
+  //    COUNT_ONLY_NO_PRINT is set in Flags.\r
+  //\r
+  if ((BufferSize > 0) || ((Flags & COUNT_ONLY_NO_PRINT) != 0)) {\r
+    SAFE_PRINT_CONSTRAINT_CHECK ((Format != NULL), 0);\r
+  }\r
+\r
+  //\r
+  // 3. BufferSize shall not be greater than RSIZE_MAX for Unicode output or\r
+  //    ASCII_RSIZE_MAX for Ascii output.\r
+  //\r
+  if ((Flags & OUTPUT_UNICODE) != 0) {\r
+    if (RSIZE_MAX != 0) {\r
+      SAFE_PRINT_CONSTRAINT_CHECK ((BufferSize <= RSIZE_MAX), 0);\r
+    }\r
+    BytesPerOutputCharacter = 2;\r
+  } else {\r
+    if (ASCII_RSIZE_MAX != 0) {\r
+      SAFE_PRINT_CONSTRAINT_CHECK ((BufferSize <= ASCII_RSIZE_MAX), 0);\r
+    }\r
+    BytesPerOutputCharacter = 1;\r
+  }\r
+\r
+  //\r
+  // 4. Format shall not contain more than RSIZE_MAX Unicode characters or\r
+  //    ASCII_RSIZE_MAX Ascii characters.\r
+  //\r
+  if ((Flags & FORMAT_UNICODE) != 0) {\r
+    if (RSIZE_MAX != 0) {\r
+      SAFE_PRINT_CONSTRAINT_CHECK ((StrnLenS ((CHAR16 *)Format, RSIZE_MAX + 1) <= RSIZE_MAX), 0);\r
+    }\r
+    BytesPerFormatCharacter = 2;\r
+    FormatMask = 0xffff;\r
+  } else {\r
+    if (ASCII_RSIZE_MAX != 0) {\r
+      SAFE_PRINT_CONSTRAINT_CHECK ((AsciiStrnLenS (Format, ASCII_RSIZE_MAX + 1) <= ASCII_RSIZE_MAX), 0);\r
+    }\r
+    BytesPerFormatCharacter = 1;\r
+    FormatMask = 0xff;\r
+  }\r
+\r
   if ((Flags & COUNT_ONLY_NO_PRINT) != 0) {\r
     if (BufferSize == 0) {\r
       Buffer = NULL;\r
@@ -1265,13 +1408,6 @@ InternalPrintLibSPrintMarker (
     if (BufferSize == 0) {\r
       return 0;\r
     }\r
-    ASSERT (Buffer != NULL);\r
-  }\r
-\r
-  if ((Flags & OUTPUT_UNICODE) != 0) {\r
-    BytesPerOutputCharacter = 2;\r
-  } else {\r
-    BytesPerOutputCharacter = 1;\r
   }\r
 \r
   LengthToReturn = 0;\r
@@ -1291,24 +1427,6 @@ InternalPrintLibSPrintMarker (
     EndBuffer = Buffer + BufferSize * BytesPerOutputCharacter;\r
   }\r
 \r
-  if ((Flags & FORMAT_UNICODE) != 0) {\r
-    //\r
-    // Make sure format string cannot contain more than PcdMaximumUnicodeStringLength\r
-    // Unicode characters if PcdMaximumUnicodeStringLength is not zero. \r
-    //\r
-    ASSERT (StrSize ((CHAR16 *) Format) != 0);\r
-    BytesPerFormatCharacter = 2;\r
-    FormatMask = 0xffff;\r
-  } else {\r
-    //\r
-    // Make sure format string cannot contain more than PcdMaximumAsciiStringLength\r
-    // Ascii characters if PcdMaximumAsciiStringLength is not zero. \r
-    //\r
-    ASSERT (AsciiStrSize (Format) != 0);\r
-    BytesPerFormatCharacter = 1;\r
-    FormatMask = 0xff;\r
-  }\r
-\r
   //\r
   // Get the first character from the format string\r
   //\r
@@ -1877,16 +1995,6 @@ InternalPrintLibSPrintMarker (
   // Null terminate the Unicode or ASCII string\r
   //\r
   InternalPrintLibFillBuffer (Buffer, EndBuffer + BytesPerOutputCharacter, 1, 0, BytesPerOutputCharacter);\r
-  //\r
-  // Make sure output buffer cannot contain more than PcdMaximumUnicodeStringLength\r
-  // Unicode characters if PcdMaximumUnicodeStringLength is not zero. \r
-  //\r
-  ASSERT ((((Flags & OUTPUT_UNICODE) == 0)) || (StrSize ((CHAR16 *) OriginalBuffer) != 0));\r
-  //\r
-  // Make sure output buffer cannot contain more than PcdMaximumAsciiStringLength\r
-  // ASCII characters if PcdMaximumAsciiStringLength is not zero. \r
-  //\r
-  ASSERT ((((Flags & OUTPUT_UNICODE) != 0)) || (AsciiStrSize (OriginalBuffer) != 0));\r
 \r
   return ((Buffer - OriginalBuffer) / BytesPerOutputCharacter);\r
 }\r
@@ -1895,9 +2003,13 @@ InternalPrintLibSPrintMarker (
   Returns the number of characters that would be produced by if the formatted \r
   output were produced not including the Null-terminator.\r
 \r
-  If FormatString is NULL, then ASSERT().\r
   If FormatString is not aligned on a 16-bit boundary, then ASSERT().\r
 \r
+  If FormatString is NULL, then ASSERT() and 0 is returned.\r
+  If PcdMaximumUnicodeStringLength is not zero, and FormatString contains more\r
+  than PcdMaximumUnicodeStringLength Unicode characters not including the\r
+  Null-terminator, then ASSERT() and 0 is returned.\r
+\r
   @param[in]  FormatString    A Null-terminated Unicode format string.\r
   @param[in]  Marker          VA_LIST marker for the variable argument list.\r
 \r
@@ -1911,7 +2023,7 @@ SPrintLength (
   IN  VA_LIST       Marker\r
   )\r
 {\r
-  ASSERT(FormatString != NULL);\r
+  ASSERT_UNICODE_BUFFER (FormatString);\r
   return InternalPrintLibSPrintMarker (NULL, 0, FORMAT_UNICODE | OUTPUT_UNICODE | COUNT_ONLY_NO_PRINT, (CHAR8 *)FormatString, Marker, NULL);\r
 }\r
 \r
@@ -1919,7 +2031,10 @@ SPrintLength (
   Returns the number of characters that would be produced by if the formatted \r
   output were produced not including the Null-terminator.\r
 \r
-  If FormatString is NULL, then ASSERT().\r
+  If FormatString is NULL, then ASSERT() and 0 is returned.\r
+  If PcdMaximumAsciiStringLength is not zero, and FormatString contains more\r
+  than PcdMaximumAsciiStringLength Ascii characters not including the\r
+  Null-terminator, then ASSERT() and 0 is returned.\r
 \r
   @param[in]  FormatString    A Null-terminated ASCII format string.\r
   @param[in]  Marker          VA_LIST marker for the variable argument list.\r
@@ -1934,6 +2049,5 @@ SPrintLengthAsciiFormat (
   IN  VA_LIST       Marker\r
   )\r
 {\r
-  ASSERT(FormatString != NULL);\r
   return InternalPrintLibSPrintMarker (NULL, 0, OUTPUT_UNICODE | COUNT_ONLY_NO_PRINT, (CHAR8 *)FormatString, Marker, NULL);\r
 }\r