From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mga01.intel.com (mga01.intel.com [192.55.52.88]) (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 AA45A81EFA for ; Sun, 26 Feb 2017 23:27:53 -0800 (PST) Received: from orsmga005.jf.intel.com ([10.7.209.41]) by fmsmga101.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 26 Feb 2017 23:27:53 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.35,213,1484035200"; d="scan'208";a="69405556" Received: from fmsmsx106.amr.corp.intel.com ([10.18.124.204]) by orsmga005.jf.intel.com with ESMTP; 26 Feb 2017 23:27:52 -0800 Received: from fmsmsx119.amr.corp.intel.com (10.18.124.207) by FMSMSX106.amr.corp.intel.com (10.18.124.204) with Microsoft SMTP Server (TLS) id 14.3.248.2; Sun, 26 Feb 2017 23:27:52 -0800 Received: from shsmsx151.ccr.corp.intel.com (10.239.6.50) by FMSMSX119.amr.corp.intel.com (10.18.124.207) with Microsoft SMTP Server (TLS) id 14.3.248.2; Sun, 26 Feb 2017 23:27:52 -0800 Received: from shsmsx102.ccr.corp.intel.com ([169.254.2.88]) by SHSMSX151.ccr.corp.intel.com ([169.254.3.204]) with mapi id 14.03.0248.002; Mon, 27 Feb 2017 15:27:49 +0800 From: "Fan, Jeff" To: "Wu, Hao A" , "edk2-devel@lists.01.org" Thread-Topic: [PATCH v3 11/12] SourceLevelDebugPkg: Refine casting expression result to bigger size Thread-Index: AQHSjyXcGcqHwhSURkeuyBis9AklxqF8d62w Date: Mon, 27 Feb 2017 07:27:48 +0000 Message-ID: <542CF652F8836A4AB8DBFAAD40ED192A4C54A08A@shsmsx102.ccr.corp.intel.com> References: <1487999555-9764-1-git-send-email-hao.a.wu@intel.com> <1487999555-9764-12-git-send-email-hao.a.wu@intel.com> In-Reply-To: <1487999555-9764-12-git-send-email-hao.a.wu@intel.com> Accept-Language: zh-CN, en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-titus-metadata-40: eyJDYXRlZ29yeUxhYmVscyI6IiIsIk1ldGFkYXRhIjp7Im5zIjoiaHR0cDpcL1wvd3d3LnRpdHVzLmNvbVwvbnNcL0ludGVsMyIsImlkIjoiYTZjZjMxZDgtMzMzNi00MTIwLWI1NjEtNDhmZTQyZTU1MDYwIiwicHJvcHMiOlt7Im4iOiJDVFBDbGFzc2lmaWNhdGlvbiIsInZhbHMiOlt7InZhbHVlIjoiQ1RQX0lDIn1dfV19LCJTdWJqZWN0TGFiZWxzIjpbXSwiVE1DVmVyc2lvbiI6IjE1LjkuNi42IiwiVHJ1c3RlZExhYmVsSGFzaCI6IjN0cTR3RStGQVVDd2FjRnZiTnlyWitiemhBbHlya2lRV2x3a2FPVFlrXC9zPSJ9 x-ctpclassification: CTP_IC x-originating-ip: [10.239.127.40] MIME-Version: 1.0 Subject: Re: [PATCH v3 11/12] SourceLevelDebugPkg: 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: Mon, 27 Feb 2017 07:27:53 -0000 Content-Language: en-US Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable Reviewed-by: Jeff Fan -----Original Message----- From: Wu, Hao A=20 Sent: Saturday, February 25, 2017 1:13 PM To: edk2-devel@lists.01.org Cc: Wu, Hao A; Fan, Jeff Subject: [PATCH v3 11/12] SourceLevelDebugPkg: Refine casting expression re= sult to bigger size There are cases that the operands of an expression are all with rank less t= han 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 =3D (UINT64) (a + b); Some static code checkers may warn that the expression result might overflo= w 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 =3D (UINT64)a + b; 2). When the expression will not overflow within the rank of "int", remove = the explicit type casts: c =3D a + b; 3). When the expression will be cast to pointer of possible greater size: UINT32 a,b; VOID *c; c =3D (VOID *)(UINTN)(a + b); --> c =3D (VOID *)((UINTN)a + b); 4). When one side of a comparison expression contains only operands with ra= nk 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'. Cc: Jeff Fan Contributed-under: TianoCore Contribution Agreement 1.0 Signed-off-by: Hao Wu --- SourceLevelDebugPkg/Library/DebugAgent/DxeDebugAgent/DxeDebugAgentLib.c = | 6 +++--- SourceLevelDebugPkg/Library/DebugAgent/SecPeiDebugAgent/SecPeiDebugAgentLi= b.c | 8 ++++---- SourceLevelDebugPkg/Library/DebugAgent/SmmDebugAgent/SmmDebugAgentLib.c = | 6 +++--- SourceLevelDebugPkg/Library/DebugCommunicationLibUsb/DebugCommunicationLib= Usb.c | 18 +++++++++--------- 4 files changed, 19 insertions(+), 19 deletions(-) diff --git a/SourceLevelDebugPkg/Library/DebugAgent/DxeDebugAgent/DxeDebugA= gentLib.c b/SourceLevelDebugPkg/Library/DebugAgent/DxeDebugAgent/DxeDebugAg= entLib.c index a63932c..c74a1f6 100644 --- a/SourceLevelDebugPkg/Library/DebugAgent/DxeDebugAgent/DxeDebugAgentLib= .c +++ b/SourceLevelDebugPkg/Library/DebugAgent/DxeDebugAgent/DxeDebugAgent +++ Lib.c @@ -1,7 +1,7 @@ /** @file Debug Agent library implementition for Dxe Core and Dxr modules. =20 - Copyright (c) 2010 - 2015, Intel Corporation. All rights reserved.
+ Copyright (c) 2010 - 2017, Intel Corporation. All rights=20 + reserved.
This program and the accompanying materials are licensed and made available under the terms and conditions of the BS= D License which accompanies this distribution. The full text of the license may b= e found at @@ -512,8 +512,8 @@ InitializeDebugAgent ( if (Context !=3D NULL) { Ia32Idtr =3D (IA32_DESCRIPTOR *) Context; Ia32IdtEntry =3D (IA32_IDT_ENTRY *)(Ia32Idtr->Base); - MailboxLocation =3D (UINT64 *) (UINTN) (Ia32IdtEntry[DEBUG_MAILBOX_V= ECTOR].Bits.OffsetLow + - (UINT32) (Ia32IdtEntry[DEBUG_MAILBOX_VEC= TOR].Bits.OffsetHigh << 16)); + MailboxLocation =3D (UINT64 *) ((UINTN) Ia32IdtEntry[DEBUG_MAILBOX_V= ECTOR].Bits.OffsetLow + + ((UINTN)=20 + Ia32IdtEntry[DEBUG_MAILBOX_VECTOR].Bits.OffsetHigh << 16)); Mailbox =3D (DEBUG_AGENT_MAILBOX *)(UINTN)(*MailboxLocation); VerifyMailboxChecksum (Mailbox); } diff --git a/SourceLevelDebugPkg/Library/DebugAgent/SecPeiDebugAgent/SecPei= DebugAgentLib.c b/SourceLevelDebugPkg/Library/DebugAgent/SecPeiDebugAgent/S= ecPeiDebugAgentLib.c index 128c69f..b717e33 100644 --- a/SourceLevelDebugPkg/Library/DebugAgent/SecPeiDebugAgent/SecPeiDebugAg= entLib.c +++ b/SourceLevelDebugPkg/Library/DebugAgent/SecPeiDebugAgent/SecPeiDebu +++ gAgentLib.c @@ -1,7 +1,7 @@ /** @file SEC Core Debug Agent Library instance implementition. =20 - Copyright (c) 2010 - 2015, Intel Corporation. All rights reserved.
+ Copyright (c) 2010 - 2017, Intel Corporation. All rights=20 + reserved.
This program and the accompanying materials are licensed and made available under the terms and conditions of the BS= D License which accompanies this distribution. The full text of the license may b= e found at @@ -213,7 +213,7 @@ GetMailboxPointer ( // Fix up Debug Port handler and save new mailbox in IDT entry // Mailbox =3D (DEBUG_AGENT_MAILBOX *)((UINTN)Mailbox + ((UINTN)(MailboxL= ocationInHob) - (UINTN)MailboxLocationInIdt)); - DebugPortHandle =3D (UINT64)((UINTN)Mailbox->DebugPortHandle + ((UINTN= )(MailboxLocationInHob) - (UINTN)MailboxLocationInIdt)); + DebugPortHandle =3D (UINTN)Mailbox->DebugPortHandle +=20 + ((UINTN)(MailboxLocationInHob) - (UINTN)MailboxLocationInIdt); UpdateMailboxContent (Mailbox, DEBUG_MAILBOX_DEBUG_PORT_HANDLE_INDEX, = DebugPortHandle); *MailboxLocationInHob =3D (UINT64)(UINTN)Mailbox; SetLocationSavedMailboxPointerInIdtEntry (MailboxLocationInHob); @@ -5= 82,8 +582,8 @@ InitializeDebugAgent ( } else { Ia32Idtr =3D (IA32_DESCRIPTOR *) Context; Ia32IdtEntry =3D (IA32_IDT_ENTRY *)(Ia32Idtr->Base); - MailboxLocationPointer =3D (UINT64 *) (UINTN) (Ia32IdtEntry[DEBUG_MA= ILBOX_VECTOR].Bits.OffsetLow + - (UINT32) (Ia32IdtEntry[DEBUG_MAIL= BOX_VECTOR].Bits.OffsetHigh << 16)); + MailboxLocationPointer =3D (UINT64 *) ((UINTN) Ia32IdtEntry[DEBUG_MA= ILBOX_VECTOR].Bits.OffsetLow + + ((UINTN)=20 + Ia32IdtEntry[DEBUG_MAILBOX_VECTOR].Bits.OffsetHigh << 16)); Mailbox =3D (DEBUG_AGENT_MAILBOX *) (UINTN)(*MailboxLocationPointer)= ; // // Mailbox should valid and setup before executing thunk code diff -= -git a/SourceLevelDebugPkg/Library/DebugAgent/SmmDebugAgent/SmmDebugAgentLi= b.c b/SourceLevelDebugPkg/Library/DebugAgent/SmmDebugAgent/SmmDebugAgentLib= .c index 6216142..11afd32 100644 --- a/SourceLevelDebugPkg/Library/DebugAgent/SmmDebugAgent/SmmDebugAgentLib= .c +++ b/SourceLevelDebugPkg/Library/DebugAgent/SmmDebugAgent/SmmDebugAgent +++ Lib.c @@ -1,7 +1,7 @@ /** @file Debug Agent library implementition. =20 - Copyright (c) 2010 - 2016, Intel Corporation. All rights reserved.
+ Copyright (c) 2010 - 2017, Intel Corporation. All rights=20 + reserved.
This program and the accompanying materials are licensed and made available under the terms and conditions of the BS= D License which accompanies this distribution. The full text of the license may b= e found at @@ -336,8 +336,8 @@ InitializeDebugAgent ( } else { Ia32Idtr =3D (IA32_DESCRIPTOR *) Context; Ia32IdtEntry =3D (IA32_IDT_ENTRY *)(Ia32Idtr->Base); - MailboxLocation =3D (UINT64 *) (UINTN) (Ia32IdtEntry[DEBUG_MAILBOX_V= ECTOR].Bits.OffsetLow +=20 - (UINT32) (Ia32IdtEntry[DEBUG_MAILBOX_VEC= TOR].Bits.OffsetHigh << 16)); + MailboxLocation =3D (UINT64 *) ((UINTN) Ia32IdtEntry[DEBUG_MAILBOX_V= ECTOR].Bits.OffsetLow + + ((UINTN)=20 + Ia32IdtEntry[DEBUG_MAILBOX_VECTOR].Bits.OffsetHigh << 16)); mMailboxPointer =3D (DEBUG_AGENT_MAILBOX *)(UINTN)(*MailboxLocation)= ; VerifyMailboxChecksum (mMailboxPointer); // diff --git a/SourceLevelDebugPkg/Library/DebugCommunicationLibUsb/DebugComm= unicationLibUsb.c b/SourceLevelDebugPkg/Library/DebugCommunicationLibUsb/De= bugCommunicationLibUsb.c index d7829c2..d996f80 100644 --- a/SourceLevelDebugPkg/Library/DebugCommunicationLibUsb/DebugCommunicati= onLibUsb.c +++ b/SourceLevelDebugPkg/Library/DebugCommunicationLibUsb/DebugCommunic +++ ationLibUsb.c @@ -1,7 +1,7 @@ /** @file Debug Port Library implementation based on usb debug port. =20 - Copyright (c) 2010 - 2015, Intel Corporation. All rights reserved.
+ Copyright (c) 2010 - 2017, Intel Corporation. All rights=20 + reserved.
This program and the accompanying materials are licensed and made available under the terms and conditions of the BS= D License which accompanies this distribution. The full text of the license may b= e found at @@ -559,7 +559,7 @@ NeedReinitializeHardware( // // 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. // - UsbDebugPortRegister =3D (USB_DEBUG_PORT_REGISTER *)(UINTN)(Handle->UsbD= ebugPortMemoryBase + Handle->DebugPortOffset); + UsbDebugPortRegister =3D (USB_DEBUG_PORT_REGISTER=20 + *)((UINTN)Handle->UsbDebugPortMemoryBase + Handle->DebugPortOffset); if ((MmioRead32((UINTN)&UsbDebugPortRegister->ControlStatus) & (USB_DEBU= G_PORT_OWNER | USB_DEBUG_PORT_ENABLE | USB_DEBUG_PORT_IN_USE)) !=3D (USB_DEBUG_PORT_OWNER | USB_DEBUG_PORT_ENABLE | USB_DEBUG_PORT= _IN_USE)) { Status =3D TRUE; @@ -604,10 +604,10 @@ InitializeUsbDebugHardware ( UINT8 DebugPortNumber; UINT8 Length; =20 - UsbDebugPortRegister =3D (USB_DEBUG_PORT_REGISTER *)(UINTN)(Handle->UsbD= ebugPortMemoryBase + Handle->DebugPortOffset); - UsbHCSParam =3D (UINT32 *)(UINTN)(Handle->EhciMemoryBase + 0x04); - UsbCmd =3D (UINT32 *)(UINTN)(Handle->EhciMemoryBase + 0x20); - UsbStatus =3D (UINT32 *)(UINTN)(Handle->EhciMemoryBase + 0x24); + UsbDebugPortRegister =3D (USB_DEBUG_PORT_REGISTER=20 + *)((UINTN)Handle->UsbDebugPortMemoryBase + Handle->DebugPortOffset); Usb= HCSParam =3D (UINT32 *)((UINTN)Handle->EhciMemoryBase + 0x04); + UsbCmd =3D (UINT32 *)((UINTN)Handle->EhciMemoryBase + 0x20); + UsbStatus =3D (UINT32 *)((UINTN)Handle->EhciMemoryBase + 0x24); =20 // // Check if the debug port is enabled and owned by myself. @@ -652,7 +652,7 @@ InitializeUsbDebugHardware ( // // Should find a device is connected at debug port // - PortStatus =3D (UINT32 *)(UINTN)(Handle->EhciMemoryBase + 0x64 + (DebugP= ortNumber - 1) * 4); + PortStatus =3D (UINT32 *)((UINTN)Handle->EhciMemoryBase + 0x64 +=20 + (DebugPortNumber - 1) * 4); if (!(MmioRead32((UINTN)PortStatus) & BIT0)) { Handle->Initialized =3D USBDBG_NO_DEV; return RETURN_NOT_FOUND; @@ -870,7 +870,7 @@ DebugPortWriteBuffer ( } } =20 - UsbDebugPortRegister =3D (USB_DEBUG_PORT_REGISTER *)(UINTN)(UsbDebugPort= Handle->UsbDebugPortMemoryBase + UsbDebugPortHandle->DebugPortOffset); + UsbDebugPortRegister =3D (USB_DEBUG_PORT_REGISTER=20 + *)((UINTN)UsbDebugPortHandle->UsbDebugPortMemoryBase +=20 + UsbDebugPortHandle->DebugPortOffset); =20 while ((Total < NumberOfBytes)) { if (NumberOfBytes - Total > USB_DEBUG_PORT_MAX_PACKET_SIZE) { @@ -950,= 7 +950,7 @@ DebugPortPollBuffer ( return TRUE; } =20 - UsbDebugPortRegister =3D (USB_DEBUG_PORT_REGISTER *)(UINTN)(UsbDebugPort= Handle->UsbDebugPortMemoryBase + UsbDebugPortHandle->DebugPortOffset); + UsbDebugPortRegister =3D (USB_DEBUG_PORT_REGISTER=20 + *)((UINTN)UsbDebugPortHandle->UsbDebugPortMemoryBase +=20 + UsbDebugPortHandle->DebugPortOffset); =20 UsbDebugPortRegister->TokenPid =3D INPUT_PID; if (UsbDebugPortHandle->BulkInToggle =3D=3D 0) { -- 1.9.5.msysgit.0