sprintf(3s) query

Guy Harris guy at auspex.UUCP
Tue Dec 20 07:42:00 AEST 1988


>I think the following three points are uncontroversial:
>(1) The (4.2BSD, S5R2, S5R3, Ultrix, SunOS) manual pages for printf(3s)
>    do not say what any of the conditions which can cause an error return
>    are.  (I haven't seen the 4.3BSD manual page.)

I checked the 4.3-tahoe manual, and a quick look reveals no indication
of what the conditions that cause an error return are; I don't have the
4.3BSD page handy, but I don't remember it containing any such
indication.

>(2) There are a lot of errors which sprintf() either converts quietly to
>    something plausible or dumps core with, not least buffer overflow.

This doesn't have to be true of "sprintf", although for most existing
implementations this is true (buffer overflow is harder to catch in most
C implementations than, say, some format-string syntax errors are).

>(3) A portable program cannot rely on sprintf() catching any error at
>    all, and thus had better do its own argument validation in any
>    case.

This is true.

>It would be interesting to hear from someone who has UNIX sources what
>the reported errors are.  

My quick check of the S5R3 source indicates that "printf" and "fprintf". 
return EOF only if "ferror" on the stream being written to indicates an
error, or if "_doprnt" returns EOF, and "sprintf" does so only if
"_doprnt" returns EOF.  "_doprnt", and other I/O functions, set the I/O
error flag on a stream only if a "write" call returns an error
indication.  Since "sprintf" isn't supposed to write anything, this
means it should never return EOF in that implementation.



More information about the Comp.lang.c mailing list