[OpenAFS-devel] Re: [OpenAFS] sprintf -> snprintf...

Russ Allbery rra@stanford.edu
Tue, 01 Jul 2003 13:55:51 -0700


R Lindsay Todd <toddr@rpi.edu> writes:

> Well, if the platform has a specific way to represent pointers, it is
> true we can't do that without some platform specific knowledge.  But
> having just looked into this a bit more, I see that %p is supposed to be
> a void* pointer.  We know how long these are: sizeof(void*).  We can
> print them out a character at a time, in hex.  This should be portable,
> shouldn't it?

Not really.  You're inherently making the assumption that a void * pointer
is a contiguous block of memory accessible a character at a time and that
the results of printing it in that fashion is meaningful.  The C standard,
as I understand it, doesn't make those guarantees.

However, as mentioned earlier, none of the platforms on which OpenAFS will
currently compile break any of those assumptions, so it's not clear that
anyone would actually care.

> What is currently found in OpenAFS is instances of:  sprintf(buf, "%x",
> ptr).  There isn't even a cast, just a presumption that stdarg treats
> ptrs as ints.  These are caught by splint, so they could be fixed either
> by casting to intmax_t, or by implementing %p.

This is certainly less portable.

> The fact that this "works" is either evidence that it really doesn't
> matter, or that very few people run the volserver in very verbose modes
> and study the logging output.

Agreed.

-- 
Russ Allbery (rra@stanford.edu)             <http://www.eyrie.org/~eagle/>