https://gcc.gnu.org/bugzilla/show_bug.cgi?id=116834

Harald van Dijk <harald at gigawatt dot nl> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |harald at gigawatt dot nl

--- Comment #4 from Harald van Dijk <harald at gigawatt dot nl> ---
(In reply to Sam James from comment #3)
> Please provide preprocessed source for the original.

Not entirely preprocessed (due to using __SIZE_TYPE__ rather than its expanded
version) but a self-contained compilable version of the original code from
comment #2 is:

typedef struct bstr {
    unsigned char *start;
    __SIZE_TYPE__ len;
} bstr;

__SIZE_TYPE__ talloc_get_size(const void *);
void resize_append(void *, bstr *, __SIZE_TYPE__);
void bstr_xappend(void *, bstr *, bstr);

static inline struct bstr bstr0(const char *s)
{
    return (struct bstr){(unsigned char *)s, s ? __builtin_strlen(s) : 0};
}

void bstr_xappend_vasprintf(void *talloc_ctx, bstr *s, const char *fmt,
                            __builtin_va_list ap)
{
    int size;
    __builtin_va_list copy;
    __builtin_va_copy(copy, ap);
    __SIZE_TYPE__ avail = talloc_get_size(s->start) - s->len;
    char *dest = s->start ? s->start + s->len : 0;
    size = __builtin_vsnprintf(dest, avail, fmt, copy);
    __builtin_va_end(copy);

    if (size < 0) {
        bstr_xappend(talloc_ctx, s, bstr0("format error: "));
        bstr_xappend(talloc_ctx, s, bstr0(fmt));
        return;
    }

    if (avail < 1 || size + 1 > avail) {
        resize_append(talloc_ctx, s, size + 1);
        __builtin_vsnprintf(s->start + s->len, size + 1, fmt, ap);
    }
    s->len += size;
}

$ gcc-14 -fsanitize=undefined -Wformat -O2 -c bug116834.c
bug116834.c: In function ‘bstr_xappend_vasprintf’:
bug116834.c:23:12: warning: null format string [-Wformat-truncation=]
   23 |     size = __builtin_vsnprintf(dest, avail, fmt, copy);
      |            ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
$ gcc-14 --version
gcc-14 (Ubuntu 14.2.0-4ubuntu2~24.04) 14.2.0
Copyright (C) 2024 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Reply via email to