diff options
author | Linus Torvalds <torvalds@linux-foundation.org> | 2020-10-01 13:01:33 -0700 |
---|---|---|
committer | Dirk Hohndel <dirk@hohndel.org> | 2020-10-01 13:43:21 -0700 |
commit | d8f35711ff1f79fc8d2d39e53d6cfbb0db254ecc (patch) | |
tree | f786446142f58aa9524da395bb515040d1bed1d4 | |
parent | 5f79ceef5e4091e4003474ba740339953bf08bfb (diff) | |
download | subsurface-d8f35711ff1f79fc8d2d39e53d6cfbb0db254ecc.tar.gz |
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>
-rw-r--r-- | core/membuffer.c | 12 |
1 files changed, 12 insertions, 0 deletions
diff --git a/core/membuffer.c b/core/membuffer.c index d0e5160ef..3b42797e1 100644 --- a/core/membuffer.c +++ b/core/membuffer.c @@ -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; |