Re: Re: StringBuffer overflow
Stefano Corsi <[email protected]> Mon, 7 Jul 2003 15:49:40 +0000
| Newsgroups | gmane.comp.lang.moto.devel |
|---|---|
| Organization | Moto Project |
| Message-ID | <[email protected]> |
>
> That sounds like a real good plan ... could you try it with an initial
> max of 4096 bytes and verify that the gccbufferoverflow test passes
> with the algorithm you sketched out ?
>
Ok. This is the resulting buf_printf. It works and passes=20
gccbufferoverflow test.=20
It should work the old way with buffers < 4096 while for larger buffers i=
t=20
should allocate more space. It has two more variables on the stack and I=20
don't know which impact this could have on performance. There is probably=
=20
some way to do it without these extra variables ... any ideas?
int
buf_printf(StringBuffer *d, const char *format, ...) {
int r, i =3D 1, ssize =3D d->size;
va_list args;
buf_grow(d, (d)->size + BUF_PRINTF_MAXLEN);
fflush(stdout);
va_start(args, format);
while ((r =3D vsnprintf((d)->data + ssize, ((BUF_PRINTF_MAXLEN * i) - =
1),=20
format, args)) > (BUF_PRINTF_MAXLEN * i++)) {
buf_grow(d, (d)->size + (BUF_PRINTF_MAXLEN * i));
}
va_end(args);
return ((d)->size +=3D r);
}
Should I modify buf_vprintf too... ?! What about the NO_LEN_VSPRINTF cpp =
flag?=20
I don't understand clearly the implications.
Stefano