Reviewed-by: Andrew Fish <afish@apple.com>

Thanks,

Andrew Fish

On Jul 16, 2019, at 9:58 AM, Carsey, Jaben <jaben.carsey@intel.com> wrote:

Reviewed-by: Jaben Carsey <jaben.carsey@intel.com>

Thanks
-Jaben

-----Original Message-----
From: Gao, Zhichao
Sent: Tuesday, July 16, 2019 2:12 AM
To: devel@edk2.groups.io
Cc: Carsey, Jaben <jaben.carsey@intel.com>; Ni, Ray <ray.ni@intel.com>;
Andrew Fish <afish@apple.com>
Subject: [PATCH V2] ShellPkg/Type.c: Add value check before (LoopVar - 1)

REF: https://bugzilla.tianocore.org/show_bug.cgi?id=1964

If the file begin with single line Feed ('\n'), then
"AsciiChar == '\n' && ((CHAR8*)Buffer)[LoopVar-1] != '\r'"
would cause a underflow. Add this condition
"(AsciiChar == '\n' && LoopVar == 0)" before it to make sure
(LoopVar - 1) would never encounter a underflow.

Same change in Unicode section.

Cc: Jaben Carsey <jaben.carsey@intel.com>
Cc: Ray Ni <ray.ni@intel.com>
Cc: Andrew Fish <afish@apple.com>
Signed-off-by: Zhichao Gao <zhichao.gao@intel.com>
---
V2:
1. Update the copyright
2. Fix the same issue in unicode section
3. Fix typo

.../Library/UefiShellLevel3CommandsLib/Type.c | 24 ++++++++++---------
1 file changed, 13 insertions(+), 11 deletions(-)

diff --git a/ShellPkg/Library/UefiShellLevel3CommandsLib/Type.c
b/ShellPkg/Library/UefiShellLevel3CommandsLib/Type.c
index 4efc0a8e24..f0aa57af3d 100644
--- a/ShellPkg/Library/UefiShellLevel3CommandsLib/Type.c
+++ b/ShellPkg/Library/UefiShellLevel3CommandsLib/Type.c
@@ -2,7 +2,7 @@
  Main file for Type shell level 3 function.

  (C) Copyright 2013-2015 Hewlett-Packard Development Company, L.P.<BR>
-  Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved. <BR>
+  Copyright (c) 2009 - 2019, Intel Corporation. All rights reserved. <BR>
  SPDX-License-Identifier: BSD-2-Clause-Patent

**/
@@ -78,12 +78,13 @@ TypeFileByHandle (
          // Allow Line Feed (LF) (0xA) & Carriage Return (CR) (0xD)
          // characters to be displayed as is.
          //
-          if (AsciiChar == '\n' && ((CHAR8*)Buffer)[LoopVar-1] != '\r') {
+          if ((AsciiChar == '\n' && LoopVar == 0) ||
+              (AsciiChar == '\n' && ((CHAR8*)Buffer)[LoopVar-1] != '\r')) {
            //
-            // In case Line Feed (0xA) is encountered & Carriage Return (0xD)
-            // was not the previous character, print CR and LF. This is because
-            // Shell 2.0 requires carriage return with line feed for displaying
-            // each new line from left.
+            // In case file begin with single line Feed or Line Feed (0xA) is
+            // encountered & Carriage Return (0xD) was not previous character,
+            // print CR and LF. This is because Shell 2.0 requires carriage
+            // return with line feed for displaying each new line from left.
            //
            ShellPrintEx (-1, -1, L"\r\n");
            continue;
@@ -121,12 +122,13 @@ TypeFileByHandle (
          // Allow Line Feed (LF) (0xA) & Carriage Return (CR) (0xD)
          // characters to be displayed as is.
          //
-          if (Ucs2Char == '\n' && ((CHAR16*)Buffer)[LoopVar-1] != '\r') {
+          if ((Ucs2Char == '\n' && LoopVar == 0) ||
+              (Ucs2Char == '\n' && ((CHAR16*)Buffer)[LoopVar-1] != '\r')) {
            //
-            // In case Line Feed (0xA) is encountered & Carriage Return (0xD)
-            // was not the previous character, print CR and LF. This is because
-            // Shell 2.0 requires carriage return with line feed for displaying
-            // each new line from left.
+            // In case file begin with single line Feed or Line Feed (0xA) is
+            // encountered & Carriage Return (0xD) was not previous character,
+            // print CR and LF. This is because Shell 2.0 requires carriage
+            // return with line feed for displaying each new line from left.
            //
            ShellPrintEx (-1, -1, L"\r\n");
            continue;
--
2.21.0.windows.1