[quagga-dev 4391] Re: bgpd printf size_t warning

Andrew J. Schorr aschorr at telemetry-investments.com
Sat Sep 23 17:35:49 BST 2006


On Fri, Sep 22, 2006 at 09:06:09PM +0100, Paul Jakma wrote:
> I'd prefer the cast to %z right now, just not supported widely 
> enough.

That was my sense (too soon to use %z).

> Cast will make it awfully hard later to find printf()'s of 
> size_t though, as/when %z support becomes available enough to use.

But that's not really a problem unless you're worried that casting
a size_t to u_long is somehow problematic.  I don't see anything
wrong with casting size_t to u_long, unless we end up trying to
support a platform where size_t is wider than u_long.  Are you aware
of any such platforms?

> Would it be possible to just use %lu, without cast? That'll stop 
> warnings on ILP32 machines anyway, while still allowing the places 
> concerned to be found easily (by compiling on LP64).

In my experiments, that doesn't solve the problem:

   bash-2.05b$ cat printf_sizeof.c
   #include <stdio.h>

   int
   main(int argc, char **argv)
   {
      size_t x = argc;
      printf("%s: argc as a size_t is %lu\n",argv[0],x);
      return 0;
   }

On 32-bit linux with gcc 3.3.2:

   bash-2.05b$ gcc -E printf_sizeof.c | grep 'typedef.* size_t;'
   typedef unsigned int size_t;

   bash-2.05b$ gcc -Wall -o a.out printf_sizeof.c
   printf_sizeof.c: In function `main':
   printf_sizeof.c:7: warning: long unsigned int format, size_t arg (arg 3)

On 64-bit linux with gcc 4.0.2 in 32-bit mode:

   bash-3.00$ gcc -m32 -E printf_sizeof.c | grep 'typedef.* size_t;'
   typedef unsigned int size_t;

   bash-3.00$ gcc -m32 -Wall -o a.out printf_sizeof.c
   printf_sizeof.c: In function ‘main’:
   printf_sizeof.c:7: warning: format ‘%lu’ expects type ‘long unsigned int’, but argument 3 has type ‘size_t’

On 64-bit linux with gcc 4.0.2 in 64-bit mode:

   bash-3.00$ gcc -m64 -E printf_sizeof.c | grep 'typedef.* size_t;'
   typedef long unsigned int size_t;
   bash-3.00$ gcc -m64 -Wall -o a.out printf_sizeof.c

So I see the opposite: the warning message persists on ILP32, but the
warning message goes away on LP64...

Regards,
Andy



More information about the Quagga-dev mailing list