From: Ian Jackson Subject: Re: [PATCH] libxc/pm: Fix NULL pointer checks. Date: Tue, 10 Sep 2013 16:02:46 +0100 Message-ID: <21039.13462.170828.789414@mariner.uk.xensource.com> References: <1378805379-18700-1-git-send-email-andrew.cooper3@citrix.com> <522F2B5802000078000F1DEF@nat28.tlf.novell.com> <1378817307.21748.110.camel@kazak.uk.xensource.com> Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Return-path: Received: from mail6.bemta5.messagelabs.com ([195.245.231.135]) by lists.xen.org with esmtp (Exim 4.72) (envelope-from ) id 1VJPTN-0001cl-Qo for xen-devel@lists.xenproject.org; Tue, 10 Sep 2013 15:03:22 +0000 In-Reply-To: <1378817307.21748.110.camel@kazak.uk.xensource.com> List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Sender: xen-devel-bounces@lists.xen.org Errors-To: xen-devel-bounces@lists.xen.org To: Ian Campbell Cc: Andrew Cooper , Jan Beulich , xen-devel List-Id: xen-devel@lists.xenproject.org Ian Campbell writes ("Re: [Xen-devel] [PATCH] libxc/pm: Fix NULL pointer checks."): > On Tue, 2013-09-10 at 13:23 +0100, Jan Beulich wrote: > > I'm very surprised the compiler didn't reject this - I'm unaware of > > an extension that would allow pointers to be compared by other > > than == and != (plus it's all but clear what e.g. a "negative" > > pointer really is). It is legal to compare for inequality[1] pointers into the same object. However, it is not legal to compare for inequality any null pointer; doing so is undefined behaviour. C99 6.5.8(5). > We were just discussing this at lunch and couldn't work it out either, > but indeed both gcc 4.7.[23] and clang 3.2 accept this when building > with -Wall: > int main(int argc, char **argv) > { > if ( argv[1] < 0 ) > printf("ARGV[1] < 0\n"); > else > printf("ARGV[1] >= 0\n"); > return 0; I think it would be legal for this program to be compiled into #!/bin/sh echo hahahah In the future, I wouldn't be surprised if a compiler were to maliciously optimise away the test and one of the arms, or perhaps the whole of the function. Ian. [1] < > <= >= are inequalities. == and != are not.