https://gcc.gnu.org/bugzilla/show_bug.cgi?id=129144
Bug ID: 129144
Summary: Bogus warning about missing terminating nul for puts
Product: gcc
Version: 16.2.1
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: knapek.mar at gmail dot com
Target Milestone: ---
Hi, consider this code [1], it generates an array of 4 bytes, first 3 are
ASCII, the last one is NUL. The program then sends this array to the puts
function. But the compiler emits the [2] warning about missing NUL. This is
bogus, as the NUL is clearly there. Copy & Paste the [1] code to Compiler
Explorer (Godbolt), set the compiler to GCC 16.2 for x64, set the compiler
options to -std=c++26 -O2 -freflection, the assembly will be [3]. The NUL is
clearly visible at assembly line 12. Sincerely, Marek.
[1]
#include <algorithm>
#include <array>
#include <meta>
template<typename t>
[[nodiscard]] constexpr static inline char const* get_type_name(void)
noexcept(true)
{
auto impl = []<typename tt>()
{
constexpr auto const refl = ^^tt;
constexpr auto const dso = display_string_of(refl);
std::array<char, dso.size() + 1> arr;
std::copy(dso.cbegin(), dso.cend(), arr.begin());
arr[arr.size() - 1] = '\0';
return arr;
};
static constexpr auto const arr = impl.template operator()<t>();
static constexpr auto const cstr = &arr[0];
return cstr;
}
#include <cstdio>
int main()
{
printf("%s\n", get_type_name<int>());
}
[2]
<source>: In function 'int main()':
<source>:23:11: warning: 'int __builtin_puts(const char*)' argument missing
terminating nul [-Wstringop-overread]
23 | printf("%s\n", get_type_name<int>());
| ~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<source>:16:33: note: referenced argument declared here
16 | static constexpr auto const arr = impl.template operator()<t>();
| ^~~
Compiler returned: 0
[3]
"main":
sub rsp, 8
mov edi, OFFSET FLAT:"char const* get_type_name<int>()::arr"
call "puts"
xor eax, eax
add rsp, 8
ret
"char const* get_type_name<int>()::arr":
.byte 105
.byte 110
.byte 116
.byte 0 <--- But the NUL is here!