https://gcc.gnu.org/bugzilla/show_bug.cgi?id=110536
Bug ID: 110536 Summary: Bogus -Wstringop-overflow warning in std::transform Product: gcc Version: 14.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: eric.niebler at gmail dot com Target Milestone: --- Compile the following with -O3 -std=c++17 -Wall <<<<<<BEGIN #include <algorithm> #include <cstdint> #include <vector> template <typename TypeParam, typename T> std::vector<TypeParam> make_type_param_vector(std::initializer_list<T> const& init_list) { // std::vector<T> input{init_list}; //uncomment to remove warning std::vector<TypeParam> vec(init_list.size()); std::transform(std::cbegin(init_list), std::cend(init_list), std::begin(vec), [](auto const& e) { if constexpr (std::is_unsigned_v<TypeParam>) { return static_cast<TypeParam>(std::abs(e)); } return static_cast<TypeParam>(e); }); return vec; } template <typename T> void validate_A() { auto const input_column_valid_a = make_type_param_vector<uint8_t>({1, 0}); auto const input_column_valid_b = make_type_param_vector<uint8_t>({0, 0}); auto const input_column_valid_c = make_type_param_vector<T>({15, 16}); } int main() { validate_A<float>(); validate_A<double>(); validate_A<int8_t>(); validate_A<int16_t>(); validate_A<int32_t>(); validate_A<int64_t>(); validate_A<uint8_t>(); validate_A<uint16_t>(); validate_A<uint32_t>(); } <<<<<<END Result: In file included from /opt/compiler-explorer/gcc-trunk-20230703/include/c++/14.0.0/algorithm:61, from <source>:1: In function '_OIter std::transform(_IIter, _IIter, _OIter, _UnaryOperation) [with _IIter = const int*; _OIter = __gnu_cxx::__normal_iterator<unsigned char*, vector<unsigned char, allocator<unsigned char> > >; _UnaryOperation = make_type_param_vector<unsigned char, int>(const std::initializer_list<int>&)::<lambda(const auto:1&)>]', inlined from 'std::vector<TypeParam> make_type_param_vector(const std::initializer_list<T>&) [with TypeParam = unsigned char; T = int]' at <source>:10:17: /opt/compiler-explorer/gcc-trunk-20230703/include/c++/14.0.0/bits/stl_algo.h:4216:19: warning: writing 1 byte into a region of size 0 [-Wstringop-overflow=] 4216 | *__result = __unary_op(*__first); | ~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~ Demo: https://godbolt.org/z/PKqfjr9cb