public inbox for devel@edk2.groups.io
 help / color / mirror / Atom feed
From: Hao Wu <hao.a.wu@intel.com>
To: edk2-devel@lists.01.org
Cc: Hao Wu <hao.a.wu@intel.com>
Subject: [PATCH v3 00/12] Refine casting expression result to bigger size
Date: Sat, 25 Feb 2017 13:12:23 +0800	[thread overview]
Message-ID: <1487999555-9764-1-git-send-email-hao.a.wu@intel.com> (raw)

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



             reply	other threads:[~2017-02-25  5:12 UTC|newest]

Thread overview: 25+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-02-25  5:12 Hao Wu [this message]
2017-02-25  5:12 ` [PATCH v3 01/12] MdePkg: Refine casting expression result to bigger size Hao Wu
2017-02-25  5:12 ` [PATCH v3 02/12] MdeModulePkg: " Hao Wu
2017-03-06  1:37   ` Tian, Feng
2017-02-25  5:12 ` [PATCH v3 03/12] FatPkg: " Hao Wu
2017-02-27  5:07   ` Ni, Ruiyu
2017-02-25  5:12 ` [PATCH v3 04/12] IntelFrameworkModulePkg: " Hao Wu
2017-02-27  7:06   ` Fan, Jeff
2017-02-25  5:12 ` [PATCH v3 05/12] IntelFsp2WrapperPkg: " Hao Wu
2017-02-25  5:51   ` Yao, Jiewen
2017-02-25  5:12 ` [PATCH v3 06/12] IntelFspWrapperPkg: " Hao Wu
2017-02-25  5:51   ` Yao, Jiewen
2017-02-25  5:12 ` [PATCH v3 07/12] NetworkPkg: " Hao Wu
2017-02-27  2:21   ` Wu, Jiaxin
2017-02-25  5:12 ` [PATCH v3 08/12] PcAtChipsetPkg: " Hao Wu
2017-02-27  7:24   ` Ni, Ruiyu
2017-02-25  5:12 ` [PATCH v3 09/12] SecurityPkg/Opal: " Hao Wu
2017-03-06  1:40   ` Dong, Eric
2017-02-25  5:12 ` [PATCH v3 10/12] ShellPkg: " Hao Wu
2017-02-27  2:22   ` Ni, Ruiyu
2017-02-27 16:38   ` Carsey, Jaben
2017-02-25  5:12 ` [PATCH v3 11/12] SourceLevelDebugPkg: " Hao Wu
2017-02-27  7:27   ` Fan, Jeff
2017-02-25  5:12 ` [PATCH v3 12/12] UefiCpuPkg: " Hao Wu
2017-02-27  7:27   ` Fan, Jeff

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-list from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1487999555-9764-1-git-send-email-hao.a.wu@intel.com \
    --to=devel@edk2.groups.io \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox