Skip to content

-Wformat-security false-positive in consteval functions #126389

@vvd170501

Description

@vvd170501

Reproducer:

void FormatFunc(const char* format, ...) __attribute__((__format__(__printf__, 1, 2)));

consteval void Foo() {
    if (false) {
        FormatFunc("test");  // error: format string is not a string literal (potentially insecure) [-Werror,-Wformat-security]
    }
}

constexpr void Bar() {
    if (false) {
        FormatFunc("test");  // ok in constexpr
    }
}

consteval void Baz() {
    if (false) {
        FormatFunc("%s", "test");  // Adding a second arg somehow fixes the warning
    }
}

The warning is present in clang trunk (https://godbolt.org/z/q336xWjzb) and in all recent versions of clang (I checked clang16-clang19).

A more realistic example:
[[noreturn]] void Panic(const char* format, ...) noexcept __attribute__((__format__(__printf__, 1, 2)));

// assert()-like macro which allows adding description to failure message
#define FANCY_ASSERT(x, ...)    \
    if (!(x)) {                 \
        Panic(" " __VA_ARGS__);   \
    }

constexpr int NonZeroConstexpr(int x) {
    FANCY_ASSERT(x);
    return x;
}

constexpr int x1 = NonZeroConstexpr(1);  // ok
// constexpr int x2 = NonZeroConstexpr(0);  // error - Panic(...) is not constexpr

// Same, but with consteval
consteval int NonZeroConsteval(int x) {
    FANCY_ASSERT(x);  // error: format string is not a string literal (potentially insecure) [-Werror,-Wformat-security]
    return x;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    clang:diagnosticsNew/improved warning or error message in Clang, but not in clang-tidy or static analyzerfalse-positiveWarning fires when it should not

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions