[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: |
Ivan Shmakov |
Subject: |
Re: [PATCH 1/3] ipc: avoid dereference of null pointer and quiet the GCC warning about uninitialized variable |
Date: |
Wed, 18 Dec 2013 10:26:51 +0000 |
User-agent: |
Gnus/5.13 (Gnus v5.13) Emacs/24.3 (gnu/linux) |
>>>>> Marin Ramesa <mpr@hi.t-com.hr> writes:
>>>>> 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 [FALSE]. If !a equals 0, that means the
> entry is a null pointer, and evaluation of !b is a dereference of a
> null pointer.
Well, when explaining these operators to the students, I’d
rather call them “conditionals” (along with A ? B : C and ‘if’.)
Specifically:
• the A && B operator evaluates the A expression; if the result
is zero, zero is returned; if the result is non-zero, the B
expression is evaluated, and its result is returned;
• the A || B operator evaluates the A expression; if the result
is non-zero, it is returned; if the result is zero, the B
expression is evaluated, and its result is returned.
Essentially, A && B is equivalent to (A ? B : 0), while A || B
is equivalent to (A ? A : B), except that in latter case, A is
evaluated only once. (Thus A++ || B doesn’t result in A being
incremented twice, as would be in the case of A++ ? A++ : B.)
One may wish to check the following simplictic C code.
#include <stdio.h>
int
main ()
{
int a = 0, b = 42;
int c
= a && puts ("a is true");
int d
= a || puts ("a is false");
int e
= b && puts ("b is true");
int f
= b || puts ("b is false");
/* . */
return 0;
}
Note that only some of the puts () calls are in fact evaluated.
--
FSF associate member #7257
- Re: [PATCH 1/3] ipc: avoid dereference of null pointer and quiet the GCC warning about uninitialized variable, (continued)
- Re: [PATCH 1/3] ipc: avoid dereference of null pointer and quiet the GCC warning about uninitialized variable, Richard Braun, 2013/12/18
- Re: [PATCH 1/3] ipc: avoid dereference of null pointer and quiet the GCC warning about uninitialized variable, Richard Braun, 2013/12/18
- Re: [PATCH 1/3] ipc: avoid dereference of null pointer and quiet the GCC warning about uninitialized variable, Marin Ramesa, 2013/12/18
- Re: [PATCH 1/3] ipc: avoid dereference of null pointer and quiet the GCC warning about uninitialized variable, Marin Ramesa, 2013/12/18
- Re: [PATCH 1/3] ipc: avoid dereference of null pointer and quiet the GCC warning about uninitialized variable, Richard Braun, 2013/12/18
- Re: [PATCH 1/3] ipc: avoid dereference of null pointer and quiet the GCC warning about uninitialized variable, Marin Ramesa, 2013/12/18
- Re: [PATCH 1/3] ipc: avoid dereference of null pointer and quiet the GCC warning about uninitialized variable, Richard Braun, 2013/12/18
- Re: [PATCH 1/3] ipc: avoid dereference of null pointer and quiet the GCC warning about uninitialized variable, Marin Ramesa, 2013/12/18
- Re: [PATCH 1/3] ipc: avoid dereference of null pointer and quiet the GCC warning about uninitialized variable, Richard Braun, 2013/12/18
- Re: [PATCH 1/3] ipc: avoid dereference of null pointer and quiet the GCC warning about uninitialized variable, Marin Ramesa, 2013/12/18
- Re: [PATCH 1/3] ipc: avoid dereference of null pointer and quiet the GCC warning about uninitialized variable,
Ivan Shmakov <=