]> git.proxmox.com Git - mirror_edk2.git/commitdiff
IntelFrameworkModulePkg: Refine casting expression result to bigger size
authorHao Wu <hao.a.wu@intel.com>
Mon, 23 Jan 2017 03:24:50 +0000 (11:24 +0800)
committerHao Wu <hao.a.wu@intel.com>
Mon, 6 Mar 2017 06:33:22 +0000 (14:33 +0800)
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: Jeff Fan <jeff.fan@intel.com>
IntelFrameworkModulePkg/Csm/BiosThunk/KeyboardDxe/BiosKeyboard.c
IntelFrameworkModulePkg/Csm/BiosThunk/Snp16Dxe/BiosSnp16.c
IntelFrameworkModulePkg/Csm/BiosThunk/Snp16Dxe/Misc.c
IntelFrameworkModulePkg/Csm/BiosThunk/VideoDxe/BiosVideo.c
IntelFrameworkModulePkg/Csm/LegacyBiosDxe/LegacyBios.c
IntelFrameworkModulePkg/Csm/LegacyBiosDxe/LegacyBootSupport.c
IntelFrameworkModulePkg/Library/GenericBdsLib/BdsBoot.c
IntelFrameworkModulePkg/Library/GenericBdsLib/BdsMisc.c
IntelFrameworkModulePkg/Library/LegacyBootManagerLib/LegacyBm.c
IntelFrameworkModulePkg/Universal/BdsDxe/FrontPage.c
IntelFrameworkModulePkg/Universal/CpuIoDxe/CpuIo.c

index a597d99a9755954ff04abc254707acaa387717ec..742d00982f5cc3fe1f435282c7ca8bc15d3722f5 100644 (file)
@@ -1,7 +1,7 @@
 /** @file\r
   ConsoleOut Routines that speak VGA.\r
 \r
-Copyright (c) 2006 - 2016, Intel Corporation. All rights reserved.<BR>\r
+Copyright (c) 2006 - 2017, Intel Corporation. All rights reserved.<BR>\r
 \r
 This program and the accompanying materials\r
 are licensed and made available under the terms and conditions\r
@@ -418,7 +418,7 @@ BiosKeyboardDriverBindingStart (
     // Check bit 6 of Feature Byte 2.\r
     // If it is set, then Int 16 Func 09 is supported\r
     //\r
-    if (*(UINT8 *)(UINTN) ((Regs.X.ES << 4) + Regs.X.BX + 0x06) & 0x40) {\r
+    if (*(UINT8 *) (((UINTN) Regs.X.ES << 4) + Regs.X.BX + 0x06) & 0x40) {\r
       //\r
       // Get Keyboard Functionality\r
       //\r
index a2a7797f0748e13608a1cbcbfa221b446a1ad5ca..b586a91c7badbbe6fa166e4daa012c0c0025d095 100644 (file)
@@ -1,6 +1,6 @@
 /** @file\r
 \r
-Copyright (c) 1999 - 2014, Intel Corporation. All rights reserved.<BR>\r
+Copyright (c) 1999 - 2017, Intel Corporation. All rights reserved.<BR>\r
 \r
 This program and the accompanying materials\r
 are licensed and made available under the terms and conditions\r
@@ -1858,7 +1858,7 @@ Undi16SimpleNetworkIsr (
 \r
       CopyMem (\r
         Frame,\r
-        (VOID *)(UINTN) ((SimpleNetworkDevice->Isr.FrameSegSel << 4) + SimpleNetworkDevice->Isr.FrameOffset),\r
+        (VOID *) (((UINTN) SimpleNetworkDevice->Isr.FrameSegSel << 4) + SimpleNetworkDevice->Isr.FrameOffset),\r
         SimpleNetworkDevice->Isr.BufferLength\r
         );\r
       Frame = Frame + SimpleNetworkDevice->Isr.BufferLength;\r
index 4750b2f99d9c2d8c55ab69f26d81832ef9a7fdcf..a1dc86786b1d4514df7e0af884433e4bdf87a569 100644 (file)
@@ -1,7 +1,7 @@
 /** @file\r
   Helper Routines that use a PXE-enabled NIC option ROM.\r
  \r
-Copyright (c) 1999 - 2014, Intel Corporation. All rights reserved.<BR>\r
+Copyright (c) 1999 - 2017, Intel Corporation. All rights reserved.<BR>\r
 \r
 This program and the accompanying materials\r
 are licensed and made available under the terms and conditions\r
@@ -49,7 +49,7 @@ CacheVectorAddress (
 {\r
   UINT32  *Address;\r
 \r
-  Address                          = (UINT32 *)(UINTN) (IVT_BASE + VectorNumber * 4);\r
+  Address                          = (UINT32 *) ((UINTN) IVT_BASE + VectorNumber * 4);\r
   CachedVectorAddress[VectorNumber] = *Address;\r
   return EFI_SUCCESS;\r
 }\r
@@ -68,7 +68,7 @@ RestoreCachedVectorAddress (
 {\r
   UINT32  *Address;\r
 \r
-  Address  = (UINT32 *)(UINTN) (IVT_BASE + VectorNumber * 4);\r
+  Address  = (UINT32 *) ((UINTN) IVT_BASE + VectorNumber * 4);\r
   *Address = CachedVectorAddress[VectorNumber];\r
   return EFI_SUCCESS;\r
 }\r
@@ -469,7 +469,7 @@ LaunchBaseCode (
 \r
   RomIdTableAddress = (UNDI_ROMID_T *) (RomAddress + OPTION_ROM_PTR->PxeRomIdOffset);\r
 \r
-  if ((UINTN) (OPTION_ROM_PTR->PxeRomIdOffset + RomIdTableAddress->StructLength) > RomLength) {\r
+  if (((UINT32)OPTION_ROM_PTR->PxeRomIdOffset + RomIdTableAddress->StructLength) > RomLength) {\r
     DEBUG ((DEBUG_ERROR, "ROM ID Offset Error\n\r"));\r
     return EFI_NOT_FOUND;\r
   }\r
@@ -754,10 +754,10 @@ LaunchBaseCode (
   Print_Undi_Loader_Table (UndiLoaderTable);\r
 \r
   DEBUG ((DEBUG_NET, "Display the PXENV+ and !PXE tables exported by NIC\n\r"));\r
-  Print_PXENV_Table ((VOID *)(UINTN)((UndiLoaderTable->PXENVptr.Segment << 4) | UndiLoaderTable->PXENVptr.Offset));\r
-  Print_PXE_Table ((VOID *)(UINTN)((UndiLoaderTable->PXEptr.Segment << 4) + UndiLoaderTable->PXEptr.Offset));\r
+  Print_PXENV_Table ((VOID *)(((UINTN)UndiLoaderTable->PXENVptr.Segment << 4) | UndiLoaderTable->PXENVptr.Offset));\r
+  Print_PXE_Table ((VOID *)(((UINTN)UndiLoaderTable->PXEptr.Segment << 4) + UndiLoaderTable->PXEptr.Offset));\r
 \r
-  Pxe = (PXE_T *)(UINTN)((UndiLoaderTable->PXEptr.Segment << 4) + UndiLoaderTable->PXEptr.Offset);\r
+  Pxe = (PXE_T *)(((UINTN)UndiLoaderTable->PXEptr.Segment << 4) + UndiLoaderTable->PXEptr.Offset);\r
   SimpleNetworkDevice->Nii.Id = (UINT64)(UINTN) Pxe;\r
 \r
   gBS->FreePool (Buffer);\r
index f1c8b295088843421de490dc0a3dc7db4d599b7d..08672cf4725cd3135fa460cef6bba090d89d7a4b 100644 (file)
@@ -1,7 +1,7 @@
 /** @file\r
   ConsoleOut Routines that speak VGA.\r
 \r
-Copyright (c) 2007 - 2014, Intel Corporation. All rights reserved.<BR>\r
+Copyright (c) 2007 - 2017, Intel Corporation. All rights reserved.<BR>\r
 \r
 This program and the accompanying materials\r
 are licensed and made available under the terms and conditions\r
@@ -1714,7 +1714,7 @@ BiosVideoCheckForVbe (
     //\r
     // Make sure the FrameBufferSize does not exceed the max available frame buffer size reported by VEB.\r
     //\r
-    ASSERT (CurrentModeData->FrameBufferSize <= (UINTN)(BiosVideoPrivate->VbeInformationBlock->TotalMemory * 64 * 1024));\r
+    ASSERT (CurrentModeData->FrameBufferSize <= ((UINT32)BiosVideoPrivate->VbeInformationBlock->TotalMemory * 64 * 1024));\r
     \r
     BiosVideoPrivate->ModeData = ModeBuffer;\r
   }\r
index dd2e2b9167c72c009c30ddcbe2b5519799c5a459..3ead2d98280929b0192ecf1222c02f9023db9fd2 100644 (file)
@@ -1,6 +1,6 @@
 /** @file\r
 \r
-Copyright (c) 2006 - 2013, Intel Corporation. All rights reserved.<BR>\r
+Copyright (c) 2006 - 2017, Intel Corporation. All rights reserved.<BR>\r
 \r
 This program and the accompanying materials\r
 are licensed and made available under the terms and conditions\r
@@ -144,7 +144,7 @@ LegacyBiosGetLegacyRegion (
      );\r
 \r
   if (Regs.X.AX == 0) {\r
-    *LegacyMemoryAddress  = (VOID *) (UINTN) ((Regs.X.DS << 4) + Regs.X.BX);\r
+    *LegacyMemoryAddress  = (VOID *) (((UINTN) Regs.X.DS << 4) + Regs.X.BX);\r
     Status = EFI_SUCCESS;\r
   } else {\r
     Status = EFI_OUT_OF_RESOURCES;\r
@@ -728,7 +728,7 @@ InstallSmbiosEventCallback (
   }\r
   \r
   if ((mStructureTableAddress != 0) && \r
-      (mStructureTablePages < (UINTN) EFI_SIZE_TO_PAGES (EntryPointStructure->TableLength))) {\r
+      (mStructureTablePages < EFI_SIZE_TO_PAGES ((UINT32)EntryPointStructure->TableLength))) {\r
     //\r
     // If original buffer is not enough for the new SMBIOS table, free original buffer and re-allocate\r
     //\r
index 52bcae2d1331eeb9527707fd54c3d1eac7e7173a..1e098b3726d8a52579b911f23f9120b309405f1c 100644 (file)
@@ -1,6 +1,6 @@
 /** @file\r
 \r
-Copyright (c) 2006 - 2015, Intel Corporation. All rights reserved.<BR>\r
+Copyright (c) 2006 - 2017, Intel Corporation. All rights reserved.<BR>\r
 \r
 This program and the accompanying materials\r
 are licensed and made available under the terms and conditions\r
@@ -91,7 +91,7 @@ PrintBbsTable (
     //\r
     // Print DescString\r
     //\r
-    String = (CHAR8 *)(UINTN)((BbsTable[Index].DescStringSegment << 4) + BbsTable[Index].DescStringOffset);\r
+    String = (CHAR8 *)(((UINTN)BbsTable[Index].DescStringSegment << 4) + BbsTable[Index].DescStringOffset);\r
     if (String != NULL) {\r
       DEBUG ((EFI_D_INFO," ("));\r
       for (SubIndex = 0; String[SubIndex] != 0; SubIndex++) {\r
index 628424d0e53959a4df0095bbfc7efa7bf545b49a..d1da635f35b3ea25ca5a1049df284242691a8acd 100644 (file)
@@ -227,7 +227,7 @@ BdsBuildLegacyDevNameString (
   //\r
   // If current BBS entry has its description then use it.\r
   //\r
-  StringDesc = (UINT8 *) (UINTN) ((CurBBSEntry->DescStringSegment << 4) + CurBBSEntry->DescStringOffset);\r
+  StringDesc = (UINT8 *) (((UINTN) CurBBSEntry->DescStringSegment << 4) + CurBBSEntry->DescStringOffset);\r
   if (NULL != StringDesc) {\r
     //\r
     // Only get fisrt 32 characters, this is suggested by BBS spec\r
index 2ba511aada0058a48877cafae39c54c38354c095..48938b0670b07a4558c869b39350cebd9081be86 100644 (file)
@@ -569,11 +569,11 @@ CharToUint (
   )\r
 {\r
   if ((Char >= L'0') && (Char <= L'9')) {\r
-    return (UINTN) (Char - L'0');\r
+    return (Char - L'0');\r
   }\r
 \r
   if ((Char >= L'A') && (Char <= L'F')) {\r
-    return (UINTN) (Char - L'A' + 0xA);\r
+    return (Char - L'A' + 0xA);\r
   }\r
 \r
   ASSERT (FALSE);\r
index 080a436c717316e8b5cfdf2f0fca03aea17b087a..76902eced32598a93b8de993f61ae44a1f2923d8 100644 (file)
@@ -176,7 +176,7 @@ LegacyBmBuildLegacyDevNameString (
   //\r
   // If current BBS entry has its description then use it.\r
   //\r
-  StringDesc = (CHAR8 *) (UINTN) ((CurBBSEntry->DescStringSegment << 4) + CurBBSEntry->DescStringOffset);\r
+  StringDesc = (CHAR8 *) (((UINTN) CurBBSEntry->DescStringSegment << 4) + CurBBSEntry->DescStringOffset);\r
   if (NULL != StringDesc) {\r
     //\r
     // Only get fisrt 32 characters, this is suggested by BBS spec\r
index ec9142245a46d7a3837ba2e90d87759a458ea228..2163710cc9666bb875b326dcfcf60514ff640738 100644 (file)
@@ -620,7 +620,7 @@ ConvertProcessorToString (
 \r
   if (Base10Exponent >= 6) {\r
     FreqMhz = ProcessorFrequency;\r
-    for (Index = 0; Index < (UINTN) (Base10Exponent - 6); Index++) {\r
+    for (Index = 0; Index < ((UINT32)Base10Exponent - 6); Index++) {\r
       FreqMhz *= 10;\r
     }\r
   } else {\r
index 9db9dbe97407827b0404b0766cd7af9c5a832137..9474606e2091dae191694e669f1ed06ec1ba667b 100644 (file)
@@ -1,7 +1,7 @@
 /** @file\r
   Uses the services of the I/O Library to produce the CPU I/O Protocol\r
 \r
-Copyright (c) 2004 - 2012, Intel Corporation. All rights reserved.<BR>\r
+Copyright (c) 2004 - 2017, Intel Corporation. All rights reserved.<BR>\r
 Copyright (c) 2017, AMD Incorporated. All rights reserved.<BR>\r
 \r
 This program and the accompanying materials                          \r
@@ -141,7 +141,7 @@ CpuIoCheckParameter (
   //\r
   // Check to see if Address is aligned\r
   //\r
-  if ((Address & (UINT64)(mInStride[Width] - 1)) != 0) {\r
+  if ((Address & ((UINT64)mInStride[Width] - 1)) != 0) {\r
     return EFI_UNSUPPORTED;\r
   }\r
 \r