From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received-SPF: Pass (sender SPF authorized) identity=mailfrom; client-ip=66.187.233.73; helo=mx1.redhat.com; envelope-from=lersek@redhat.com; receiver=edk2-devel@lists.01.org Received: from mx1.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) (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 9652022352290 for ; Wed, 28 Feb 2018 03:08:45 -0800 (PST) Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 4035B4023112; Wed, 28 Feb 2018 11:14:52 +0000 (UTC) Received: from lacos-laptop-7.usersys.redhat.com (ovpn-120-11.rdu2.redhat.com [10.10.120.11]) by smtp.corp.redhat.com (Postfix) with ESMTP id 5B44B10AF9C9; Wed, 28 Feb 2018 11:14:51 +0000 (UTC) To: =?UTF-8?Q?Marvin_H=c3=a4user?= , "edk2-devel@lists.01.org" Cc: "ruiyu.ni@intel.com" , "eric.dong@intel.com" , "star.zeng@intel.com" References: <20180227164940.6956-1-Marvin.Haeuser@outlook.com> From: Laszlo Ersek Message-ID: Date: Wed, 28 Feb 2018 12:14:50 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.6.0 MIME-Version: 1.0 In-Reply-To: X-Scanned-By: MIMEDefang 2.78 on 10.11.54.3 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.6]); Wed, 28 Feb 2018 11:14:52 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.6]); Wed, 28 Feb 2018 11:14:52 +0000 (UTC) for IP:'10.11.54.3' DOMAIN:'int-mx03.intmail.prod.int.rdu2.redhat.com' HELO:'smtp.corp.redhat.com' FROM:'lersek@redhat.com' RCPT:'' Subject: Re: [PATCH 2/2] MdeModulePkg/BaseSerialPortLib16550: Prevent truncating constant values. X-BeenThere: edk2-devel@lists.01.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: EDK II Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 28 Feb 2018 11:08:46 -0000 Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 8bit On 02/27/18 19:55, Marvin Häuser wrote: > Hey Laszlo, > > Thanks for your... detailed explanation. :) > I actually submitted another patch to prevent what you explained - > "[edk2] [PATCH 1/2] MdePkg/Base.h: Ensure safe bitwise operations.", > which marks all BIT defines (and more) as unsigned. > Most definitely I should have mentioned it in the commit message or > held it back till that patch will be accepted (or denied?), seems like > I forgot about that. > Would you still prefer your suggestion even when the Base.h patch is > merged? After all, int might happen to be even larger than INT32, if > I'm not mistaken. > > I'm quite sure VS2015x86 issued the warning despite that commit being > applied locally. It seems to always warn when a constant is truncated, > explicitely or implicitely, to give you the change to increase its > size: > https://msdn.microsoft.com/en-us/library/sz5z1byt.aspx With the other (pre-requisite) change applied, such that #define BIT0 0x00000001u #define BIT1 0x00000002u it's even less comprehensible why VS2015x86 warns about the original code, namely: (UINT16)~(BIT0 | BIT1) In this case we apply bit-or to two "unsigned int" operands, and then apply bit-neg to the "unsigned int" result. Finally we explicitly cast the expression to another unsigned integer type. All well-defined. The MSDN reference writes, "The type conversion causes a constant to exceed the space allocated for it." I don't understand how that does *not* apply to the post-patch code, namely (UINT16)~(UINT16)(BIT0 | BIT1) Again, in this variant (with the BIT macros being unsigned), we basically have (UINT16)(-4) How on earth does the constant "minus 4" (-4) fit in the UINT16 "space"? It totally doesn't. Sorry, VS2015x86 boggles my mind. I think it's a compiler bug, plain and simple. Please just do whatever you have to do to shut it up. I give up. Thanks, Laszlo