]> git.proxmox.com Git - mirror_edk2.git/commitdiff
SourceLevelDebugPkg: Refine casting expression result to bigger size
authorHao Wu <hao.a.wu@intel.com>
Fri, 17 Feb 2017 03:44:04 +0000 (11:44 +0800)
committerHao Wu <hao.a.wu@intel.com>
Mon, 6 Mar 2017 06:33:25 +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>
SourceLevelDebugPkg/Library/DebugAgent/DxeDebugAgent/DxeDebugAgentLib.c
SourceLevelDebugPkg/Library/DebugAgent/SecPeiDebugAgent/SecPeiDebugAgentLib.c
SourceLevelDebugPkg/Library/DebugAgent/SmmDebugAgent/SmmDebugAgentLib.c
SourceLevelDebugPkg/Library/DebugCommunicationLibUsb/DebugCommunicationLibUsb.c

index a63932c6a71b8c20691dcdfa144d74dd1fd7cf3b..c74a1f6be3fb097277a24111e9a7ace3581dfa40 100644 (file)
@@ -1,7 +1,7 @@
 /** @file\r
   Debug Agent library implementition for Dxe Core and Dxr modules.\r
 \r
-  Copyright (c) 2010 - 2015, Intel Corporation. All rights reserved.<BR>\r
+  Copyright (c) 2010 - 2017, Intel Corporation. All rights reserved.<BR>\r
   This program and the accompanying materials\r
   are licensed and made available under the terms and conditions of the BSD License\r
   which accompanies this distribution.  The full text of the license may be found at\r
@@ -512,8 +512,8 @@ InitializeDebugAgent (
     if (Context != NULL) {\r
       Ia32Idtr =  (IA32_DESCRIPTOR *) Context;\r
       Ia32IdtEntry = (IA32_IDT_ENTRY *)(Ia32Idtr->Base);\r
-      MailboxLocation = (UINT64 *) (UINTN) (Ia32IdtEntry[DEBUG_MAILBOX_VECTOR].Bits.OffsetLow +\r
-                                  (UINT32) (Ia32IdtEntry[DEBUG_MAILBOX_VECTOR].Bits.OffsetHigh << 16));\r
+      MailboxLocation = (UINT64 *) ((UINTN) Ia32IdtEntry[DEBUG_MAILBOX_VECTOR].Bits.OffsetLow +\r
+                                   ((UINTN) Ia32IdtEntry[DEBUG_MAILBOX_VECTOR].Bits.OffsetHigh << 16));\r
       Mailbox = (DEBUG_AGENT_MAILBOX *)(UINTN)(*MailboxLocation);\r
       VerifyMailboxChecksum (Mailbox);\r
     }\r
index 128c69f60cb24901724c6b642a1cb46b90047e98..b717e33197647daf1279aeaa23d9cf6301908757 100644 (file)
@@ -1,7 +1,7 @@
 /** @file\r
   SEC Core Debug Agent Library instance implementition.\r
 \r
-  Copyright (c) 2010 - 2015, Intel Corporation. All rights reserved.<BR>\r
+  Copyright (c) 2010 - 2017, Intel Corporation. All rights reserved.<BR>\r
   This program and the accompanying materials\r
   are licensed and made available under the terms and conditions of the BSD License\r
   which accompanies this distribution.  The full text of the license may be found at\r
@@ -213,7 +213,7 @@ GetMailboxPointer (
     // Fix up Debug Port handler and save new mailbox in IDT entry\r
     //\r
     Mailbox = (DEBUG_AGENT_MAILBOX *)((UINTN)Mailbox + ((UINTN)(MailboxLocationInHob) - (UINTN)MailboxLocationInIdt));\r
-    DebugPortHandle = (UINT64)((UINTN)Mailbox->DebugPortHandle + ((UINTN)(MailboxLocationInHob) - (UINTN)MailboxLocationInIdt));\r
+    DebugPortHandle = (UINTN)Mailbox->DebugPortHandle + ((UINTN)(MailboxLocationInHob) - (UINTN)MailboxLocationInIdt);\r
     UpdateMailboxContent (Mailbox, DEBUG_MAILBOX_DEBUG_PORT_HANDLE_INDEX, DebugPortHandle);\r
     *MailboxLocationInHob = (UINT64)(UINTN)Mailbox;\r
     SetLocationSavedMailboxPointerInIdtEntry (MailboxLocationInHob);\r
@@ -582,8 +582,8 @@ InitializeDebugAgent (
     } else {\r
       Ia32Idtr =  (IA32_DESCRIPTOR *) Context;\r
       Ia32IdtEntry = (IA32_IDT_ENTRY *)(Ia32Idtr->Base);\r
-      MailboxLocationPointer = (UINT64 *) (UINTN) (Ia32IdtEntry[DEBUG_MAILBOX_VECTOR].Bits.OffsetLow +\r
-                                         (UINT32) (Ia32IdtEntry[DEBUG_MAILBOX_VECTOR].Bits.OffsetHigh << 16));\r
+      MailboxLocationPointer = (UINT64 *) ((UINTN) Ia32IdtEntry[DEBUG_MAILBOX_VECTOR].Bits.OffsetLow +\r
+                                          ((UINTN) Ia32IdtEntry[DEBUG_MAILBOX_VECTOR].Bits.OffsetHigh << 16));\r
       Mailbox = (DEBUG_AGENT_MAILBOX *) (UINTN)(*MailboxLocationPointer);\r
       //\r
       // Mailbox should valid and setup before executing thunk code\r
index 6216142dbaf21af7e398e9324faf4666fd5cdeda..11afd329fad334c6f572fe0b9f5725dd4158ca54 100644 (file)
@@ -1,7 +1,7 @@
 /** @file\r
   Debug Agent library implementition.\r
 \r
-  Copyright (c) 2010 - 2016, Intel Corporation. All rights reserved.<BR>\r
+  Copyright (c) 2010 - 2017, Intel Corporation. All rights reserved.<BR>\r
   This program and the accompanying materials\r
   are licensed and made available under the terms and conditions of the BSD License\r
   which accompanies this distribution.  The full text of the license may be found at\r
@@ -336,8 +336,8 @@ InitializeDebugAgent (
     } else {\r
       Ia32Idtr =  (IA32_DESCRIPTOR *) Context;\r
       Ia32IdtEntry = (IA32_IDT_ENTRY *)(Ia32Idtr->Base);\r
-      MailboxLocation = (UINT64 *) (UINTN) (Ia32IdtEntry[DEBUG_MAILBOX_VECTOR].Bits.OffsetLow + \r
-                                  (UINT32) (Ia32IdtEntry[DEBUG_MAILBOX_VECTOR].Bits.OffsetHigh << 16));\r
+      MailboxLocation = (UINT64 *) ((UINTN) Ia32IdtEntry[DEBUG_MAILBOX_VECTOR].Bits.OffsetLow +\r
+                                   ((UINTN) Ia32IdtEntry[DEBUG_MAILBOX_VECTOR].Bits.OffsetHigh << 16));\r
       mMailboxPointer = (DEBUG_AGENT_MAILBOX *)(UINTN)(*MailboxLocation);\r
       VerifyMailboxChecksum (mMailboxPointer);\r
       //\r
index d7829c211077e16d1a4e37d8f09bbe23810a92a2..d996f80f59e31ed087c982bc2cf674b7e9136924 100644 (file)
@@ -1,7 +1,7 @@
 /** @file\r
   Debug Port Library implementation based on usb debug port.\r
 \r
-  Copyright (c) 2010 - 2015, Intel Corporation. All rights reserved.<BR>\r
+  Copyright (c) 2010 - 2017, Intel Corporation. All rights reserved.<BR>\r
   This program and the accompanying materials\r
   are licensed and made available under the terms and conditions of the BSD License\r
   which accompanies this distribution.  The full text of the license may be found at\r
@@ -559,7 +559,7 @@ NeedReinitializeHardware(
   //\r
   // If the owner and in_use bit is not set, it means system is doing cold/warm boot or EHCI host controller is reset by system software.\r
   //\r
-  UsbDebugPortRegister = (USB_DEBUG_PORT_REGISTER *)(UINTN)(Handle->UsbDebugPortMemoryBase + Handle->DebugPortOffset);\r
+  UsbDebugPortRegister = (USB_DEBUG_PORT_REGISTER *)((UINTN)Handle->UsbDebugPortMemoryBase + Handle->DebugPortOffset);\r
   if ((MmioRead32((UINTN)&UsbDebugPortRegister->ControlStatus) & (USB_DEBUG_PORT_OWNER | USB_DEBUG_PORT_ENABLE | USB_DEBUG_PORT_IN_USE))\r
        != (USB_DEBUG_PORT_OWNER | USB_DEBUG_PORT_ENABLE | USB_DEBUG_PORT_IN_USE)) {\r
     Status = TRUE;\r
@@ -604,10 +604,10 @@ InitializeUsbDebugHardware (
   UINT8                     DebugPortNumber;\r
   UINT8                     Length;\r
 \r
-  UsbDebugPortRegister = (USB_DEBUG_PORT_REGISTER *)(UINTN)(Handle->UsbDebugPortMemoryBase + Handle->DebugPortOffset);\r
-  UsbHCSParam = (UINT32 *)(UINTN)(Handle->EhciMemoryBase + 0x04);\r
-  UsbCmd      = (UINT32 *)(UINTN)(Handle->EhciMemoryBase + 0x20);\r
-  UsbStatus   = (UINT32 *)(UINTN)(Handle->EhciMemoryBase + 0x24);\r
+  UsbDebugPortRegister = (USB_DEBUG_PORT_REGISTER *)((UINTN)Handle->UsbDebugPortMemoryBase + Handle->DebugPortOffset);\r
+  UsbHCSParam = (UINT32 *)((UINTN)Handle->EhciMemoryBase + 0x04);\r
+  UsbCmd      = (UINT32 *)((UINTN)Handle->EhciMemoryBase + 0x20);\r
+  UsbStatus   = (UINT32 *)((UINTN)Handle->EhciMemoryBase + 0x24);\r
 \r
   //\r
   // Check if the debug port is enabled and owned by myself.\r
@@ -652,7 +652,7 @@ InitializeUsbDebugHardware (
   //\r
   // Should find a device is connected at debug port\r
   //\r
-  PortStatus = (UINT32 *)(UINTN)(Handle->EhciMemoryBase + 0x64 + (DebugPortNumber - 1) * 4);\r
+  PortStatus = (UINT32 *)((UINTN)Handle->EhciMemoryBase + 0x64 + (DebugPortNumber - 1) * 4);\r
   if (!(MmioRead32((UINTN)PortStatus) & BIT0)) {\r
     Handle->Initialized = USBDBG_NO_DEV;\r
     return RETURN_NOT_FOUND;\r
@@ -870,7 +870,7 @@ DebugPortWriteBuffer (
     }\r
   }\r
 \r
-  UsbDebugPortRegister = (USB_DEBUG_PORT_REGISTER *)(UINTN)(UsbDebugPortHandle->UsbDebugPortMemoryBase + UsbDebugPortHandle->DebugPortOffset);\r
+  UsbDebugPortRegister = (USB_DEBUG_PORT_REGISTER *)((UINTN)UsbDebugPortHandle->UsbDebugPortMemoryBase + UsbDebugPortHandle->DebugPortOffset);\r
 \r
   while ((Total < NumberOfBytes)) {\r
     if (NumberOfBytes - Total > USB_DEBUG_PORT_MAX_PACKET_SIZE) {\r
@@ -950,7 +950,7 @@ DebugPortPollBuffer (
     return TRUE;\r
   }\r
 \r
-  UsbDebugPortRegister = (USB_DEBUG_PORT_REGISTER *)(UINTN)(UsbDebugPortHandle->UsbDebugPortMemoryBase + UsbDebugPortHandle->DebugPortOffset);\r
+  UsbDebugPortRegister = (USB_DEBUG_PORT_REGISTER *)((UINTN)UsbDebugPortHandle->UsbDebugPortMemoryBase + UsbDebugPortHandle->DebugPortOffset);\r
 \r
   UsbDebugPortRegister->TokenPid = INPUT_PID;\r
   if (UsbDebugPortHandle->BulkInToggle == 0) {\r