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>
/** @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
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
(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
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
- 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
//\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
// 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
// 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
/** @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
// 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
/** @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
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
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
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
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
//\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
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
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
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
// 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
// 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
(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