pspp-dev
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[patch #5727] fix size_t format specifiers


From: Ben Pfaff
Subject: [patch #5727] fix size_t format specifiers
Date: Thu, 08 Feb 2007 05:54:49 +0000
User-agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.1.1) Gecko/20061205 Iceweasel/2.0.0.1 (Debian-2.0.0.1+dfsg-1)

Follow-up Comment #3, patch #5727 (project pspp):

>1. Any sane implementation of printf would cast to the appropriate
>type internally. 

The implementation of printf has little to do with it.  The compiler passes
arguments to printf as the types that are supplied.  It doesn't know what
format specifier(s) are given.  Thus, if you pass a 64-bit size_t for a %d
argument, and on that machine "int" is 32 bits, then what you print is half
garbage.

Now, GCC is a little smarter and knows about printf, but that doesn't change
how it passes arguments, it just allows it to print out helpful warnings.

>2. In some cases you're casting from size_t to int. If size_t on a
>particular achitecture is 64 bits, and int is 32bits, then 
>confusing things could happen. 

The changes I made all have the effect of making the argument match the
format specifier: if I cast to int, then I use %d; if I cast to unsigned
long, then I use %lu; and so on.  Arguably I could have chosen different
types in some cases, but I tried to choose what made sense to me at the
time.

I'm hoping that someday gnulib will replace printf-like functions with
versions that accept the "z" format specifier (which was introduced in C99
for printing size_t), so that instead of these casts we can just write "%zu".
 But that day is not here yet.

    _______________________________________________________

Reply to this item at:

  <http://savannah.gnu.org/patch/?5727>

_______________________________________________
  Message sent via/by Savannah
  http://savannah.gnu.org/





reply via email to

[Prev in Thread] Current Thread [Next in Thread]