mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Paulo Marques <pmarques@grupopie.com>
To: Shankar Unni <shankarunni@netscape.net>
Cc: linux-kernel@vger.kernel.org, khali@linux-fr.org, bunk@stusta.de,
	akpm@osdl.org
Subject: Re: Do not misuse Coverity please
Date: Wed, 30 Mar 2005 20:14:32 +0100	[thread overview]
Message-ID: <424AFA98.9080402@grupopie.com> (raw)
In-Reply-To: <d2er4p$qp$1@sea.gmane.org>

Shankar Unni wrote:
> Jean Delvare wrote:
> 
>>     v = p->field;
>>     if (!p) return;
>>
>> can be seen as equivalent to
>>
>>     if (!p) return;
>>     v = p->field;
> 
> 
> Heck, no.
> 
> You're missing the side-effect of a null pointer dereference crash (for 
> p->field) (even though v is unused before the return). The optimizer is 
> not allowed to make exceptions go away as a result of the hoisting.

I just had to try this out :)

Using gcc 3.3.2 this code sample:

> struct test {
>   int code;
> };
> 
> int test_func(struct test *a)
> {
>   int ret;
>   if (!a) return -1;
>   ret = a->code;
>   return ret;
> }

is compiled into:

>    0:   8b 54 24 04             mov    0x4(%esp,1),%edx
>    4:   83 c8 ff                or     $0xffffffff,%eax
>    7:   85 d2                   test   %edx,%edx
>    9:   74 02                   je     d <test_func+0xd>
>    b:   8b 02                   mov    (%edx),%eax
>    d:   c3                      ret

whereas this one:

> int test_func(struct test *a)
> {
>   int ret;
>   ret = a->code;
>   if (!a) return -1;
>   return ret;
> }

is simply compiled into:

>    0:   8b 44 24 04             mov    0x4(%esp,1),%eax
>    4:   8b 00                   mov    (%eax),%eax
>    6:   c3                      ret

It seems that gcc is smart enough to know that after we've dereferenced 
a pointer, if it was NULL, it doesn't matter any more. So it just 
assumes that if execution reaches that "if" statement then the pointer 
can not be NULL at all.

So the 2 versions aren't equivalent, and gcc doesn't treat them as such 
either.

Just a minor nitpick, though: wouldn't it be possible for an application 
to catch the SIGSEGV and let the code proceed, making invalid the 
assumption made by gcc?

-- 
Paulo Marques - www.grupopie.com

All that is necessary for the triumph of evil is that good men do nothing.
Edmund Burke (1729 - 1797)

  parent reply	other threads:[~2005-03-30 19:20 UTC|newest]

Thread overview: 26+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-03-27 20:50 [2.6 patch] sound/oss/cs46xx.c: fix a check after use Adrian Bunk
2005-03-27 21:21 ` Do not misuse Coverity please (Was: sound/oss/cs46xx.c: fix a check after use) Jean Delvare
2005-03-27 21:43   ` Adrian Bunk
2005-03-27 22:34     ` Jean Delvare
2005-03-27 22:45       ` Russell King
2005-03-28 12:54       ` Matthias-Christian Ott
2005-03-28 23:57     ` L. A. Walsh
2005-03-29  6:05       ` Daniel Barkalow
2005-03-29  6:23   ` Andrew Morton
2005-03-29 10:46     ` Jean Delvare
2005-03-29 14:12       ` Chris Friesen
2005-03-30  1:25       ` Horst von Brand
2005-03-30  7:53         ` Do not misuse Coverity please Jean Delvare
2005-03-30 17:09           ` Horst von Brand
2005-04-11 20:23             ` Pavel Machek
2005-03-30 18:29           ` Shankar Unni
2005-03-30 18:55             ` Olivier Galibert
2005-03-31  2:01               ` Patrick McFarland
2005-03-30 19:14             ` Paulo Marques [this message]
2005-03-30 23:11               ` Big GCC bug!!! [Was: Re: Do not misuse Coverity please] Kyle Moffett
2005-03-30 23:38                 ` Not a GCC bug (was Re: Big GCC bug!!! [Was: Re: Do not misuse Coverity please]) Jakub Jelinek
2005-03-31  0:58                   ` Kyle Moffett
2005-03-31  1:12                     ` Nick Piggin
2005-03-31  1:27                       ` Kyle Moffett
2005-03-29 14:22     ` Do not misuse Coverity please (Was: sound/oss/cs46xx.c: fix a check after use) Daniel Jacobowitz
2005-03-29 22:37       ` Kyle Moffett

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=424AFA98.9080402@grupopie.com \
    --to=pmarques@grupopie.com \
    --cc=akpm@osdl.org \
    --cc=bunk@stusta.de \
    --cc=khali@linux-fr.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=shankarunni@netscape.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

Powered by JetHome