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

            Bug ID: 113826
           Summary: string_view::find sometimes not working in constexpr
                    contexts under -fsanitize=undefined
           Product: gcc
           Version: 14.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: ivan.lazaric.gcc at gmail dot com
  Target Milestone: ---

https://godbolt.org/z/qKvecx48W

```
#include <string_view>

template <unsigned N> struct fixed_string {
  char buf[N + 1]{};
  consteval fixed_string(char const *s) {
    for (unsigned i = 0; i != N; ++i)
      buf[i] = s[i];
    buf[N] = 0;
  }
  consteval operator std::string_view() const {return {buf, N};}
};
template <unsigned N> fixed_string(char const (&)[N]) -> fixed_string<N - 1>;

template <fixed_string Str> struct storage {
  inline static constexpr std::string_view sv{Str};
  inline static constexpr bool hasA = sv.find('A') != std::string_view::npos;
};

int main(){
    using NS = storage<"ABC">;
    return NS::hasA;
}
```

Locally I am using g++ version 14.0.1
When building with flags `-std=c++20` it builds normally
When building with flags `-std=c++20 -fsanitize=undefined` it explodes
When building with clang with flags `-std=c++20 -fsanitize=undefined` it works
normally

Error message:
```
In file included from
/home/ilazaric/installs/gcc-latest/include/c++/14.0.1/string_view:898,
                 from ex.cpp:1:
/home/ilazaric/installs/gcc-latest/include/c++/14.0.1/bits/string_view.tcc: In
instantiation of ‘constexpr const bool storage<fixed_string<3>{"ABC"}>::hasA’:
ex.cpp:21:16:   required from here
   21 |     return NS::hasA;
      |                ^~~~
ex.cpp:16:46:   in ‘constexpr’ expansion of
‘storage<fixed_string<3>{"ABC"}>::sv.std::basic_string_view<char>::find(65, 0)’
/home/ilazaric/installs/gcc-latest/include/c++/14.0.1/bits/string_view.tcc:88:15:
error: ‘(((const char*)(& fixed_string<3>{"ABC"}.fixed_string<3>::buf)) != 0)’
is not a constant expression
   88 |           if (__p)
      |               ^~~
```

Might be related to https://gcc.gnu.org/bugzilla/show_bug.cgi?id=104426

Reply via email to