Skip to content

Commit f9c6e64

Browse files
committed
Fix buffer forwarding to vsnprintf to not resize by one at a time
1 parent d8484d1 commit f9c6e64

File tree

2 files changed

+7
-10
lines changed

2 files changed

+7
-10
lines changed

sostream.cc

+4-5
Original file line numberDiff line numberDiff line change
@@ -121,14 +121,13 @@ int ostringstream::vformat (const char* fmt, va_list args)
121121
#undef __va_copy
122122
#define __va_copy(x,y)
123123
#endif
124-
size_t rv, space;
124+
int rv, space;
125125
do {
126126
space = remaining();
127127
__va_copy (args2, args);
128-
rv = vsnprintf (ipos(), space, fmt, args2);
129-
if (ssize_t(rv) < 0)
130-
rv = space;
131-
} while (rv >= space && rv < overflow(rv + 1));
128+
if (0 > (rv = vsnprintf (ipos(), space, fmt, args2)))
129+
return (rv);
130+
} while (rv >= space && rv < (int)overflow(rv+1));
132131
SetPos (pos() + min (rv, space));
133132
return (rv);
134133
}

ustring.cc

+3-5
Original file line numberDiff line numberDiff line change
@@ -327,14 +327,12 @@ int string::vformat (const char* fmt, va_list args)
327327
#undef __va_copy
328328
#define __va_copy(x,y)
329329
#endif
330-
size_t rv = size();
330+
int rv = size();
331331
do {
332-
reserve (rv);
332+
resize (rv);
333333
__va_copy (args2, args);
334334
rv = vsnprintf (data(), memblock::capacity(), fmt, args2);
335-
rv = min (rv, memblock::capacity());
336-
} while (rv > capacity());
337-
resize (min (rv, capacity()));
335+
} while (rv >= (int)memblock::capacity());
338336
return (rv);
339337
}
340338

0 commit comments

Comments
 (0)