Re: [PATCH 1/4] stringbuf: A string buffer implementation

Kyle Moffett
2007-10-24T21:00:16Z

On Oct 24, 2007, at 15:59:49, Matthew Wilcox wrote:
> +static void sb_vprintf(struct stringbuf *sb, gfp_t gfp, const char
> *format, va_list args)
> +{

[...]

> + s = sb->buf + sb->len;
> + size = vsnprintf(s, sb->alloc - sb->len, format, args);

[...]

> + /* Point to the end of the old string since we already updated -
> >len */
> + s += sb->len - size;
> + vsprintf(s, format, args);

[...]

> +void sb_printf(struct stringbuf *sb, gfp_t gfp, const char
> *format, ...)
> +{
> + va_list args;
> +
> + va_start(args, format);
> + sb_vprintf(sb, gfp, format, args);
> + va_end(args);
> +}

This seems unlikely to work reliably as the various "v*printf"
functions modify the va_list argument they are passed. It may happen
to work on your particular architecture depending on how that
argument data is passed and stored, but you probably actually want to
make a copy of the varargs list for the first vsnprintf call.
Example below:

> va_list argscopy;
> va_copy(argscopy, args);
>
> [...]
>
> size = vsnprintf(s, sb->alloc - sb->len, format, argscopy)
>
> [...]
>
> s += sb->len - size;
> vsprintf(s, format, args);
>
> [...]
>
> va_end(argscopy);

Cheers,
Kyle Moffett