Subject: Re: [PATCH 03/14] Define SNPRINTF_SIZE_CORR=1 for Microsoft Visual C++

From: Johannes Sixt <[email-masked]>
Date: Fri, 21 Aug 2009 23:41:37 +0200

On Freitag, 21. August 2009, Junio C Hamano wrote:
> Marius Storm-Olsen <[email-masked]> writes:
> > From: Frank Li <[email-masked]>
> >
> > The Microsoft C runtime's vsnprintf function does not add NUL at
> > the end of the buffer.
>
> This contradicts the way I read the comment in compat/snprintf.c from
> f4626df (Add target architecture MinGW., 2007-12-01).
>
> As far as I can see, the correction is about the meaning of the size
> parameter to the function, namely, that a broken implementation may
> mistakenly think that it was told the maximum length of the meat of the
> string to write, and it is allowed to append a NUL beyond the limit, when
> the caller actually is telling it the size of the buffer.

Not quite. The parameter *is* the size of the buffer and vsnprintf does not 
write beyond the buffer. However, it has the awkward behavior that if the 
buffer is too short by exactly one byte, i.e. there is room for the complete 
string, but not for the terminating NUL, then vsnprintf does not report an 
error!

Therefore, we tell vsnprintf that the buffer is shorter by one byte and always 
write NUL in the last position so that we get a correctly terminated string 
even in this case.

-- Hannes