Properly handle the case where vsnprintf returns -1.
This commit is contained in:
parent
d559fbd803
commit
3a503499aa
|
@ -79,18 +79,18 @@ char* strmake(const char* fmt, ...)
|
||||||
{
|
{
|
||||||
int n;
|
int n;
|
||||||
size_t size = 100;
|
size_t size = 100;
|
||||||
char* p;
|
|
||||||
va_list ap;
|
va_list ap;
|
||||||
|
|
||||||
p = xmalloc (size);
|
|
||||||
while (1)
|
while (1)
|
||||||
{
|
{
|
||||||
|
char *p = xmalloc (size);
|
||||||
va_start(ap, fmt);
|
va_start(ap, fmt);
|
||||||
n = vsnprintf (p, size, fmt, ap);
|
n = vsnprintf (p, size, fmt, ap);
|
||||||
va_end(ap);
|
va_end(ap);
|
||||||
if (n > -1 && (size_t)n < size) return p;
|
if (n == -1) size *= 2;
|
||||||
size = min( size*2, (size_t)n+1 );
|
else if ((size_t)n >= size) size = n + 1;
|
||||||
p = xrealloc (p, size);
|
else return p;
|
||||||
|
free(p);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue