2003-05-02 17:57:45

by Urs Thuermann

[permalink] [raw]
Subject: Re: [BUG] settimeofday(2) succeeds for microsecond value more than USEC_PER_SEC and for negative value

george anzinger <[email protected]> writes:

> Uh, sure. This is the test I prefer:
>
> if( (unsigned long)tv->usec > USEC_PER_SEC)
> return EINVAL;
>
> Note that the unsigned picks up the negative value as well as the >
> (and it does it in only one machine code test/jmp :)

No, don't do the compilers job. Just write

if (tv->usec < 0 || tv->usec >= USEC_PER_SEC) { ... }

This is easier to read, portable, and generates the same machine code
as your C code, at least in all gcc versions since gcc-2.7.2.3 (I
don't have older gcc versions here on my machine to test).


urs