On October 30, 2003 07:28 pm, Ferenc Wagner wrote:
if (n > -1 && n < size) return p;
size = min( size*2, n+1 );
if (n > -1 && (size_t)n < size) return p;
size = min( size*2, (size_t)n+1 );
Once at it, could you please explain to me what the hell is going on here (min)?
Well, the way I see it, it's just trying to minimize memory usage in a portable manner. Normally, vsnprintf() should return the necessary size of the buffer (in n), if the current size is too small. If however vsnprintf() fails to return that, we should just double the size of the buffer until we have something large enough.
Now, the way the code it's written right now will not work if vsnprintf() returns -1 for failure. Question is, do we really need to worry about vsnprintf() not returning the correct size of the required buffer?
BTW, IIRC the code has been taken from one of glibc's man pages...