https://gcc.gnu.org/bugzilla/show_bug.cgi?id=116572
Bug ID: 116572
Summary: Using a std::string passed to a virtual member
function of a side-casted pointer spuriously triggers
UBSan on 14.2.0, and ICEs on trunk
Product: gcc
Version: 14.2.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: eddiejnolan at gmail dot com
Target Milestone: ---
Given the following code, built with -std=c++20 -O3 -fsanitize=undefined:
#include <string>
int main()
{
struct VirtualBase {
virtual ~VirtualBase() {}
};
struct VirtualBase2 {
virtual void foo(std::string s) const = 0;
};
struct MultiBase : VirtualBase, VirtualBase2 {
void foo(std::string s) const override {
(void)s.find("X");
}
} multibase{};
VirtualBase* p{&multibase};
dynamic_cast<VirtualBase2*>(p)->foo("Y");
}
GCC 14.2.0 compiles the program but produces the following UBSan report:
p\x13@:4203136: runtime error: execution reached an unreachable program point
(I'm using \x13 to substitute for an unprintable ASCII character in the
original output).
GCC trunk fails to compile entirely with the following internal compiler error:
<source>:18:1: internal compiler error: in release_body, at cgraph.cc:1865
18 | }
| ^
0x27ccba5 diagnostic_context::diagnostic_impl(rich_location*,
diagnostic_metadata const*, int, char const*, __va_list_tag (*) [1],
diagnostic_t)
???:0
0x27e0145 internal_error(char const*, ...)
???:0
0xa7fe58 fancy_abort(char const*, int, char const*)
???:0
0xeb4b4b symbol_table::finalize_compilation_unit()
???:0
Please submit a full bug report, with preprocessed source (by using
-freport-bug).
Please include the complete backtrace with any bug report.
See <https://gcc.gnu.org/bugs/> for instructions.
Compiler returned: 1
Compiler explorer link: https://godbolt.org/z/Gcc48qEMn