]> git.ipfire.org Git - thirdparty/git.git/commitdiff
don't report vsnprintf(3) error as bug
authorRené Scharfe <l.s.r@web.de>
Sun, 21 Apr 2024 12:40:28 +0000 (14:40 +0200)
committerJunio C Hamano <gitster@pobox.com>
Sun, 21 Apr 2024 19:27:07 +0000 (12:27 -0700)
strbuf_addf() has been reporting a negative return value of vsnprintf(3)
as a bug since f141bd804d (Handle broken vsnprintf implementations in
strbuf, 2007-11-13).  Other functions copied that behavior:

7b03c89ebd (add xsnprintf helper function, 2015-09-24)
5ef264dbdb (strbuf.c: add `strbuf_insertf()` and `strbuf_vinsertf()`, 2019-02-25)
8d25663d70 (mem-pool: add mem_pool_strfmt(), 2024-02-25)

However, vsnprintf(3) can legitimately return a negative value if the
formatted output would be longer than INT_MAX.  Stop accusing it of
being broken and just report the fact that formatting failed.

Suggested-by: Jeff King <peff@peff.net>
Signed-off-by: René Scharfe <l.s.r@web.de>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
mem-pool.c
strbuf.c
wrapper.c

index 3065b12b237a9d9e2314e08306222d33c6f137dd..a3ba38831d94d6fcde4de9c19972c3df7a369ee2 100644 (file)
@@ -4,6 +4,7 @@
 
 #include "git-compat-util.h"
 #include "mem-pool.h"
+#include "gettext.h"
 
 #define BLOCK_GROWTH_SIZE (1024 * 1024 - sizeof(struct mp_block))
 
@@ -122,7 +123,7 @@ static char *mem_pool_strvfmt(struct mem_pool *pool, const char *fmt,
        len = vsnprintf(next_free, available, fmt, cp);
        va_end(cp);
        if (len < 0)
-               BUG("your vsnprintf is broken (returned %d)", len);
+               die(_("unable to format message: %s"), fmt);
 
        size = st_add(len, 1); /* 1 for NUL */
        ret = mem_pool_alloc(pool, size);
index 1492a0822526fe4fa8e982fa36f52cfd40b4b0ad..0d929e4e195fa3db65257142e558938471e7c141 100644 (file)
--- a/strbuf.c
+++ b/strbuf.c
@@ -277,7 +277,7 @@ void strbuf_vinsertf(struct strbuf *sb, size_t pos, const char *fmt, va_list ap)
        len = vsnprintf(sb->buf + sb->len, 0, fmt, cp);
        va_end(cp);
        if (len < 0)
-               BUG("your vsnprintf is broken (returned %d)", len);
+               die(_("unable to format message: %s"), fmt);
        if (!len)
                return; /* nothing to do */
        if (unsigned_add_overflows(sb->len, len))
@@ -404,7 +404,7 @@ void strbuf_vaddf(struct strbuf *sb, const char *fmt, va_list ap)
        len = vsnprintf(sb->buf + sb->len, sb->alloc - sb->len, fmt, cp);
        va_end(cp);
        if (len < 0)
-               BUG("your vsnprintf is broken (returned %d)", len);
+               die(_("unable to format message: %s"), fmt);
        if (len > strbuf_avail(sb)) {
                strbuf_grow(sb, len);
                len = vsnprintf(sb->buf + sb->len, sb->alloc - sb->len, fmt, ap);
index eeac3741cf1a4e90ceab3bef07028aad632ebfb6..f87d90bf5794a55418d657160f3c86735c1e4bd7 100644 (file)
--- a/wrapper.c
+++ b/wrapper.c
@@ -670,7 +670,7 @@ int xsnprintf(char *dst, size_t max, const char *fmt, ...)
        va_end(ap);
 
        if (len < 0)
-               BUG("your snprintf is broken");
+               die(_("unable to format message: %s"), fmt);
        if (len >= max)
                BUG("attempt to snprintf into too-small buffer");
        return len;