patchPSPP - Patches: patch #5727, fix size_t format specifiers

 
 

You are not allowed to post comments on this tracker with your current authentication level.

patch #5727: fix size_t format specifiers

Submitted by:  Ben Pfaff <blp>
Submitted on:  Wed 07 Feb 2007 05:24:45 AM UTC  
 
Category:  None Item Group:  None
Status:  Done Assigned to:  None
Open/Closed:  Closed

Thu 08 Feb 2007 05:54:48 AM UTC, comment #3:

>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.

Ben Pfaff <blp>
Project Administrator
Wed 07 Feb 2007 11:20:41 PM UTC, comment #2:

I'm not sure what this will actually do for us.

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

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.

Or have I completely misunderstood the way it works?

John Darrington <jmd>
Project Administrator
Wed 07 Feb 2007 01:46:56 PM UTC, comment #1:

Works for me on OBSD, 64-bit x86.

Jason H Stover <jstover>
Project Member
Wed 07 Feb 2007 05:24:45 AM UTC, original submission:

We weren't always formatting "size_t"s correctly (by casting them to another type and using the appropriate format).  This is sure to be a problem on 64-bit machines.  Here's a patch.

Ben Pfaff <blp>
Project Administrator

 

Attached Files
file #11938:  printf.patch added by blp (17KiB - text/x-patch - fix)

 

Depends on the following items: None found

Items that depend on this one: None found

 

Carbon-Copy List
  • -email is unavailable- added by jmd (Posted a comment)
  • -email is unavailable- added by jstover (Posted a comment)
  • -email is unavailable- added by blp (Submitted the item)
  •  

    Do you think this task is very important?
    If so, you can add your encouragement to it.
    This task has 0 encouragements so far.

    Only logged-in users can vote.

     

     

     

    Follow 3 latest changes.

    Date Changed by Updated Field Previous Value => Replaced by
    2007-02-09 blp StatusReady For Test/Review => Done
        Open/ClosedOpen => Closed
    2007-02-07 blp Attached File- => Added printf.patch, #11938

    Back to the top


    Powered by Savane 3.4