Re: [patch 143/166] preadv/pwritev: Add preadv and pwritev system calls.

Andrew Morton
2009-04-04T17:45:26Z

On Fri, 3 Apr 2009 08:03:22 -0700 (PDT) Linus Torvalds <torvalds@linux-foundation.org> wrote:

> +static inline loff_t pos_from_hilo(unsigned long high, unsigned long low)
> +{
> +#define HALF_LONG_BITS (BITS_PER_LONG / 2)
> + return ((high << HALF_LONG_BITS) << HALF_LONG_BITS) | low;
> +}

Does C promote the `unsigned long high' beforehand, or will the
intermediate expression overflow?

<does it the old-fashioned way>

y:/home/akpm> cat t.c

#define HALF_LONG_BITS 16

main()
{
unsigned hi = 0x43;
unsigned lo = 0x42;
unsigned long long res;

res = ((hi << 16) << 16) | lo;
printf("%llx\n", res);
}
y:/home/akpm> gcc -O t.c
y:/home/akpm> ./a.out
42

I think it's wrong on 32-bit?

Also, HALF_LONG_BITS is 32 on 64-bit, so "high" gets shifted to zero.
It's unclear whether this was deliberate, but either way, it's a sneaky
trick and deserves a code comment!