[Bug c++/105918] New: Spurious Warray-bounds in std::to_chars

ed at catmur dot uk gcc-bugzilla@gcc.gnu.org
Fri Jun 10 14:01:13 GMT 2022


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

            Bug ID: 105918
           Summary: Spurious Warray-bounds in std::to_chars
           Product: gcc
           Version: 12.1.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: ed at catmur dot uk
  Target Milestone: ---

#include <charconv>
template<int N>
std::to_chars_result toChars(char (& buf)[N], int number) {
    char temp[N];
    std::to_chars_result result = std::to_chars(temp, temp + N, number);
    if (result.ec != std::errc())
        return result;
    char* it = temp;
    char* end = result.ptr;
    std::size_t i = 0u;
    while (i != N && it != end)
        buf[i++] = *it++;
    if (i != N)
        buf[i] = '\0';
    return std::to_chars_result{.ptr = buf + (result.ptr - temp), .ec =
std::errc()};
}
#include <memory>
struct ar {
    struct V { bool value; } value;
    std::unique_ptr<int> s;
};
void f(...);
ar g();
ar evaluate(bool value) {
    ar res{value};
    if (res.value.value)
        return res;
    return g();
}
#define CHECK(P) f(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, evaluate(P), 0, 0, 0, 0, 0, 0,
0, 0, 0 );
int main() {
    char s3[3];
    auto res3 = toChars(s3, 12);
    CHECK((res3.ec == std::errc()));
    CHECK((s3[0] == '1'));
    CHECK((s3[1] == '2'));
    CHECK((s3[2] == '\0'));
    char s1[1];
    auto res1 = toChars(s1, 12);
    CHECK((res1.ec != std::errc()));
}

Compiled at -O3 -Warray-bounds:

In file included from include/c++/12.1.0/charconv:42,
                 from <source>:1:
In function 'void std::__detail::__to_chars_10_impl(char*, unsigned int, _Tp)
[with _Tp = unsigned int]',
    inlined from 'std::__detail::__integer_to_chars_result_type<_Tp>
std::__detail::__to_chars_10(char*, char*, _Tp) [with _Tp = unsigned int]' at
include/c++/12.1.0/charconv:225:35,
    inlined from 'std::__detail::__integer_to_chars_result_type<_Tp>
std::__to_chars_i(char*, char*, _Tp, int) [with _Tp = int]' at
include/c++/12.1.0/charconv:351:32,
    inlined from 'std::to_chars_result std::to_chars(char*, char*, int, int)'
at include/c++/12.1.0/charconv:370:1,
    inlined from 'std::to_chars_result toChars(char (&)[N], int) [with int N =
1]' at <source>:7:48:
include/c++/12.1.0/bits/charconv.h:95:22: warning: array subscript 1 is outside
array bounds of 'char [1]' [-Warray-bounds]
   95 |           __first[1] = __digits[__num + 1];
      |           ~~~~~~~~~~~^~~~~~~~~~~
<source>: In function 'std::to_chars_result toChars(char (&)[N], int) [with int
N = 1]':
<source>:6:10: note: at offset 1 into object 'temp' of size 1
    6 |     char temp[N];
      |          ^~~~
<source>:6:10: note: at offset 2 into object 'temp' of size 1
Compiler returned: 0


More information about the Gcc-bugs mailing list