From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) (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 2E0E721A13499 for ; Thu, 18 May 2017 10:24:37 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 7DED390907; Thu, 18 May 2017 17:24:36 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 7DED390907 Authentication-Results: ext-mx05.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx05.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=lersek@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com 7DED390907 Received: from lacos-laptop-7.usersys.redhat.com (ovpn-116-86.phx2.redhat.com [10.3.116.86]) by smtp.corp.redhat.com (Postfix) with ESMTP id 103D75C3FA; Thu, 18 May 2017 17:24:34 +0000 (UTC) To: Michael Kinney , edk2-devel@lists.01.org Cc: Ruiyu Ni , Andrew Fish References: <1495126989-21904-1-git-send-email-michael.d.kinney@intel.com> From: Laszlo Ersek Message-ID: Date: Thu, 18 May 2017 19:24:34 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.1.1 MIME-Version: 1.0 In-Reply-To: <1495126989-21904-1-git-send-email-michael.d.kinney@intel.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.29]); Thu, 18 May 2017 17:24:36 +0000 (UTC) Subject: Re: [Patch] PcAtChipsetPkg/SerialIoLib: Remove negative value shift X-BeenThere: edk2-devel@lists.01.org X-Mailman-Version: 2.1.22 Precedence: list List-Id: EDK II Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 18 May 2017 17:24:37 -0000 Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 7bit On 05/18/17 19:03, Michael Kinney wrote: > https://bugzilla.tianocore.org/show_bug.cgi?id=553 > > Remove left shift of negative values that always evaluate > to 0 to address build errors from the llvm/clang compiler > used in the XCODE5 tool chain. > > Cc: Ruiyu Ni > Cc: Andrew Fish > Cc: Laszlo Ersek > Contributed-under: TianoCore Contribution Agreement 1.0 > Signed-off-by: Michael D Kinney > --- > PcAtChipsetPkg/Library/SerialIoLib/SerialPortLib.c | 4 ++-- > 1 file changed, 2 insertions(+), 2 deletions(-) > > diff --git a/PcAtChipsetPkg/Library/SerialIoLib/SerialPortLib.c b/PcAtChipsetPkg/Library/SerialIoLib/SerialPortLib.c > index 95e0db7..0a2e20c 100644 > --- a/PcAtChipsetPkg/Library/SerialIoLib/SerialPortLib.c > +++ b/PcAtChipsetPkg/Library/SerialIoLib/SerialPortLib.c > @@ -102,7 +102,7 @@ SerialPortInitialize ( > // > // Switch back to bank 0 > // > - OutputData = (UINT8) ((~DLAB << 7) | (gBreakSet << 6) | (gParity << 3) | (gStop << 2) | Data); > + OutputData = (UINT8) ( (gBreakSet << 6) | (gParity << 3) | (gStop << 2) | Data); > IoWrite8 (gUartBase + LCR_OFFSET, OutputData); > > return RETURN_SUCCESS; > @@ -481,7 +481,7 @@ SerialPortSetAttributes ( > // > // Switch back to bank 0 > // > - OutputData = (UINT8) ((~DLAB << 7) | (gBreakSet << 6) | (LcrParity << 3) | (LcrStop << 2) | LcrData); > + OutputData = (UINT8) ((gBreakSet << 6) | (LcrParity << 3) | (LcrStop << 2) | LcrData); > IoWrite8 (gUartBase + LCR_OFFSET, OutputData); > > return RETURN_SUCCESS; > The patch looks correct, but the commit message is not. As you write in the BZ, DLAB is defined as #define DLAB 0x01 After macro expansion, the value 0x01 has type "int". On all edk2 platforms, "int" has 1 sign bit, 31 value bits, 0 padding bits, and the representation is two's complement. After applying the bit-neg operator (which is itself *not* undefined), the result has value -2 (bit pattern 0xFFFF_FFFE). When "int" value (-2) is left-shifted, the behavior is undefined. This is why clang complains (justifiedly). The fact that all left-shifted nonzero bits would be thrown away anyway, due to the final conversion to UINT8, does not save the undefined behavior in the left-shift of (-2). That argument would only matter if we had: ~(UINT32)DLAB or else DLAB were defined as #define DLAB 0x01u So, I would put the commit message as follows: ---- Clang rightfully complains about left-shifting ~DLAB. DLAB is #defined as 0x01 (an "int"), hence ~DLAB has value (-2) on all edk2 platforms. Left-shifting a negative int is undefined behavior. Rather than replacing ~DLAB with ~(UINT32)DLAB, realize that the nonzero bits of (~(UINT32)DLAB << 7) would all be truncated away in the final conversion to UINT8 anyway. So just remove (~DLAB << 7). ---- With a commit message like this, Reviewed-by: Laszlo Ersek Thanks, Laszlo