https://gcc.gnu.org/bugzilla/show_bug.cgi?id=120825
Bug ID: 120825
Summary: Constexpr evaluation fails with the ub sanitizer
Product: gcc
Version: 13.3.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: addmlbx at gmail dot com
Target Milestone: ---
Usage of the undefined behaviour sanitizer causes failure of the evaluation of
the constexpr function.
Code below, if put in the file `file.cpp`, compiles without errors when command
`g++ -Wall -Wextra -Wpedantic -std=c++20 file.cpp` is used, but compilation
fails if command `g++ -Wall -Wextra -Wpedantic -std=c++20 -fsanitize=undefined
file.cpp` is used (fails at std::string_view::find at bits/string_view.tcc:66
when pointer __first is compared to null)
```c++
#include <array>
#include <cstddef>
#include <string_view>
struct S {
static constexpr std::array kNullTerminatedCharsArray =
std::to_array("abc");
static constexpr std::string_view kStr{kNullTerminatedCharsArray.data()};
};
int main() {
[[maybe_unused]] constexpr size_t pos = S::kStr.find("ab");
}
```
Possible error:
In file included from /usr/include/c++/13/string_view:905,
from file.cpp:3:
/usr/include/c++/13/bits/string_view.tcc: In function ‘int main()’:
file.cpp:11:57: in ‘constexpr’ expansion of
‘S::kStr.std::basic_string_view<char>::find(((const char*)"ab"), 0)’
/usr/include/c++/13/string_view:462:26: in ‘constexpr’ expansion of ‘((const
std::basic_string_view<char>*)this)->std::basic_string_view<char>::find(__str,
__pos, std::char_traits<char>::length(__str))’
/usr/include/c++/13/bits/string_view.tcc:66:16: error: ‘(((const char*)(&
S::kNullTerminatedCharsArray.std::array<char, 4>::_M_elems)) == 0)’ is not a
constant expression
66 | if (!__first)
| ^~~~~~~
Used gcc is g++ (Ubuntu 13.3.0-6ubuntu2~24.04) 13.3.0