[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: error reporting

From: Edward Welbourne
Subject: Re: error reporting
Date: Wed, 09 Apr 2014 08:12:09 +0000

> Note that in Unix, vsnprintf() returns the TOTAL number of chars
> needed (add 1 for the null).

This is not correct.  The buffer size (that you pass in) is the total
number of bytes available (and the most the function shall use,
including the terminator); but the *return* is the strlen() that would
have resulted had the buffer been big enough.  If this is the buffer
size you passed in, the output has been truncated, in order to fit in
the terminator, by one byte; but the return doesn't include the

> If the output would overflow the buffer, then you would get a return
> value larger than the specified buffer size.

Yes, and you need to add one to the return value to get the buffer size
you need for the subsequent call that'll get the job done properly.  The
function hasn't added one for the terminator: you need to do that.

> In Windoze, vsnprintf() will return -1 if the buffer would be
> overflowed,

This is quite usual amongst iron-age implementations of libc.


reply via email to

[Prev in Thread] Current Thread [Next in Thread]