From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mga03.intel.com (mga03.intel.com [134.134.136.65]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ml01.01.org (Postfix) with ESMTPS id CF96B8213A for ; Fri, 24 Feb 2017 21:12:50 -0800 (PST) Received: from orsmga002.jf.intel.com ([10.7.209.21]) by orsmga103.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 24 Feb 2017 21:12:50 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.35,202,1484035200"; d="scan'208";a="52623994" Received: from shwdeopenpsi014.ccr.corp.intel.com ([10.239.9.13]) by orsmga002.jf.intel.com with ESMTP; 24 Feb 2017 21:12:49 -0800 From: Hao Wu To: edk2-devel@lists.01.org Cc: Hao Wu Date: Sat, 25 Feb 2017 13:12:23 +0800 Message-Id: <1487999555-9764-1-git-send-email-hao.a.wu@intel.com> X-Mailer: git-send-email 1.9.5.msysgit.0 Subject: [PATCH v3 00/12] Refine casting expression result to bigger size X-BeenThere: edk2-devel@lists.01.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: EDK II Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 25 Feb 2017 05:12:51 -0000 V3: Send the full patch serires for review. The series is also available at: https://github.com/hwu25/edk2 branch: refine_cast_v3 This version of series also add two new rules (3 & 4) when refining type casts: 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'. V2: Follow the below rules to refine codes: 1). When the expression will not overflow within the rank of "int", remove the explicit type casts: c = a + b; 2). When the expression is possible to overflow the range of unsigned int/ int: c = (UINT64)a + b; V1: There are cases that the operands of an expression are all with rank less than UINT64/INT64 and the result of the expression is casted 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. For the consideration of generated binaries size, the commit will keep the size of the operands as the size of int, and explitly add a type cast before converting the result to UINT64/INT64. 1). When there is no operand with type UINTN (UINTN) (a + b) -> (UINTN)(UINT32) (a + b) or (UINT64) (a + b) -> (UINT64)(UINT32) (a + b) 2). Otherwise (UINT64) (a + b) -> (UINT64)(UINTN) (a + b) Hao Wu (12): MdePkg: Refine casting expression result to bigger size MdeModulePkg: Refine casting expression result to bigger size FatPkg: Refine casting expression result to bigger size IntelFrameworkModulePkg: Refine casting expression result to bigger size IntelFsp2WrapperPkg: Refine casting expression result to bigger size IntelFspWrapperPkg: Refine casting expression result to bigger size NetworkPkg: Refine casting expression result to bigger size PcAtChipsetPkg: Refine casting expression result to bigger size SecurityPkg/Opal: Refine casting expression result to bigger size ShellPkg: Refine casting expression result to bigger size SourceLevelDebugPkg: Refine casting expression result to bigger size UefiCpuPkg: Refine casting expression result to bigger size FatPkg/EnhancedFatDxe/ReadWrite.c | 4 ++-- FatPkg/FatPei/FatLiteAccess.c | 4 ++-- IntelFrameworkModulePkg/Csm/BiosThunk/KeyboardDxe/BiosKeyboard.c | 4 ++-- IntelFrameworkModulePkg/Csm/BiosThunk/Snp16Dxe/BiosSnp16.c | 4 ++-- IntelFrameworkModulePkg/Csm/BiosThunk/Snp16Dxe/Misc.c | 14 +++++++------- IntelFrameworkModulePkg/Csm/BiosThunk/VideoDxe/BiosVideo.c | 4 ++-- IntelFrameworkModulePkg/Csm/LegacyBiosDxe/LegacyBios.c | 6 +++--- IntelFrameworkModulePkg/Csm/LegacyBiosDxe/LegacyBootSupport.c | 4 ++-- IntelFrameworkModulePkg/Library/GenericBdsLib/BdsBoot.c | 2 +- IntelFrameworkModulePkg/Library/GenericBdsLib/BdsMisc.c | 4 ++-- IntelFrameworkModulePkg/Library/LegacyBootManagerLib/LegacyBm.c | 2 +- IntelFrameworkModulePkg/Universal/BdsDxe/FrontPage.c | 4 ++-- IntelFrameworkModulePkg/Universal/CpuIoDxe/CpuIo.c | 4 ++-- IntelFsp2WrapperPkg/FspWrapperNotifyDxe/LoadBelow4G.c | 4 ++-- IntelFsp2WrapperPkg/Library/BaseFspWrapperApiLib/FspWrapperApiLib.c | 10 +++++----- IntelFspWrapperPkg/FspNotifyDxe/LoadBelow4G.c | 4 ++-- IntelFspWrapperPkg/Library/BaseFspApiLib/FspApiLib.c | 12 ++++++------ MdeModulePkg/Application/UiApp/FrontPage.c | 4 ++-- MdeModulePkg/Bus/Pci/EhciDxe/EhciReg.c | 8 ++++---- MdeModulePkg/Bus/Pci/IdeBusPei/AtapiPeim.c | 4 ++-- MdeModulePkg/Bus/Pci/PciBusDxe/PciOptionRomSupport.c | 2 +- MdeModulePkg/Bus/Pci/XhciDxe/XhciReg.c | 20 ++++++++++---------- MdeModulePkg/Bus/Ufs/UfsBlockIoPei/UfsHci.c | 6 +++--- MdeModulePkg/Bus/Ufs/UfsPassThruDxe/UfsPassThruHci.c | 6 +++--- MdeModulePkg/Core/Dxe/Image/Image.c | 14 ++++++-------- MdeModulePkg/Core/Dxe/Misc/DebugImageInfo.c | 4 ++-- MdeModulePkg/Core/Pei/Image/Image.c | 12 +++++------- MdeModulePkg/Core/PiSmmCore/Dispatcher.c | 18 ++++++++---------- MdeModulePkg/Core/PiSmmCore/PiSmmIpl.c | 12 +++++------- MdeModulePkg/Library/DxeCapsuleLibFmp/DxeCapsuleLib.c | 4 ++-- MdeModulePkg/Library/DxeCoreMemoryAllocationLib/MemoryAllocationLib.c | 4 ++-- MdeModulePkg/Library/DxeNetLib/DxeNetLib.c | 4 ++-- MdeModulePkg/Library/PiDxeS3BootScriptLib/BootScriptSave.c | 4 ++-- MdeModulePkg/Library/PiSmmCoreMemoryAllocationLib/MemoryAllocationLib.c | 4 ++-- MdeModulePkg/Library/SmmMemoryAllocationProfileLib/MemoryAllocationLib.c | 4 ++-- MdeModulePkg/Library/UefiBootManagerLib/BmMisc.c | 4 ++-- MdeModulePkg/Library/UefiHiiLib/HiiLib.c | 4 ++-- MdeModulePkg/Library/UefiMemoryAllocationProfileLib/MemoryAllocationLib.c | 4 ++-- MdeModulePkg/Library/VarCheckHiiLib/VarCheckHiiLibNullClass.c | 6 +++--- MdeModulePkg/Universal/Acpi/BootScriptExecutorDxe/ScriptExecute.c | 4 ++-- MdeModulePkg/Universal/Acpi/S3SaveStateDxe/AcpiS3ContextSave.c | 6 +++--- MdeModulePkg/Universal/CapsulePei/Common/CapsuleCoalesce.c | 4 ++-- MdeModulePkg/Universal/DisplayEngineDxe/FormDisplay.c | 8 ++++---- MdeModulePkg/Universal/EbcDxe/EbcExecute.c | 10 +++++----- MdeModulePkg/Universal/FaultTolerantWriteDxe/UpdateWorkingBlock.c | 4 ++-- MdeModulePkg/Universal/HiiDatabaseDxe/Font.c | 20 ++++++++++---------- MdeModulePkg/Universal/Network/UefiPxeBcDxe/PxeBcImpl.c | 6 +++--- MdeModulePkg/Universal/PCD/Dxe/Service.c | 4 ++-- MdeModulePkg/Universal/PCD/Pei/Service.c | 4 ++-- MdeModulePkg/Universal/SmbiosDxe/SmbiosDxe.c | 6 +++--- MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c | 6 +++--- MdePkg/Library/BaseLib/String.c | 4 ++-- MdePkg/Library/BasePeCoffLib/BasePeCoff.c | 12 +++++------- MdePkg/Library/BaseS3PciLib/S3PciLib.c | 4 ++-- MdePkg/Library/SmmMemoryAllocationLib/MemoryAllocationLib.c | 4 ++-- MdePkg/Library/UefiMemoryAllocationLib/MemoryAllocationLib.c | 4 ++-- NetworkPkg/IpSecDxe/Ikev2/Payload.c | 4 ++-- NetworkPkg/IpSecDxe/IpSecConfigImpl.c | 8 ++++---- NetworkPkg/IpSecDxe/IpSecConfigImpl.h | 4 ++-- NetworkPkg/Mtftp6Dxe/Mtftp6Support.c | 4 ++-- NetworkPkg/UefiPxeBcDxe/PxeBcImpl.c | 10 +++++----- PcAtChipsetPkg/Library/SerialIoLib/SerialPortLib.c | 18 +++++++++--------- SecurityPkg/Tcg/Opal/OpalPasswordSmm/OpalNvmeMode.c | 4 ++-- ShellPkg/Library/UefiShellDebug1CommandsLib/HexEdit/BufferImage.c | 8 ++++---- ShellPkg/Library/UefiShellDebug1CommandsLib/UefiShellDebug1CommandsLib.c | 4 ++-- ShellPkg/Library/UefiShellLib/UefiShellLib.c | 4 ++-- SourceLevelDebugPkg/Library/DebugAgent/DxeDebugAgent/DxeDebugAgentLib.c | 6 +++--- SourceLevelDebugPkg/Library/DebugAgent/SecPeiDebugAgent/SecPeiDebugAgentLib.c | 8 ++++---- SourceLevelDebugPkg/Library/DebugAgent/SmmDebugAgent/SmmDebugAgentLib.c | 6 +++--- SourceLevelDebugPkg/Library/DebugCommunicationLibUsb/DebugCommunicationLibUsb.c | 18 +++++++++--------- UefiCpuPkg/CpuIo2Dxe/CpuIo2Dxe.c | 4 ++-- UefiCpuPkg/CpuIo2Smm/CpuIo2Smm.c | 4 ++-- UefiCpuPkg/Library/SmmCpuFeaturesLib/SmmStm.c | 8 ++++---- UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuDxeSmm.c | 4 ++-- UefiCpuPkg/PiSmmCpuDxeSmm/SmramSaveState.c | 6 +++--- 75 files changed, 237 insertions(+), 247 deletions(-) -- 1.9.5.msysgit.0