os_stream: fix bugs in allocation path

This commit is contained in:
Luca Barbieri 2010-08-21 00:39:48 +02:00
parent 9960200d5e
commit 132b9439e2
1 changed files with 8 additions and 16 deletions

View File

@ -9,28 +9,20 @@ os_default_stream_vprintf (struct os_stream* stream, const char *format, va_list
{
char buf[1024];
int retval;
retval = util_vsnprintf(buf, sizeof(buf), format, ap);
va_list ap2;
va_copy(ap2, ap);
retval = util_vsnprintf(buf, sizeof(buf), format, ap2);
va_end(ap2);
if(retval <= 0)
{}
else if(retval < sizeof(buf))
stream->write(stream, buf, retval);
else
{
int alloc = sizeof(buf);
char* str = NULL;
for(;;)
{
alloc += alloc;
if(str)
FREE(str);
str = MALLOC(alloc);
if(!str)
return -1;
retval = util_vsnprintf(str, alloc, format, ap);
} while(retval >= alloc);
char* str = MALLOC(retval + 1);
if(!str)
return -1;
retval = util_vsnprintf(str, retval + 1, format, ap);
if(retval > 0)
stream->write(stream, str, retval);
FREE(str);