bug-hurd
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [PATCH 1/3] ipc: avoid dereference of null pointer and quiet the GCC


From: Richard Braun
Subject: Re: [PATCH 1/3] ipc: avoid dereference of null pointer and quiet the GCC warning about uninitialized variable
Date: Wed, 18 Dec 2013 11:11:10 +0100
User-agent: Mutt/1.5.21 (2010-09-15)

On Wed, Dec 18, 2013 at 10:55:47AM +0100, Marin Ramesa wrote:
> On 18.12.2013 10:46:40, Richard Braun wrote:
> > No, that's wrong. The && and || operators are guaranteed to be
> > evaluated left-to-right, and yield if the first operand compares 
> > equal to 0. And that's exactly why this check against NULL is done 
> > first.
> 
> In the expression (!a && !b), if !a equals 0, the compiler must check 
> !b == 0 in order to return TRUE. If !a equals 0, that means the entry 
> is a null pointer, and evaluation of !b is a dereference of a null 
> pointer.

The expression is ((a == NULL) || a->something), and I agree it is
equivalent to !((a != NULL) && !a->something). And again, both the
&& and || operators are guaranteed to be evaluated left-to-right and
*yield* without evaluating the second operand if the first compares
or not to 0, depending on the operator.

So, let's take the seconds form since that's what you've used, without
the negation for simplicity : ((a != NULL) && !a->something)

If a isn't NULL, then it returns !a->something. If a is NULL, then
(a != NULL) compares equal to 0, and && returns 0 before evaluating
!a->something. So no, there can't be a null pointer dereference here.

And this really is basic C, so please double check your changes.

-- 
Richard Braun



reply via email to

[Prev in Thread] Current Thread [Next in Thread]