llvm / llvm-project

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
http://llvm.org
Other
29.15k stars 12.03k forks source link

[libc++] `std::format` reads past the 0 terminator in `char[N]` arrays #115935

Open Fulgen301 opened 5 days ago

Fulgen301 commented 5 days ago

Reproducer

If one passes a fixed size char - array containing a 0-terminated string to std::format, the entire array minus one character is added to the result, instead of just the part until the null terminator. This not only seemingly violates [format.formatter.spec]/2.2, but also doesn't match the behavior of MS-STL or libstdc++. (And only formatting 49 characters out of a 50 character array seems buggy anyway.)

The issue is reproducible with all versions of libc++ that shipped with clang since <format> was added, including current trunk on godbolt.

frederick-vs-ja commented 5 days ago

See here: https://github.com/llvm/llvm-project/blob/37143fe27e082b478d333ca28f6f1af5210b7c6b/libcxx/include/__format/format_arg_store.h#L104-L116

Per [format.arg]/6, char arrays should be converted to char pointers first.

CC @mordante