| Issue |
208646
|
| Summary |
[compiler-rt][tysan][libc++] False positives with std::optional storage
|
| Labels |
libc++
|
| Assignees |
|
| Reporter |
onurcan-isler-oss
|
# `std::optional` + `std::string`
#### Flags: `-stdlib=libc++ -fsanitize=type -O1`
#### Compiler: x86-64 clang 22.1.0
#### Link: https://godbolt.org/z/8dvaY7sKW
#### Code:
```.cpp
#include <string>
#include <optional>
static std::optional<std::string> optional_var = std::nullopt;
int main() {
optional_var = "this is a random long string (short one does not reproduce)";
return 0;
}
```
#### Logs:
```
==1==ERROR: TypeSanitizer: type-aliasing-violation on address 0x57a032406ef0 (pc 0x57a031aac059 bp 0x7ffc71704bf0 sp 0x7ffc71704b80 tid 1)
WRITE of size 8 at 0x57a032406ef0 with type long accesses part of an existing object of type std::__1::optional<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>>> that starts at offset -8
#0 0x57a031aac058 (/app/output.s+0x36058)
==1==ERROR: TypeSanitizer: type-aliasing-violation on address 0x57a032406ef8 (pc 0x57a031aac087 bp 0x7ffc71704bf0 sp 0x7ffc71704b80 tid 1)
WRITE of size 8 at 0x57a032406ef8 with type p1 omnipotent char accesses part of an existing object of type std::__1::optional<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>>> that starts at offset -16
#0 0x57a031aac086 (/app/output.s+0x36086)
```
#### Explanation:
`std::optional<...>`'s value lives in a `union`. This reproduces only with a long string because it bypasses `std::string`’s small-string optimization.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs