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

            Bug ID: 115185
           Summary: Missing "too long" warning when string-array size
                    doesn't include NULL byte
           Product: gcc
           Version: 14.1.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: Hi-Angel at yandex dot ru
  Target Milestone: ---

It's a common pattern to have some `enum Foo { one, two, three }`, and then
some 2-dimensinal array `FooAsString`, which allows one to get name of the
`Foo` field by passing an index, like `printf("%s",
FooAsString[my_Foo_variable])`.

Problem though is that it isn't allowed to declare multi-dimensinal array and
omit all dimensions, so one have to declare as `FooAsString[][6]` for example.
That makes programmer to rely on the compiler to detect when the `6` is not
enough to hold the string.

GCC kind of tries to warn about that, however its check is off-by-one because
it does not take into account the NULL byte that the C-string has to end with.


# Steps to reproduce (in terms of terminal commands)

    λ cat test.c
    #include <stdio.h>

    int main() {
        enum Foo {
            hello,
        } obj = hello;
        static const char description[][5] = {[hello] = "hello"};

        printf("%s", description[obj]);
    }
    λ gcc test.c -o a -Wall -Wextra


## Expected

A warning:

    test.c:7:53: warning: initializer-string for array of ‘char’ is too long
        7 |     static const char description[][5] = {[hello] = "hello"};

## Actual

No warnings.

Reply via email to