membuffer: be defensive about bad C library vsnprintf implementations

Dirk reports that some Windows users have had odd corruption in the
commit messages in the cloud storage.  They make no sense at all unless
there is some very weird Windows library bug.

The prime suspect is 'vsnprintf()' returning a negative error when the
target buffer is too small (rather than the proper "this is how much
space it would need").  That is a very traditional C library bug that I
thougth had been fixed everywhere, but there doesn't really seem to be a
lot of other likely causes.

So let's make our membuffer code be defensive against bad libraries that
return negative error numbers from vsnprintf.

Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
This commit is contained in:
Linus Torvalds 2020-10-01 13:01:33 -07:00 committed by Dirk Hohndel
parent 5f79ceef5e
commit d8f35711ff

View file

@ -110,6 +110,18 @@ void put_vformat(struct membuffer *b, const char *fmt, va_list args)
len = vsnprintf(target, room, fmt, copy);
va_end(copy);
// Buggy C library?
if (len < 0) {
// We have to just give up at some point
if (room > 1000)
return;
// We don't know how big an area we should ask for,
// so just expand our allocation by 50%
room = room * 3 / 2;
continue;
}
if (len < room) {
b->len += len;
return;