]> git.proxmox.com Git - mirror_edk2.git/commitdiff
ShellPkg: Refine type cast for pointer subtraction
authorHao Wu <hao.a.wu@intel.com>
Mon, 23 Jan 2017 06:38:43 +0000 (14:38 +0800)
committerHao Wu <hao.a.wu@intel.com>
Mon, 6 Mar 2017 06:16:00 +0000 (14:16 +0800)
For pointer subtraction, the result is of type "ptrdiff_t". According to
the C11 standard (Committee Draft - April 12, 2011):

"When two pointers are subtracted, both shall point to elements of the
same array object, or one past the last element of the array object; the
result is the difference of the subscripts of the two array elements. The
size of the result is implementation-defined, and its type (a signed
integer type) is ptrdiff_t defined in the <stddef.h> header. If the result
is not representable in an object of that type, the behavior is
undefined."

In our codes, there are cases that the pointer subtraction is not
performed by pointers to elements of the same array object. This might
lead to potential issues, since the behavior is undefined according to C11
standard.

Also, since the size of type "ptrdiff_t" is implementation-defined. Some
static code checkers may warn that the pointer subtraction might underflow
first and then being cast to a bigger size. For example:

UINT8  *Ptr1, *Ptr2;
UINTN  PtrDiff;
...
PtrDiff = (UINTN) (Ptr1 - Ptr2);

The commit will refine the pointer subtraction expressions by casting each
pointer to UINTN first and then perform the subtraction:

PtrDiff = (UINTN) Ptr1 - (UINTN) Ptr2;

Contributed-under: TianoCore Contribution Agreement 1.0
Signed-off-by: Hao Wu <hao.a.wu@intel.com>
Acked-by: Laszlo Ersek <lersek@redhat.com>
Reviewed-by: Ruiyu Ni <ruiyu.ni@intel.com>
Reviewed-by: Jaben Carsey <jaben.carsey@intel.com>
ShellPkg/Application/Shell/ShellParametersProtocol.c
ShellPkg/Library/UefiShellDebug1CommandsLib/DmpStore.c
ShellPkg/Library/UefiShellDebug1CommandsLib/SmbiosView/SmbiosView.c
ShellPkg/Library/UefiShellDriver1CommandsLib/Dh.c
ShellPkg/Library/UefiShellLevel2CommandsLib/Cd.c

index 499915521aeec1f27d4dc66266453afd152424ff..8d76fb4be7ff0910d333b1814ed471a9e23099f1 100644 (file)
@@ -5,7 +5,7 @@
   (C) Copyright 2016 Hewlett Packard Enterprise Development LP<BR>\r
   Copyright (C) 2014, Red Hat, Inc.\r
   (C) Copyright 2013 Hewlett-Packard Development Company, L.P.<BR>\r
-  Copyright (c) 2009 - 2016, Intel Corporation. All rights reserved.<BR>\r
+  Copyright (c) 2009 - 2017, Intel Corporation. All rights reserved.<BR>\r
   This program and the accompanying materials\r
   are licensed and made available under the terms and conditions of the BSD License\r
   which accompanies this distribution.  The full text of the license may be found at\r
@@ -1034,9 +1034,9 @@ UpdateStdInStdOutStdErr(
   StrnCpyS(CommandLineCopy, StrSize(CommandLineCopy)/sizeof(CHAR16), NewCommandLine, StrLen(NewCommandLine));\r
 \r
   if (FirstLocation != CommandLineCopy + StrLen(CommandLineCopy)\r
-    && ((UINTN)(FirstLocation - CommandLineCopy) < StrLen(NewCommandLine))\r
+    && (((UINTN)FirstLocation - (UINTN)CommandLineCopy)/sizeof(CHAR16) < StrLen(NewCommandLine))\r
     ){\r
-    *(NewCommandLine + (UINTN)(FirstLocation - CommandLineCopy)) = CHAR_NULL;\r
+    *(NewCommandLine + ((UINTN)FirstLocation - (UINTN)CommandLineCopy)/sizeof(CHAR16)) = CHAR_NULL;\r
   }\r
 \r
   if (!EFI_ERROR(Status)) {\r
index 701ff7505f2158f3d59923c2127fe20639270c08..23db54553f11ece5e1d629d7e9998360a7144016 100644 (file)
@@ -364,7 +364,7 @@ AppendSingleVariableToFile (
   //\r
   // Crc32\r
   //\r
-  gBS->CalculateCrc32 (Buffer, (UINTN) (Ptr - Buffer), (UINT32 *) Ptr);\r
+  gBS->CalculateCrc32 (Buffer, (UINTN) Ptr - (UINTN) Buffer, (UINT32 *) Ptr);\r
 \r
   Status = ShellWriteFile (FileHandle, &BufferSize, Buffer);\r
   FreePool (Buffer);\r
index 56b682aa606dd0d64e4267325a9a91bdd31b8c1e..a5b16fec08176ceb1c5e4bd6c780195a0c39bed4 100644 (file)
@@ -2,7 +2,7 @@
   Tools of clarify the content of the smbios table.\r
 \r
   (C) Copyright 2015 Hewlett-Packard Development Company, L.P.<BR>\r
-  Copyright (c) 2005 - 2012, Intel Corporation. All rights reserved.<BR>\r
+  Copyright (c) 2005 - 2017, Intel Corporation. All rights reserved.<BR>\r
   This program and the accompanying materials\r
   are licensed and made available under the terms and conditions of the BSD License\r
   which accompanies this distribution.  The full text of the license may be found at\r
@@ -700,7 +700,7 @@ CalculateSmbios64BitStructureCountAndLength (
     //\r
     // Length = Next structure head - this structure head\r
     //\r
-    (*Smbios64TableLength) += (UINTN) (Smbios.Raw - Raw);\r
+    (*Smbios64TableLength) += ((UINTN) Smbios.Raw - (UINTN) Raw);\r
     if ((*Smbios64TableLength) > Smbios64EntryPoint->TableMaximumSize) {\r
        //\r
        // The actual table length exceeds maximum table size,\r
index d17a29d86fbebd04b273266e14729ddc7fa2e1fa..0bcee9245c63fdb6c616c5f843e394a1bbe9f179 100644 (file)
@@ -75,7 +75,7 @@ IsValidGuidString(
        ) {\r
       Walker++;\r
     } else {\r
-      if (*Walker == L'-' && (UINTN)(Walker - PrevWalker) == mGuidDataLen[Index]) {\r
+      if (*Walker == L'-' && (((UINTN)Walker - (UINTN)PrevWalker) / sizeof (CHAR16)) == mGuidDataLen[Index]) {\r
         Walker++;\r
         PrevWalker = Walker;\r
         Index++;\r
@@ -85,7 +85,7 @@ IsValidGuidString(
     }\r
   }\r
 \r
-  if ((UINTN)(Walker - PrevWalker) == mGuidDataLen[Index]) {\r
+  if ((((UINTN)Walker - (UINTN)PrevWalker) / sizeof (CHAR16)) == mGuidDataLen[Index]) {\r
     return TRUE;\r
   } else {\r
     return FALSE;\r
index dd4a740354a331839f8868082c62781aaa3f936d..9ae81763f72fd585769ce1943a200b55d8e56239 100644 (file)
@@ -99,10 +99,10 @@ IsCurrentFileSystem (
 \r
   Splitter2 = StrStr (Cwd, L":");\r
 \r
-  if ((UINTN) (Splitter1 - FullPath) != (UINTN) (Splitter2 - Cwd)) {\r
+  if (((UINTN) Splitter1 - (UINTN) FullPath) != ((UINTN) Splitter2 - (UINTN) Cwd)) {\r
     return FALSE;\r
   } else {\r
-    if (StrniCmp (FullPath, Cwd, (UINTN) (Splitter1 - FullPath)) == NULL) {\r
+    if (StrniCmp (FullPath, Cwd, ((UINTN) Splitter1 - (UINTN) FullPath) / sizeof (CHAR16)) == NULL) {\r
       return TRUE;\r
     } else {\r
       return FALSE;\r