llvm / llvm/llvm-project

[compiler-rt][tysan][libc++] False positives with std::optional storage

Open
#208,646 0 comments 0 reactions 0 assignees View on GitHub
compiler-rt:tysan false-positive libc++ needs-reduction
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

# `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
#include

static std::optional 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::allocator>> 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::allocator>> 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.

Contributor guide

Open the contributing guide

Research direction

Start with the Godbolt reproducer using libc++, -fsanitize=type, and -O1, then inspect the compiler-rt/tysan handling of writes into std::optional storage. Compare the long-string case with the short-string case and confirm the reported union accesses. Done means the reproducer no longer emits false-positive type-aliasing violations.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Needs clarification
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.