]> git.proxmox.com Git - mirror_edk2.git/commitdiff
ShellPkg/Type.c: Add value check before (LoopVar - 1)
authorZhichao Gao <zhichao.gao@intel.com>
Mon, 15 Jul 2019 06:11:49 +0000 (14:11 +0800)
committerJaben Carsey <jaben.carsey@intel.com>
Fri, 19 Jul 2019 15:32:11 +0000 (08:32 -0700)
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>
Reviewed-by: Jaben Carsey <jaben.carsey@intel.com>
Reviewed-by: Andrew Fish <afish@apple.com>
ShellPkg/Library/UefiShellLevel3CommandsLib/Type.c

index 4efc0a8e24e9f909f375ffb796510854b53134a0..f0aa57af3d9d461d85c00610ca7910473d9082d8 100644 (file)
@@ -2,7 +2,7 @@
   Main file for Type shell level 3 function.\r
 \r
   (C) Copyright 2013-2015 Hewlett-Packard Development Company, L.P.<BR>\r
-  Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved. <BR>\r
+  Copyright (c) 2009 - 2019, Intel Corporation. All rights reserved. <BR>\r
   SPDX-License-Identifier: BSD-2-Clause-Patent\r
 \r
 **/\r
@@ -78,12 +78,13 @@ TypeFileByHandle (
           // Allow Line Feed (LF) (0xA) & Carriage Return (CR) (0xD)\r
           // characters to be displayed as is.\r
           //\r
-          if (AsciiChar == '\n' && ((CHAR8*)Buffer)[LoopVar-1] != '\r') {\r
+          if ((AsciiChar == '\n' && LoopVar == 0) ||\r
+              (AsciiChar == '\n' && ((CHAR8*)Buffer)[LoopVar-1] != '\r')) {\r
             //\r
-            // In case Line Feed (0xA) is encountered & Carriage Return (0xD)\r
-            // was not the previous character, print CR and LF. This is because\r
-            // Shell 2.0 requires carriage return with line feed for displaying\r
-            // each new line from left.\r
+            // In case file begin with single line Feed or Line Feed (0xA) is\r
+            // encountered & Carriage Return (0xD) was not previous character,\r
+            // print CR and LF. This is because Shell 2.0 requires carriage\r
+            // return with line feed for displaying each new line from left.\r
             //\r
             ShellPrintEx (-1, -1, L"\r\n");\r
             continue;\r
@@ -121,12 +122,13 @@ TypeFileByHandle (
           // Allow Line Feed (LF) (0xA) & Carriage Return (CR) (0xD)\r
           // characters to be displayed as is.\r
           //\r
-          if (Ucs2Char == '\n' && ((CHAR16*)Buffer)[LoopVar-1] != '\r') {\r
+          if ((Ucs2Char == '\n' && LoopVar == 0) ||\r
+              (Ucs2Char == '\n' && ((CHAR16*)Buffer)[LoopVar-1] != '\r')) {\r
             //\r
-            // In case Line Feed (0xA) is encountered & Carriage Return (0xD)\r
-            // was not the previous character, print CR and LF. This is because\r
-            // Shell 2.0 requires carriage return with line feed for displaying\r
-            // each new line from left.\r
+            // In case file begin with single line Feed or Line Feed (0xA) is\r
+            // encountered & Carriage Return (0xD) was not previous character,\r
+            // print CR and LF. This is because Shell 2.0 requires carriage\r
+            // return with line feed for displaying each new line from left.\r
             //\r
             ShellPrintEx (-1, -1, L"\r\n");\r
             continue;\r