]> git.proxmox.com Git - mirror_edk2.git/commitdiff
MdeModulePkg: Refine type cast for pointer subtraction
authorHao Wu <hao.a.wu@intel.com>
Mon, 23 Jan 2017 03:56:05 +0000 (11:56 +0800)
committerHao Wu <hao.a.wu@intel.com>
Mon, 6 Mar 2017 06:14:09 +0000 (14:14 +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: Feng Tian <feng.tian@intel.com>
MdeModulePkg/Bus/Pci/PciBusDxe/PciOptionRomSupport.c
MdeModulePkg/Include/Library/NetLib.h
MdeModulePkg/Library/DxeCapsuleLibFmp/DxeCapsuleLib.c
MdeModulePkg/Library/DxePrintLibPrint2Protocol/PrintLib.c
MdeModulePkg/Library/FileExplorerLib/FileExplorer.c
MdeModulePkg/Library/PiDxeS3BootScriptLib/BootScriptSave.c
MdeModulePkg/Library/UefiBootManagerLib/BmLoadOption.c
MdeModulePkg/Universal/DebugPortDxe/DebugPort.c
MdeModulePkg/Universal/FaultTolerantWriteDxe/FaultTolerantWrite.c
MdeModulePkg/Universal/HiiDatabaseDxe/Image.c
MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c

index 2bc4f8c5e8573a144e05ea21605d17a7479a994a..d2ad94eceb3a11347995b4d2dbe9cd620c218b0d 100644 (file)
@@ -1,7 +1,7 @@
 /** @file\r
   PCI Rom supporting funtions implementation for PCI Bus module.\r
 \r
 /** @file\r
   PCI Rom supporting funtions implementation for PCI Bus module.\r
 \r
-Copyright (c) 2006 - 2015, Intel Corporation. All rights reserved.<BR>\r
+Copyright (c) 2006 - 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
 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
@@ -776,7 +776,7 @@ ProcessOpRomImage (
 NextImage:\r
     RomBarOffset += ImageSize;\r
 \r
 NextImage:\r
     RomBarOffset += ImageSize;\r
 \r
-  } while (((Indicator & 0x80) == 0x00) && ((UINTN) (RomBarOffset - (UINT8 *) RomBar) < PciDevice->RomSize));\r
+  } while (((Indicator & 0x80) == 0x00) && (((UINTN) RomBarOffset - (UINTN) RomBar) < PciDevice->RomSize));\r
 \r
   return RetStatus;\r
 }\r
 \r
   return RetStatus;\r
 }\r
index 6773ed5074f073d2f6d27d01cf1e07e569a68e31..4cd42270f6c03d386497890e18afed71fd6f4ef5 100644 (file)
@@ -1607,10 +1607,10 @@ typedef struct {
   (sizeof (NET_BUF) + ((BlockOpNum) - 1) * sizeof (NET_BLOCK_OP))\r
 \r
 #define NET_HEADSPACE(BlockOp)  \\r
   (sizeof (NET_BUF) + ((BlockOpNum) - 1) * sizeof (NET_BLOCK_OP))\r
 \r
 #define NET_HEADSPACE(BlockOp)  \\r
-  (UINTN)((BlockOp)->Head - (BlockOp)->BlockHead)\r
+  ((UINTN)((BlockOp)->Head) - (UINTN)((BlockOp)->BlockHead))\r
 \r
 #define NET_TAILSPACE(BlockOp)  \\r
 \r
 #define NET_TAILSPACE(BlockOp)  \\r
-  (UINTN)((BlockOp)->BlockTail - (BlockOp)->Tail)\r
+  ((UINTN)((BlockOp)->BlockTail) - (UINTN)((BlockOp)->Tail))\r
 \r
 /**\r
   Allocate a single block NET_BUF. Upon allocation, all the\r
 \r
 /**\r
   Allocate a single block NET_BUF. Upon allocation, all the\r
index 71e05bd55783a83aeae4d0468009bca49d29a36f..d7abcc879b5abdba7a520efc8f705ae90b38c7ad 100644 (file)
@@ -10,7 +10,7 @@
   ValidateFmpCapsule(), DisplayCapsuleImage(), ConvertBmpToGopBlt() will\r
   receive untrusted input and do basic validation.\r
 \r
   ValidateFmpCapsule(), DisplayCapsuleImage(), ConvertBmpToGopBlt() will\r
   receive untrusted input and do basic validation.\r
 \r
-  Copyright (c) 2016, Intel Corporation. All rights reserved.<BR>\r
+  Copyright (c) 2016 - 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
   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
@@ -547,7 +547,7 @@ ConvertBmpToGopBlt (
 \r
     }\r
 \r
 \r
     }\r
 \r
-    ImageIndex = (UINTN) (Image - ImageHeader);\r
+    ImageIndex = (UINTN) Image - (UINTN) ImageHeader;\r
     if ((ImageIndex % 4) != 0) {\r
       //\r
       // Bmp Image starts each row on a 32-bit boundary!\r
     if ((ImageIndex % 4) != 0) {\r
       //\r
       // Bmp Image starts each row on a 32-bit boundary!\r
index 434736f9742b613bd06845e2f1da0f20778d96ea..6a4d25b73a7d293d54f3f1edb36c5b8c1bafb722 100644 (file)
@@ -226,7 +226,7 @@ DxePrintLibPrint2ProtocolVaListToBaseList (
     //\r
     // If BASE_LIST is larger than Size, then return FALSE\r
     //\r
     //\r
     // If BASE_LIST is larger than Size, then return FALSE\r
     //\r
-    if ((UINTN)((UINT8 *)BaseListMarker - (UINT8 *)BaseListStart) > Size) {\r
+    if (((UINTN)BaseListMarker - (UINTN)BaseListStart) > Size) {\r
       DEBUG ((DEBUG_ERROR, "The input variable argument list is too long. Please consider breaking into multiple print calls.\n"));\r
       return FALSE;\r
     }\r
       DEBUG ((DEBUG_ERROR, "The input variable argument list is too long. Please consider breaking into multiple print calls.\n"));\r
       return FALSE;\r
     }\r
index 5eedad72039d0392f75914b35c511ddbe610c549..9182751ad75bdaaee0f01a10bd132fc0bf4bc43b 100644 (file)
@@ -728,7 +728,7 @@ LibAppendFileName (
       // that overlap.\r
       //\r
       StrCpyS (TmpStr, MaxLen, Ptr + 3);\r
       // that overlap.\r
       //\r
       StrCpyS (TmpStr, MaxLen, Ptr + 3);\r
-      StrCpyS (LastSlash, MaxLen - (UINTN) (LastSlash - Str), TmpStr);\r
+      StrCpyS (LastSlash, MaxLen - ((UINTN) LastSlash - (UINTN) Str) / sizeof (CHAR16), TmpStr);\r
       Ptr = LastSlash;\r
     } else if (*Ptr == '\\' && *(Ptr + 1) == '.' && *(Ptr + 2) == '\\') {\r
       //\r
       Ptr = LastSlash;\r
     } else if (*Ptr == '\\' && *(Ptr + 1) == '.' && *(Ptr + 2) == '\\') {\r
       //\r
@@ -740,7 +740,7 @@ LibAppendFileName (
       // that overlap.\r
       //\r
       StrCpyS (TmpStr, MaxLen, Ptr + 2);\r
       // that overlap.\r
       //\r
       StrCpyS (TmpStr, MaxLen, Ptr + 2);\r
-      StrCpyS (Ptr, MaxLen - (UINTN) (Ptr - Str), TmpStr);\r
+      StrCpyS (Ptr, MaxLen - ((UINTN) Ptr - (UINTN) Str) / sizeof (CHAR16), TmpStr);\r
       Ptr = LastSlash;\r
     } else if (*Ptr == '\\') {\r
       LastSlash = Ptr;\r
       Ptr = LastSlash;\r
     } else if (*Ptr == '\\') {\r
       LastSlash = Ptr;\r
index 5698c916639952056700dba391e8ece5b25600b9..1f8aaf47c8753b8651c691ecd0934642c0aecd1e 100644 (file)
@@ -1,7 +1,7 @@
 /** @file\r
   Save the S3 data to S3 boot script.\r
 \r
 /** @file\r
   Save the S3 data to S3 boot script.\r
 \r
-  Copyright (c) 2006 - 2016, Intel Corporation. All rights reserved.<BR>\r
+  Copyright (c) 2006 - 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\r
 \r
   This program and the accompanying materials\r
   are licensed and made available under the terms and conditions\r
@@ -2025,7 +2025,7 @@ S3BootScriptCalculateInsertAddress (
    // calculate the Position offset\r
    //\r
    if (Position != NULL) {\r
    // calculate the Position offset\r
    //\r
    if (Position != NULL) {\r
-     PositionOffset = (UINTN) ((UINT8 *)Position - S3TableBase);\r
+     PositionOffset = (UINTN)Position - (UINTN)S3TableBase;\r
 \r
      //\r
      // If the BeforeOrAfter is FALSE, that means to insert the node right after the node.\r
 \r
      //\r
      // If the BeforeOrAfter is FALSE, that means to insert the node right after the node.\r
index 6f705bd4673f12a74919248cc5a229ace880e4d5..116cf28fe388ddf24c1094071645463819626af3 100644 (file)
@@ -1,7 +1,7 @@
 /** @file\r
   Load option library functions which relate with creating and processing load options.\r
 \r
 /** @file\r
   Load option library functions which relate with creating and processing load options.\r
 \r
-Copyright (c) 2011 - 2016, Intel Corporation. All rights reserved.<BR>\r
+Copyright (c) 2011 - 2017, Intel Corporation. All rights reserved.<BR>\r
 (C) Copyright 2015-2016 Hewlett Packard Enterprise Development LP<BR>\r
 This program and the accompanying materials\r
 are licensed and made available under the terms and conditions of the BSD License\r
 (C) Copyright 2015-2016 Hewlett Packard Enterprise Development LP<BR>\r
 This program and the accompanying materials\r
 are licensed and made available under the terms and conditions of the BSD License\r
@@ -912,7 +912,7 @@ EfiBootManagerVariableToLoadOptionEx (
   FilePath = (EFI_DEVICE_PATH_PROTOCOL *) VariablePtr;\r
   VariablePtr += FilePathSize;\r
 \r
   FilePath = (EFI_DEVICE_PATH_PROTOCOL *) VariablePtr;\r
   VariablePtr += FilePathSize;\r
 \r
-  OptionalDataSize = (UINT32) (VariableSize - (UINTN) (VariablePtr - Variable));\r
+  OptionalDataSize = (UINT32) (VariableSize - ((UINTN) VariablePtr - (UINTN) Variable));\r
   if (OptionalDataSize == 0) {\r
     OptionalData = NULL;\r
   } else {\r
   if (OptionalDataSize == 0) {\r
     OptionalData = NULL;\r
   } else {\r
index 298b6b2da3e7a18b15dc3a8f0f475e22770e213c..dcb623cfebc5211cc31f8062f740aecc8200d5c8 100644 (file)
@@ -4,7 +4,7 @@
   ALL CODE IN THE SERIALIO STACK MUST BE RE-ENTRANT AND CALLABLE FROM\r
   INTERRUPT CONTEXT\r
 \r
   ALL CODE IN THE SERIALIO STACK MUST BE RE-ENTRANT AND CALLABLE FROM\r
   INTERRUPT CONTEXT\r
 \r
-Copyright (c) 2006 - 2014, Intel Corporation. All rights reserved.<BR>\r
+Copyright (c) 2006 - 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
 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
@@ -569,7 +569,7 @@ DebugPortRead (
     LocalBufferSize = *BufferSize - (BufferPtr - (UINT8 *) Buffer);\r
   } while (LocalBufferSize != 0 && Timeout > 0);\r
 \r
     LocalBufferSize = *BufferSize - (BufferPtr - (UINT8 *) Buffer);\r
   } while (LocalBufferSize != 0 && Timeout > 0);\r
 \r
-  *BufferSize = (UINTN) (BufferPtr - (UINT8 *) Buffer);\r
+  *BufferSize = (UINTN) BufferPtr - (UINTN) Buffer;\r
 \r
   return Status;\r
 }\r
 \r
   return Status;\r
 }\r
index 2e3e8c77d14f1fa87ad7f94770eae78f754e693e..49e747b9016bd41fe2894d875ca4edc98d0cf6ea 100644 (file)
@@ -3,7 +3,7 @@
   These are the common Fault Tolerant Write (FTW) functions that are shared \r
   by DXE FTW driver and SMM FTW driver.\r
 \r
   These are the common Fault Tolerant Write (FTW) functions that are shared \r
   by DXE FTW driver and SMM FTW driver.\r
 \r
-Copyright (c) 2006 - 2015, Intel Corporation. All rights reserved.<BR>\r
+Copyright (c) 2006 - 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
 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
@@ -373,7 +373,7 @@ FtwWrite (
   //\r
   // If Record is out of the range of Header, return access denied.\r
   //\r
   //\r
   // If Record is out of the range of Header, return access denied.\r
   //\r
-  if (((UINTN)((UINT8 *) Record - (UINT8 *) Header)) > FTW_WRITE_TOTAL_SIZE (Header->NumberOfWrites - 1, Header->PrivateDataSize)) {\r
+  if (((UINTN) Record - (UINTN) Header) > FTW_WRITE_TOTAL_SIZE (Header->NumberOfWrites - 1, Header->PrivateDataSize)) {\r
     return EFI_ACCESS_DENIED;\r
   }\r
 \r
     return EFI_ACCESS_DENIED;\r
   }\r
 \r
index 1668828486a73a22f5634d97c39bb1adfe869fca..e2fa16e6e02bad5abee3cc8c79b68d2adee89930 100644 (file)
@@ -2,7 +2,7 @@
 Implementation for EFI_HII_IMAGE_PROTOCOL.\r
 \r
 \r
 Implementation for EFI_HII_IMAGE_PROTOCOL.\r
 \r
 \r
-Copyright (c) 2007 - 2016, Intel Corporation. All rights reserved.<BR>\r
+Copyright (c) 2007 - 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
 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
@@ -1124,7 +1124,7 @@ HiiSetImage (
     return EFI_OUT_OF_RESOURCES;\r
   }\r
 \r
     return EFI_OUT_OF_RESOURCES;\r
   }\r
 \r
-  Part1Size = (UINT32) (UINTN) ((UINT8 *) CurrentImageBlock - (UINT8 *) ImagePackage->ImageBlock);\r
+  Part1Size = (UINT32) ((UINTN) CurrentImageBlock - (UINTN) ImagePackage->ImageBlock);\r
   Part2Size = ImagePackage->ImageBlockSize - Part1Size - OldBlockSize;\r
   CopyMem (ImageBlocks, ImagePackage->ImageBlock, Part1Size);\r
 \r
   Part2Size = ImagePackage->ImageBlockSize - Part1Size - OldBlockSize;\r
   CopyMem (ImageBlocks, ImagePackage->ImageBlock, Part1Size);\r
 \r
index f5b6a5f93ea628fd8fcb9ea7d5abc7a5db5e6f06..b0c7434baeb1f0fe32de32a24bca99470e47ec8f 100644 (file)
@@ -16,7 +16,7 @@
   VariableServiceSetVariable() should also check authenticate data to avoid buffer overflow,\r
   integer overflow. It should also check attribute to avoid authentication bypass.\r
 \r
   VariableServiceSetVariable() should also check authenticate data to avoid buffer overflow,\r
   integer overflow. It should also check attribute to avoid authentication bypass.\r
 \r
-Copyright (c) 2006 - 2016, Intel Corporation. All rights reserved.<BR>\r
+Copyright (c) 2006 - 2017, Intel Corporation. All rights reserved.<BR>\r
 (C) Copyright 2015 Hewlett Packard Enterprise Development LP<BR>\r
 This program and the accompanying materials\r
 are licensed and made available under the terms and conditions of the BSD License\r
 (C) Copyright 2015 Hewlett Packard Enterprise Development LP<BR>\r
 This program and the accompanying materials\r
 are licensed and made available under the terms and conditions of the BSD License\r
@@ -1170,7 +1170,7 @@ Reclaim (
   // Install the new variable if it is not NULL.\r
   //\r
   if (NewVariable != NULL) {\r
   // Install the new variable if it is not NULL.\r
   //\r
   if (NewVariable != NULL) {\r
-    if ((UINTN) (CurrPtr - ValidBuffer) + NewVariableSize > VariableStoreHeader->Size) {\r
+    if (((UINTN) CurrPtr - (UINTN) ValidBuffer) + NewVariableSize > VariableStoreHeader->Size) {\r
       //\r
       // No enough space to store the new variable.\r
       //\r
       //\r
       // No enough space to store the new variable.\r
       //\r
@@ -1211,8 +1211,8 @@ Reclaim (
     // If volatile variable store, just copy valid buffer.\r
     //\r
     SetMem ((UINT8 *) (UINTN) VariableBase, VariableStoreHeader->Size, 0xff);\r
     // If volatile variable store, just copy valid buffer.\r
     //\r
     SetMem ((UINT8 *) (UINTN) VariableBase, VariableStoreHeader->Size, 0xff);\r
-    CopyMem ((UINT8 *) (UINTN) VariableBase, ValidBuffer, (UINTN) (CurrPtr - ValidBuffer));\r
-    *LastVariableOffset = (UINTN) (CurrPtr - ValidBuffer);\r
+    CopyMem ((UINT8 *) (UINTN) VariableBase, ValidBuffer, (UINTN) CurrPtr - (UINTN) ValidBuffer);\r
+    *LastVariableOffset = (UINTN) CurrPtr - (UINTN) ValidBuffer;\r
     Status  = EFI_SUCCESS;\r
   } else {\r
     //\r
     Status  = EFI_SUCCESS;\r
   } else {\r
     //\r
@@ -1223,7 +1223,7 @@ Reclaim (
               (VARIABLE_STORE_HEADER *) ValidBuffer\r
               );\r
     if (!EFI_ERROR (Status)) {\r
               (VARIABLE_STORE_HEADER *) ValidBuffer\r
               );\r
     if (!EFI_ERROR (Status)) {\r
-      *LastVariableOffset = (UINTN) (CurrPtr - ValidBuffer);\r
+      *LastVariableOffset = (UINTN) CurrPtr - (UINTN) ValidBuffer;\r
       mVariableModuleGlobal->HwErrVariableTotalSize = HwErrVariableTotalSize;\r
       mVariableModuleGlobal->CommonVariableTotalSize = CommonVariableTotalSize;\r
       mVariableModuleGlobal->CommonUserVariableTotalSize = CommonUserVariableTotalSize;\r
       mVariableModuleGlobal->HwErrVariableTotalSize = HwErrVariableTotalSize;\r
       mVariableModuleGlobal->CommonVariableTotalSize = CommonVariableTotalSize;\r
       mVariableModuleGlobal->CommonUserVariableTotalSize = CommonUserVariableTotalSize;\r