Re: *printf and zero size

From: Kurt Roeckx <kurt(at)roeckx(dot)be>
To: Bruce Momjian <pgman(at)candle(dot)pha(dot)pa(dot)us>
Cc: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, PostgreSQL-development <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: *printf and zero size
Date: 2005-12-05 21:45:14
Message-ID: 20051205214514.GA18244@roeckx.be
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Mon, Dec 05, 2005 at 04:35:31PM -0500, Bruce Momjian wrote:
>
> but it seems this is some BSD'ism that we don't need to support if the
> standard doesn't say so.

I think the Linux manpage is more informative about this:

The functions snprintf and vsnprintf do not write more than
size bytes (including the trailing '\0'). If the output was truncated
due to this limit then the return value is the number of characters
(not including the trailing '\0') which would have been written to the
final string if enough space had been available. Thus, a return value
of size or more means that the output was truncated. (See also below
under NOTES.) If an output error is encountered, a negative value is
returned.

[...]

NOTES
The glibc implementation of the functions snprintf() and vsnprintf()
conforms to the C99 standard, i.e., behaves as described above, since
glibc version 2.1. Until glibc 2.0.6 they would return -1 when the out-
put was truncated.

[...]

Concerning the return value of snprintf(), the SUSv2 and the C99 stan-
dard contradict each other: when snprintf() is called with size=0 then
SUSv2 stipulates an unspecified return value less than 1, while C99
allows str to be NULL in this case, and gives the return value (as
always) as the number of characters that would have been written in
case the output string has been large enough.

Kurt

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Tom Lane 2005-12-05 22:03:19 Re: *printf and zero size
Previous Message Qingqing Zhou 2005-12-05 21:44:27 About varlena2