llvm / llvm/llvm-project

initializer-string for char array is too long should be a warning in C++ mode when array size matches string length without null terminator

Open
#207,941 3 comments 0 reactions 0 assignees View on GitHub
c++ clang:diagnostics
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

# observation
```
echo -e '#include \nint main() { char buffer[2] = "hi"; write(STDOUT_FILENO, buffer, 2); }' > test.c
zig cc test.c
./a.out
mv test.c test.c++
zig c++ test.c++
```
This compiles fine in C, but is a hard error in C++:

> test.c++:2:31: error: initializer-string for char array is too long, array size is 2 but initializer has size 3 (including the null terminating character)

Forcing people to change to {'h','i'} or cursed casts everywhere reduces readability.

# expectation
Make this a work like in C, or add a specific flag like -Wno-initializer-string-too-long.

# versions
```
$ zig cc --version
clang version 21.1.0
Target: x86_64-unknown-linux6.17.0-gnu2.39.0
Thread model: posix
InstalledDir: zig-x86_64-linux-0.16.0
$ zig c++ --version
clang version 21.1.0
Target: x86_64-unknown-linux6.17.0-gnu2.39.0
Thread model: posix
InstalledDir: zig-x86_64-linux-0.16.0
```

Contributor guide

Open the contributing guide

Research direction

Start by running the issue's minimal reproduction with both `zig cc` and `zig c++`, then trace the Clang diagnostic for a character array initialized without room for a null terminator. Determine whether C++ mode should accept this case or expose a targeted warning-control flag, and add a regression test showing the chosen behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.