This might not be the correct forum to post this, as it looks like an error that I should report to the llvm team perhaps. Please let me know if you agree and I'll do just that.
Here's the code that produces the ubsan error, just in case I'm wrong:
OS = MacOS Ventura
Compiler = Clang 16.0.0 (Manually built LLVM with default options)
Steps to reproduce:
clang++ -fsanitize=undefined main.cpp
./a.out
main.cpp
#include <sstream>
struct MyStruct
{
static MyStruct create() noexcept
{
return MyStruct{};
}
private:
std::ostringstream oss_;
};
int main()
{
MyStruct::create();
}
Output:
~/usr/bin/../include/c++/v1/sstream:730:43: runtime error: member access within address 0x7ff7b265a298 which does not point to an object of type 'std::ostringstream'
0x7ff7b265a298: note: object is of type 'std::__1::basic_ostringstream<char, std::__1::char_traits<char>, std::__1::allocator<char>>'
f7 7f 00 00 40 11 8b 0d 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
^~~~~~~~~~~~~~~~~~~~~~~
vptr for 'std::__1::basic_ostringstream<char, std::__1::char_traits<char>, std::__1::allocator<char>>'
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior ~/usr/bin/../include/c++/v1/sstream:730:43 in
~/usr/bin/../include/c++/v1/ostream:198:7: runtime error: cast to virtual base of address 0x7ff7b265a298 which does not point to an object of type 'std::ostream'
0x7ff7b265a298: note: object is of type 'std::__1::basic_ostream<char, std::__1::char_traits<char>>'
f7 7f 00 00 c8 11 8b 0d 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
^~~~~~~~~~~~~~~~~~~~~~~
vptr for 'std::__1::basic_ostream<char, std::__1::char_traits<char>>'
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior ~/usr/bin/../include/c++/v1/ostream:198:7 in
~/usr/bin/../include/c++/v1/ostream:198:13: runtime error: member call on address 0x7ff7b265a308 which does not point to an object of type 'std::ios'
0x7ff7b265a298: note: object is base class subobject at offset 112 within object of type 'std::__1::basic_ostream<char, std::__1::char_traits<char>>'
f7 7f 00 00 c8 11 8b 0d 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
^
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior ~/usr/bin/../include/c++/v1/ostream:198:13 in
~/usr/bin/../include/c++/v1/ios:711:15: runtime error: member call on address 0x7ff7b265a308 which does not point to an object of type 'std::ios_base'
0x7ff7b265a298: note: object is base class subobject at offset 112 within object of type 'std::__1::basic_ostream<char, std::__1::char_traits<char>>'
f7 7f 00 00 c8 11 8b 0d 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
^
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior ~/usr/bin/../include/c++/v1/ios:711:15 in
~/usr/bin/../include/c++/v1/ios:712:5: runtime error: member access within address 0x7ff7b265a308 which does not point to an object of type 'std::ios'
0x7ff7b265a298: note: object is base class subobject at offset 112 within object of type 'std::__1::basic_ostream<char, std::__1::char_traits<char>>'
f7 7f 00 00 c8 11 8b 0d 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
^
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior ~/usr/bin/../include/c++/v1/ios:712:5 in
~/usr/bin/../include/c++/v1/ios:713:5: runtime error: member access within address 0x7ff7b265a308 which does not point to an object of type 'std::ios'
0x7ff7b265a298: note: object is base class subobject at offset 112 within object of type 'std::__1::basic_ostream<char, std::__1::char_traits<char>>'
f7 7f 00 00 c8 11 8b 0d 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
^
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior ~/usr/bin/../include/c++/v1/ios:713:5 in