MdeModulePkg: Refine type cast for pointer subtraction

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>
This commit is contained in:
Hao Wu 2017-01-23 11:56:05 +08:00
parent 588bb5ae52
commit 809e2bbf41
11 changed files with 24 additions and 24 deletions

View File

@ -1,7 +1,7 @@
/** @file
PCI Rom supporting funtions implementation for PCI Bus module.
Copyright (c) 2006 - 2015, Intel Corporation. All rights reserved.<BR>
Copyright (c) 2006 - 2017, Intel Corporation. All rights reserved.<BR>
This program and the accompanying materials
are licensed and made available under the terms and conditions of the BSD License
which accompanies this distribution. The full text of the license may be found at
@ -776,7 +776,7 @@ ProcessOpRomImage (
NextImage:
RomBarOffset += ImageSize;
} while (((Indicator & 0x80) == 0x00) && ((UINTN) (RomBarOffset - (UINT8 *) RomBar) < PciDevice->RomSize));
} while (((Indicator & 0x80) == 0x00) && (((UINTN) RomBarOffset - (UINTN) RomBar) < PciDevice->RomSize));
return RetStatus;
}

View File

@ -1607,10 +1607,10 @@ typedef struct {
(sizeof (NET_BUF) + ((BlockOpNum) - 1) * sizeof (NET_BLOCK_OP))
#define NET_HEADSPACE(BlockOp) \
(UINTN)((BlockOp)->Head - (BlockOp)->BlockHead)
((UINTN)((BlockOp)->Head) - (UINTN)((BlockOp)->BlockHead))
#define NET_TAILSPACE(BlockOp) \
(UINTN)((BlockOp)->BlockTail - (BlockOp)->Tail)
((UINTN)((BlockOp)->BlockTail) - (UINTN)((BlockOp)->Tail))
/**
Allocate a single block NET_BUF. Upon allocation, all the

View File

@ -10,7 +10,7 @@
ValidateFmpCapsule(), DisplayCapsuleImage(), ConvertBmpToGopBlt() will
receive untrusted input and do basic validation.
Copyright (c) 2016, Intel Corporation. All rights reserved.<BR>
Copyright (c) 2016 - 2017, Intel Corporation. All rights reserved.<BR>
This program and the accompanying materials
are licensed and made available under the terms and conditions of the BSD License
which accompanies this distribution. The full text of the license may be found at
@ -547,7 +547,7 @@ ConvertBmpToGopBlt (
}
ImageIndex = (UINTN) (Image - ImageHeader);
ImageIndex = (UINTN) Image - (UINTN) ImageHeader;
if ((ImageIndex % 4) != 0) {
//
// Bmp Image starts each row on a 32-bit boundary!

View File

@ -226,7 +226,7 @@ DxePrintLibPrint2ProtocolVaListToBaseList (
//
// If BASE_LIST is larger than Size, then return FALSE
//
if ((UINTN)((UINT8 *)BaseListMarker - (UINT8 *)BaseListStart) > Size) {
if (((UINTN)BaseListMarker - (UINTN)BaseListStart) > Size) {
DEBUG ((DEBUG_ERROR, "The input variable argument list is too long. Please consider breaking into multiple print calls.\n"));
return FALSE;
}

View File

@ -728,7 +728,7 @@ LibAppendFileName (
// that overlap.
//
StrCpyS (TmpStr, MaxLen, Ptr + 3);
StrCpyS (LastSlash, MaxLen - (UINTN) (LastSlash - Str), TmpStr);
StrCpyS (LastSlash, MaxLen - ((UINTN) LastSlash - (UINTN) Str) / sizeof (CHAR16), TmpStr);
Ptr = LastSlash;
} else if (*Ptr == '\\' && *(Ptr + 1) == '.' && *(Ptr + 2) == '\\') {
//
@ -740,7 +740,7 @@ LibAppendFileName (
// that overlap.
//
StrCpyS (TmpStr, MaxLen, Ptr + 2);
StrCpyS (Ptr, MaxLen - (UINTN) (Ptr - Str), TmpStr);
StrCpyS (Ptr, MaxLen - ((UINTN) Ptr - (UINTN) Str) / sizeof (CHAR16), TmpStr);
Ptr = LastSlash;
} else if (*Ptr == '\\') {
LastSlash = Ptr;

View File

@ -1,7 +1,7 @@
/** @file
Save the S3 data to S3 boot script.
Copyright (c) 2006 - 2016, Intel Corporation. All rights reserved.<BR>
Copyright (c) 2006 - 2017, Intel Corporation. All rights reserved.<BR>
This program and the accompanying materials
are licensed and made available under the terms and conditions
@ -2025,7 +2025,7 @@ S3BootScriptCalculateInsertAddress (
// calculate the Position offset
//
if (Position != NULL) {
PositionOffset = (UINTN) ((UINT8 *)Position - S3TableBase);
PositionOffset = (UINTN)Position - (UINTN)S3TableBase;
//
// If the BeforeOrAfter is FALSE, that means to insert the node right after the node.

View File

@ -1,7 +1,7 @@
/** @file
Load option library functions which relate with creating and processing load options.
Copyright (c) 2011 - 2016, Intel Corporation. All rights reserved.<BR>
Copyright (c) 2011 - 2017, Intel Corporation. All rights reserved.<BR>
(C) Copyright 2015-2016 Hewlett Packard Enterprise Development LP<BR>
This program and the accompanying materials
are licensed and made available under the terms and conditions of the BSD License
@ -912,7 +912,7 @@ EfiBootManagerVariableToLoadOptionEx (
FilePath = (EFI_DEVICE_PATH_PROTOCOL *) VariablePtr;
VariablePtr += FilePathSize;
OptionalDataSize = (UINT32) (VariableSize - (UINTN) (VariablePtr - Variable));
OptionalDataSize = (UINT32) (VariableSize - ((UINTN) VariablePtr - (UINTN) Variable));
if (OptionalDataSize == 0) {
OptionalData = NULL;
} else {

View File

@ -4,7 +4,7 @@
ALL CODE IN THE SERIALIO STACK MUST BE RE-ENTRANT AND CALLABLE FROM
INTERRUPT CONTEXT
Copyright (c) 2006 - 2014, Intel Corporation. All rights reserved.<BR>
Copyright (c) 2006 - 2017, Intel Corporation. All rights reserved.<BR>
This program and the accompanying materials
are licensed and made available under the terms and conditions of the BSD License
which accompanies this distribution. The full text of the license may be found at
@ -569,7 +569,7 @@ DebugPortRead (
LocalBufferSize = *BufferSize - (BufferPtr - (UINT8 *) Buffer);
} while (LocalBufferSize != 0 && Timeout > 0);
*BufferSize = (UINTN) (BufferPtr - (UINT8 *) Buffer);
*BufferSize = (UINTN) BufferPtr - (UINTN) Buffer;
return Status;
}

View File

@ -3,7 +3,7 @@
These are the common Fault Tolerant Write (FTW) functions that are shared
by DXE FTW driver and SMM FTW driver.
Copyright (c) 2006 - 2015, Intel Corporation. All rights reserved.<BR>
Copyright (c) 2006 - 2017, Intel Corporation. All rights reserved.<BR>
This program and the accompanying materials
are licensed and made available under the terms and conditions of the BSD License
which accompanies this distribution. The full text of the license may be found at
@ -373,7 +373,7 @@ FtwWrite (
//
// If Record is out of the range of Header, return access denied.
//
if (((UINTN)((UINT8 *) Record - (UINT8 *) Header)) > FTW_WRITE_TOTAL_SIZE (Header->NumberOfWrites - 1, Header->PrivateDataSize)) {
if (((UINTN) Record - (UINTN) Header) > FTW_WRITE_TOTAL_SIZE (Header->NumberOfWrites - 1, Header->PrivateDataSize)) {
return EFI_ACCESS_DENIED;
}

View File

@ -2,7 +2,7 @@
Implementation for EFI_HII_IMAGE_PROTOCOL.
Copyright (c) 2007 - 2016, Intel Corporation. All rights reserved.<BR>
Copyright (c) 2007 - 2017, Intel Corporation. All rights reserved.<BR>
This program and the accompanying materials
are licensed and made available under the terms and conditions of the BSD License
which accompanies this distribution. The full text of the license may be found at
@ -1124,7 +1124,7 @@ HiiSetImage (
return EFI_OUT_OF_RESOURCES;
}
Part1Size = (UINT32) (UINTN) ((UINT8 *) CurrentImageBlock - (UINT8 *) ImagePackage->ImageBlock);
Part1Size = (UINT32) ((UINTN) CurrentImageBlock - (UINTN) ImagePackage->ImageBlock);
Part2Size = ImagePackage->ImageBlockSize - Part1Size - OldBlockSize;
CopyMem (ImageBlocks, ImagePackage->ImageBlock, Part1Size);

View File

@ -16,7 +16,7 @@
VariableServiceSetVariable() should also check authenticate data to avoid buffer overflow,
integer overflow. It should also check attribute to avoid authentication bypass.
Copyright (c) 2006 - 2016, Intel Corporation. All rights reserved.<BR>
Copyright (c) 2006 - 2017, Intel Corporation. All rights reserved.<BR>
(C) Copyright 2015 Hewlett Packard Enterprise Development LP<BR>
This program and the accompanying materials
are licensed and made available under the terms and conditions of the BSD License
@ -1170,7 +1170,7 @@ Reclaim (
// Install the new variable if it is not NULL.
//
if (NewVariable != NULL) {
if ((UINTN) (CurrPtr - ValidBuffer) + NewVariableSize > VariableStoreHeader->Size) {
if (((UINTN) CurrPtr - (UINTN) ValidBuffer) + NewVariableSize > VariableStoreHeader->Size) {
//
// No enough space to store the new variable.
//
@ -1211,8 +1211,8 @@ Reclaim (
// If volatile variable store, just copy valid buffer.
//
SetMem ((UINT8 *) (UINTN) VariableBase, VariableStoreHeader->Size, 0xff);
CopyMem ((UINT8 *) (UINTN) VariableBase, ValidBuffer, (UINTN) (CurrPtr - ValidBuffer));
*LastVariableOffset = (UINTN) (CurrPtr - ValidBuffer);
CopyMem ((UINT8 *) (UINTN) VariableBase, ValidBuffer, (UINTN) CurrPtr - (UINTN) ValidBuffer);
*LastVariableOffset = (UINTN) CurrPtr - (UINTN) ValidBuffer;
Status = EFI_SUCCESS;
} else {
//
@ -1223,7 +1223,7 @@ Reclaim (
(VARIABLE_STORE_HEADER *) ValidBuffer
);
if (!EFI_ERROR (Status)) {
*LastVariableOffset = (UINTN) (CurrPtr - ValidBuffer);
*LastVariableOffset = (UINTN) CurrPtr - (UINTN) ValidBuffer;
mVariableModuleGlobal->HwErrVariableTotalSize = HwErrVariableTotalSize;
mVariableModuleGlobal->CommonVariableTotalSize = CommonVariableTotalSize;
mVariableModuleGlobal->CommonUserVariableTotalSize = CommonUserVariableTotalSize;