mirror_edk2/ShellPkg/Library/UefiShellDebug1CommandsLib
Hao Wu 22454f1042 ShellPkg: 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: Ruiyu Ni <ruiyu.ni@intel.com>
Reviewed-by: Jaben Carsey <jaben.carsey@intel.com>
2017-03-06 14:33:25 +08:00
..
Edit ShellPkg/UefiShellDebug1CommandsLib: Remove unnecessary EFIAPI 2016-10-09 10:27:47 +08:00
HexEdit ShellPkg: Refine casting expression result to bigger size 2017-03-06 14:33:25 +08:00
SmbiosView ShellPkg: Refine type cast for pointer subtraction 2017-03-06 14:16:00 +08:00
Comp.c ShellPkg/comp: Fix GCC build failure 2017-02-28 16:05:32 +08:00
Compress.c ShellPkg: Update sources to include MdePkg protocol definitions 2016-10-19 10:26:16 +08:00
Compress.h Refine comments and two code style. 2011-09-02 08:05:34 +00:00
Dblk.c ShellPkg/UefiShellDebug1CommandsLib: Remove unnecessary EFIAPI 2016-10-09 10:27:47 +08:00
Dmem.c ShellPkg/UefiShellDebug1CommandsLib: Remove unnecessary EFIAPI 2016-10-09 10:27:47 +08:00
DmpStore.c ShellPkg: Refine type cast for pointer subtraction 2017-03-06 14:16:00 +08:00
EditInputBar.c ShellPkg/UefiShellDebug1CommandsLib: Remove unnecessary EFIAPI 2016-10-09 10:27:47 +08:00
EditInputBar.h ShellPkg/UefiShellDebug1CommandsLib: Remove unnecessary EFIAPI 2016-10-09 10:27:47 +08:00
EditMenuBar.c ShellPkg/UefiShellDebug1CommandsLib: Remove unnecessary EFIAPI 2016-10-09 10:27:47 +08:00
EditMenuBar.h ShellPkg/UefiShellDebug1CommandsLib: Remove unnecessary EFIAPI 2016-10-09 10:27:47 +08:00
EditStatusBar.c ShellPkg/UefiShellDebug1CommandsLib: Remove unnecessary EFIAPI 2016-10-09 10:27:47 +08:00
EditStatusBar.h ShellPkg/UefiShellDebug1CommandsLib: Remove unnecessary EFIAPI 2016-10-09 10:27:47 +08:00
EditTitleBar.c ShellPkg/UefiShellDebug1CommandsLib: Remove unnecessary EFIAPI 2016-10-09 10:27:47 +08:00
EditTitleBar.h ShellPkg/UefiShellDebug1CommandsLib: Remove unnecessary EFIAPI 2016-10-09 10:27:47 +08:00
EfiCompress.c ShellPkg/EfiCompress: Handle memory allocation failure 2016-07-18 10:55:26 +08:00
EfiDecompress.c ShellPkg: Fix VS2010/2012 build failure 2016-07-19 11:12:22 +08:00
LoadPciRom.c ShellPkg/UefiShellDebug1CommandsLib: Remove unnecessary EFIAPI 2016-10-09 10:27:47 +08:00
MemMap.c ShellPkg: Add Persistent Memory support in 'memmap' command 2016-09-22 14:17:07 -07:00
Mm.c ShellPkg/UefiShellDebug1CommandsLib: Remove unnecessary EFIAPI 2016-10-09 10:27:47 +08:00
Mode.c ShellPkg: Standardized HP Copyright Message String 2015-02-04 22:25:01 +00:00
Pci.c ShellPkg/pci: Report error when invalid value is specified for "-ec" 2017-02-16 13:40:28 +08:00
Pci.h ShellPkg/pci: Use PCI definitions defined in MdePkg 2017-01-25 16:09:05 +08:00
SerMode.c ShellPkg/UefiShellDebug1CommandsLib: Remove unnecessary EFIAPI 2016-10-09 10:27:47 +08:00
SetSize.c ShellPkg: Standardized HP Copyright Message String 2015-02-04 22:25:01 +00:00
SetVar.c ShellPkg/Debug1CommandLib: Use StrToGuid/StrHexToBytes in BaseLib 2017-03-01 13:39:46 +08:00
UefiShellDebug1CommandsLib.c ShellPkg/Debug1CommandLib: Use StrToGuid/StrHexToBytes in BaseLib 2017-03-01 13:39:46 +08:00
UefiShellDebug1CommandsLib.h ShellPkg/Debug1CommandLib: Use StrToGuid/StrHexToBytes in BaseLib 2017-03-01 13:39:46 +08:00
UefiShellDebug1CommandsLib.inf ShellPkg: Update Guid/Protocol usages in INF files. 2016-04-13 14:52:59 +08:00
UefiShellDebug1CommandsLib.uni ShellPkg/comp: Add "-n <diff-count>"/"-s <diff-byte>" support 2017-02-28 10:44:05 +08:00