ArmPkg/DefaultExceptionHandlerLib: replace AsciiStrCat() with AsciiStrCatS()

AsciiStrCat() is deprecated / disabled under the
DISABLE_NEW_DEPRECATED_INTERFACES feature test macro.

The caller of CpsrString() is required to pass in "ReturnStr" with 32
CHAR8 elements. (DefaultExceptionHandler() complies with this.) "Str" is
used to build "ReturnStr" gradually. Just before calling AsciiStrCat(),
"Str" points to the then-terminating NUL character in "ReturnStr".

The difference (Str - ReturnStr) gives the number of non-NUL characters
we've written thus far, hence (32 - (Str - ReturnStr)) yields the number
of remaining bytes in ReturnStr, including the ultimately terminating NUL
character.

Cc: Ard Biesheuvel <ard.biesheuvel@linaro.org>
Cc: Leif Lindholm <leif.lindholm@linaro.org>
Cc: Michael Zimmermann <sigmaepsilon92@gmail.com>
Ref: https://bugzilla.tianocore.org/show_bug.cgi?id=164
Ref: https://bugzilla.tianocore.org/show_bug.cgi?id=165
Contributed-under: TianoCore Contribution Agreement 1.0
Signed-off-by: Laszlo Ersek <lersek@redhat.com>
Reviewed-by: Ard Biesheuvel <ard.biesheuvel@linaro.org>
Reviewed-by: Jordan Justen <jordan.l.justen@intel.com>
diff --git a/ArmPkg/Library/DefaultExceptionHandlerLib/Arm/DefaultExceptionHandler.c b/ArmPkg/Library/DefaultExceptionHandlerLib/Arm/DefaultExceptionHandler.c
index aece263..0b9da03 100644
--- a/ArmPkg/Library/DefaultExceptionHandlerLib/Arm/DefaultExceptionHandler.c
+++ b/ArmPkg/Library/DefaultExceptionHandlerLib/Arm/DefaultExceptionHandler.c
@@ -27,6 +27,12 @@
 #include <Protocol/DebugSupport.h>

 #include <Library/DefaultExceptionHandlerLib.h>

 

+//

+// The number of elements in a CHAR8 array, including the terminating NUL, that

+// is meant to hold the string rendering of the CPSR.

+//

+#define CPSR_STRING_SIZE 32

+

 typedef struct {

   UINT32  BIT;

   CHAR8   Char;

@@ -46,7 +52,8 @@
   It is possible to add extra bits by adding them to CpsrChar array.

 

   @param  Cpsr         ARM CPSR register value

-  @param  ReturnStr    32 byte string that contains string version of CPSR

+  @param  ReturnStr    CPSR_STRING_SIZE byte string that contains string

+                       version of CPSR

 

 **/

 VOID

@@ -116,8 +123,10 @@
     break;

   }

 

-  AsciiStrCat (Str, ModeStr);

-  return;

+  //

+  // See the interface contract in the leading comment block.

+  //

+  AsciiStrCatS (Str, CPSR_STRING_SIZE - (Str - ReturnStr), ModeStr);

 }

 

 CHAR8 *

@@ -192,7 +201,8 @@
     UINT32  ImageBase;

     UINT32  PeCoffSizeOfHeader;

     UINT32  Offset;

-    CHAR8   CpsrStr[32];  // char per bit. Lower 5-bits are mode that is a 3 char string

+    CHAR8   CpsrStr[CPSR_STRING_SIZE];  // char per bit. Lower 5-bits are mode

+                                        // that is a 3 char string

     CHAR8   Buffer[80];

     UINT8   *DisAsm;

     UINT32  ItBlock;