Re: sparse segfaults

Linus Torvalds
2004-11-21T22:37:47Z

On Sun, 21 Nov 2004, linux-os wrote:
> >
> > int tickadj = 500/HZ ? : 1; /* microsecs */
> >
> > which makes it look like sparse doesn't understand such constructions.
>
> I don't think any 'C' compiler should understand such constructions
> either.
> There is no result for the TRUE condition, and the standard
> does not provide for a default. The compiler should have written
> a diagnostic.

Actually, this is documented gcc behaviour, where a missing true condition
is substituted with the condition value.

So what the above does is set "tickadj" to 500/HZ _except_ if that
underflows to zero, in which case tickadj gets the value 1.

IOW, it's the same as

int tickadj = 500/HZ ? 500/HZ : 1;

except that the short syntax is not only shorter, but it's extremely
convenient in macros etc, because it only evaluates the value once, ie you
can do

int tickadj = *ptr++ ? : 1;

and it's well-behaved in that it increments the pointer only once.

Linus