cURL / Mailing Lists / curl-library / Single Mail

curl-library

Not passing "long" to varargs (was Re: curl_easy_setopt and typeof)

From: Jamie Lokier <jamie_at_shareable.org>
Date: Sun, 13 Apr 2008 17:04:35 +0100

David Shaw wrote:
> The issue is the bit-field, not whether the bits are int or long.
> This fails in the same way:
>
> struct
> {
> long myflag:1;
> long myotherflag:1;
> long verify_peer:1;
> } flags;
>
> curl_easy_setopt( curl, CURLOPT_SSL_VERIFYPEER, flags.verify_peer );
>
> In that case, I am indeed passing a long.

No, it isn't passed as a long. It's passed as an int. The "long" in
the bitfield definition is not meaningful.

And the earlier thread which concluded "int" and "long" are in
practice both usable as arguments turns out to be wrong.

I've just tried this little program on x86_64:

    #include <stdio.h>
    struct my_btype { long x : 1; } b = { -1 };
    int main (int argc, char ** argv)
    {
      printf ("%ld\n", (long) b.x);
      printf ("%ld\n", b.x);
    }

    $ gcc test.c -o test -O2 -m64
    $ qemu-x86_64 ./test
    -1
    4294967295

As you can see, the cast to long is important.

The second line of output isn't guaranteed to be the same with every
compiler: it has undefined behaviour, because an int argument is
passed to a varargs function expecting long. It could even crash on
some systems.

-- Jamie
Received on 2008-04-13