[Python-Dev] test_builtin failing? or just 64-bit platforms

Guido van Rossum guido@python.org
Sat, 01 Dec 2001 03:06:01 -0500


> > Hm, snprintf is *supposed* to truncate the result,
> 
> According to C99, yes, but MS has its own pre-C99 snprintf semantics, and
> glibc has had at least two different versions.
> 
> > but it seems that here it refused to do anything.  Maybe PyOS_snprintf
> > should be a wrapper that checks the return value of snprintf?
> 
> Check it for what?  We can't (at least not yet) count on uniform behavior
> across platform snprintf implementations.

See below.  On my system, the snprintf man page documents both
behaviors, with the gcc version where it switches.

> > I noticed that *none* of the recently checked-in PyOS_snprintf calls
> > have their return value checked,
> 
> They were all derived (and most mindlessly) from existing sprintf calls that
> didn't check either.  A goal of this transformation was to change as little
> as possible.
> 
> > and I think it's better to leave it that way (since in many cases we
> > really don't know what to do instead) -- maybe PyOS_snprintf should
> > even return void (or does it already?).
> 
> The comments suggest it wants to return the C99-defined value (an int, < 0
> for an encoding error, else the number of characters (excluding \0) written
> if the size is big enough, else the number of characters that would have
> been written (excluding \0) had size been big enough.  So the output was
> converted in full iff the return value is non-negative and strictly less
> than the size passed in.  That's fine by me, and I'll note that MS snprintf
> meets that much too (if size isn't big enough, it returns a negative
> result).  So perhaps in a debug build PyOS_snprintf could assert that the
> returned value is non-negative and less than the passed-in size ...

Should work for gcc too.

Note that the Tru64 output suggests that the behavior (== effect on
the buffer) may differ too: a truncated version of the desired output
may or may not be written to the buffer.  This can be addressed by
pre-filling the buffer with a useful pattern in PyOS_snprintf before
calling the system's snprintf -- e.g. "*-*-*-*" (to give a patriotic
example :-).

--Guido van Rossum (home page: http://www.python.org/~guido/)

PS: I feel like I'm writing this on borrowed time -- Comcast and
Excite@Home haven't disconnected me from the net yet. :-)