Bug#2906: Inconsistency in libc.info re snprintf
Package: glibcdoc
Version: 0.06-1
In the node `Formatted output functions':
- Function: int snprintf (char *S, size_t SIZE, const char *TEMPLATE,
...)
...
The return value is the number of characters stored, not including
the terminating null. If this value equals `SIZE - 1', then there
was not enough space in S for all the output. You should try
again with a bigger output string. Here is an example of doing
this:
...
while (1)
{
/* Try to print in the allocated space. */
int nchars = snprintf (buffer, size,
"value of %s is %s",
name, value);
/* If that worked, return the string. */
if (nchars < size)
return buffer;
/* Else try again with twice as much space. */
size *= 2;
buffer = (char *) xrealloc (size, buffer);
}
}
As you can see, in the example it assumes that failure is indicated by
nchars == size. The ambiguity cannot be resolved from the rest of the
manpage, because it doesn't say whether snprintf still produces a
terminating null when it fails through lack of space.
Ian.
Reply to: