From nobody Sat Nov 2 16:19:39 2024 Delivered-To: importer@patchew.org Received-SPF: none (zoho.com: 198.145.21.10 is neither permitted nor denied by domain of lists.01.org) client-ip=198.145.21.10; envelope-from=edk2-devel-bounces@lists.01.org; helo=ml01.01.org; Authentication-Results: mx.zoho.com; spf=none (zoho.com: 198.145.21.10 is neither permitted nor denied by domain of lists.01.org) smtp.mailfrom=edk2-devel-bounces@lists.01.org; Return-Path: Received: from ml01.01.org (ml01.01.org [198.145.21.10]) by mx.zohomail.com with SMTPS id 1487999601858833.0457778816725; Fri, 24 Feb 2017 21:13:21 -0800 (PST) Received: from [127.0.0.1] (localhost [IPv6:::1]) by ml01.01.org (Postfix) with ESMTP id 609A8821B5; Fri, 24 Feb 2017 21:13:05 -0800 (PST) Received: from mga03.intel.com (mga03.intel.com [134.134.136.65]) (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 84CDA821B4 for ; Fri, 24 Feb 2017 21:13:03 -0800 (PST) Received: from orsmga002.jf.intel.com ([10.7.209.21]) by orsmga103.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 24 Feb 2017 21:13:03 -0800 Received: from shwdeopenpsi014.ccr.corp.intel.com ([10.239.9.13]) by orsmga002.jf.intel.com with ESMTP; 24 Feb 2017 21:13:02 -0800 X-Original-To: edk2-devel@lists.01.org X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.35,202,1484035200"; d="scan'208";a="52624065" From: Hao Wu To: edk2-devel@lists.01.org Date: Sat, 25 Feb 2017 13:12:34 +0800 Message-Id: <1487999555-9764-12-git-send-email-hao.a.wu@intel.com> X-Mailer: git-send-email 1.9.5.msysgit.0 In-Reply-To: <1487999555-9764-1-git-send-email-hao.a.wu@intel.com> References: <1487999555-9764-1-git-send-email-hao.a.wu@intel.com> Subject: [edk2] [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: , Cc: Hao Wu , Jeff Fan MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Errors-To: edk2-devel-bounces@lists.01.org Sender: "edk2-devel" X-ZohoMail: RSF_4 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" 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 =3D (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 =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 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'. Cc: Jeff Fan Contributed-under: TianoCore Contribution Agreement 1.0 Signed-off-by: Hao Wu Reviewed-by: Jeff Fan --- 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/DxeDebugAgentLib= .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 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) Ia32IdtEntry[DEBUG_MAILBOX_VEC= TOR].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/SecPeiDebugAg= entLib.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 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 + ((UINTN)(Mailbox= LocationInHob) - (UINTN)MailboxLocationInIdt); UpdateMailboxContent (Mailbox, DEBUG_MAILBOX_DEBUG_PORT_HANDLE_INDEX, = DebugPortHandle); *MailboxLocationInHob =3D (UINT64)(UINTN)Mailbox; SetLocationSavedMailboxPointerInIdtEntry (MailboxLocationInHob); @@ -582,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) Ia32IdtEntry[DEBUG_MAIL= BOX_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/SmmDebugA= gentLib.c b/SourceLevelDebugPkg/Library/DebugAgent/SmmDebugAgent/SmmDebugAg= entLib.c index 6216142..11afd32 100644 --- a/SourceLevelDebugPkg/Library/DebugAgent/SmmDebugAgent/SmmDebugAgentLib= .c +++ b/SourceLevelDebugPkg/Library/DebugAgent/SmmDebugAgent/SmmDebugAgentLib= .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 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) Ia32IdtEntry[DEBUG_MAILBOX_VEC= TOR].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/DebugCommunicati= onLibUsb.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 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 *)((UINTN)Handle->UsbD= ebugPortMemoryBase + 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 *)((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); =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 + (DebugP= ortNumber - 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 *)((UINTN)UsbDebugPort= Handle->UsbDebugPortMemoryBase + 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 *)((UINTN)UsbDebugPort= Handle->UsbDebugPortMemoryBase + UsbDebugPortHandle->DebugPortOffset); =20 UsbDebugPortRegister->TokenPid =3D INPUT_PID; if (UsbDebugPortHandle->BulkInToggle =3D=3D 0) { --=20 1.9.5.msysgit.0 _______________________________________________ edk2-devel mailing list edk2-devel@lists.01.org https://lists.01.org/mailman/listinfo/edk2-devel