]> git.proxmox.com Git - mirror_edk2.git/commit - MdeModulePkg/Core/Dxe/Image/Image.c
MdeModulePkg: Refine casting expression result to bigger size
authorHao Wu <hao.a.wu@intel.com>
Fri, 24 Feb 2017 02:01:34 +0000 (10:01 +0800)
committerHao Wu <hao.a.wu@intel.com>
Mon, 6 Mar 2017 06:33:20 +0000 (14:33 +0800)
commit16f6922709952c7ad468dcdee6ef94b3e5a3cd90
tree999456e2e6812457d5c0b32b969ac0eed2b02149
parent95ba3d92dca2616715e2af89d2bbeca9577a3e2c
MdeModulePkg: Refine casting expression result to bigger size

There are cases that the operands of an expression are all with rank less
than UINT64/INT64 and the result of the expression is explicitly cast to
UINT64/INT64 to fit the target size.

An example will be:
UINT32 a,b;
// a and b can be any unsigned int type with rank less than UINT64, like
// UINT8, UINT16, etc.
UINT64 c;
c = (UINT64) (a + b);

Some static code checkers may warn that the expression result might
overflow within the rank of "int" (integer promotions) and the result is
then cast to a bigger size.

The commit refines codes by the following rules:
1). When the expression is possible to overflow the range of unsigned int/
int:
c = (UINT64)a + b;

2). When the expression will not overflow within the rank of "int", remove
the explicit type casts:
c = a + b;

3). When the expression will be cast to pointer of possible greater size:
UINT32 a,b;
VOID *c;
c = (VOID *)(UINTN)(a + b); --> c = (VOID *)((UINTN)a + b);

4). When one side of a comparison expression contains only operands with
rank less than UINT32:
UINT8 a;
UINT16 b;
UINTN c;
if ((UINTN)(a + b) > c) {...} --> if (((UINT32)a + b) > c) {...}

For rule 4), if we remove the 'UINTN' type cast like:
if (a + b > c) {...}
The VS compiler will complain with warning C4018 (signed/unsigned
mismatch, level 3 warning) due to promoting 'a + b' to type 'int'.

Contributed-under: TianoCore Contribution Agreement 1.0
Signed-off-by: Hao Wu <hao.a.wu@intel.com>
Reviewed-by: Feng Tian <feng.tian@intel.com>
34 files changed:
MdeModulePkg/Application/UiApp/FrontPage.c
MdeModulePkg/Bus/Pci/EhciDxe/EhciReg.c
MdeModulePkg/Bus/Pci/IdeBusPei/AtapiPeim.c
MdeModulePkg/Bus/Pci/PciBusDxe/PciOptionRomSupport.c
MdeModulePkg/Bus/Pci/XhciDxe/XhciReg.c
MdeModulePkg/Bus/Ufs/UfsBlockIoPei/UfsHci.c
MdeModulePkg/Bus/Ufs/UfsPassThruDxe/UfsPassThruHci.c
MdeModulePkg/Core/Dxe/Image/Image.c
MdeModulePkg/Core/Dxe/Misc/DebugImageInfo.c
MdeModulePkg/Core/Pei/Image/Image.c
MdeModulePkg/Core/PiSmmCore/Dispatcher.c
MdeModulePkg/Core/PiSmmCore/PiSmmIpl.c
MdeModulePkg/Library/DxeCapsuleLibFmp/DxeCapsuleLib.c
MdeModulePkg/Library/DxeCoreMemoryAllocationLib/MemoryAllocationLib.c
MdeModulePkg/Library/DxeNetLib/DxeNetLib.c
MdeModulePkg/Library/PiDxeS3BootScriptLib/BootScriptSave.c
MdeModulePkg/Library/PiSmmCoreMemoryAllocationLib/MemoryAllocationLib.c
MdeModulePkg/Library/SmmMemoryAllocationProfileLib/MemoryAllocationLib.c
MdeModulePkg/Library/UefiBootManagerLib/BmMisc.c
MdeModulePkg/Library/UefiHiiLib/HiiLib.c
MdeModulePkg/Library/UefiMemoryAllocationProfileLib/MemoryAllocationLib.c
MdeModulePkg/Library/VarCheckHiiLib/VarCheckHiiLibNullClass.c
MdeModulePkg/Universal/Acpi/BootScriptExecutorDxe/ScriptExecute.c
MdeModulePkg/Universal/Acpi/S3SaveStateDxe/AcpiS3ContextSave.c
MdeModulePkg/Universal/CapsulePei/Common/CapsuleCoalesce.c
MdeModulePkg/Universal/DisplayEngineDxe/FormDisplay.c
MdeModulePkg/Universal/EbcDxe/EbcExecute.c
MdeModulePkg/Universal/FaultTolerantWriteDxe/UpdateWorkingBlock.c
MdeModulePkg/Universal/HiiDatabaseDxe/Font.c
MdeModulePkg/Universal/Network/UefiPxeBcDxe/PxeBcImpl.c
MdeModulePkg/Universal/PCD/Dxe/Service.c
MdeModulePkg/Universal/PCD/Pei/Service.c
MdeModulePkg/Universal/SmbiosDxe/SmbiosDxe.c
MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c