diff options
| author | Felix Morgner <felix.morgner@ost.ch> | 2026-07-15 12:19:17 +0200 |
|---|---|---|
| committer | Felix Morgner <felix.morgner@ost.ch> | 2026-07-15 12:19:17 +0200 |
| commit | 8a647acda62a1a86386d6ce0d8ccfa5902899634 (patch) | |
| tree | 54c3c73b676b7aec753da8fdd278bf05e79340c6 /libs | |
| parent | ba0cf20847d9aa8eb6c540ef7be20972010d6d64 (diff) | |
| download | kernel-8a647acda62a1a86386d6ce0d8ccfa5902899634.tar.xz kernel-8a647acda62a1a86386d6ce0d8ccfa5902899634.zip | |
kstd: fix shared_ptr constructor race
Diffstat (limited to 'libs')
| -rw-r--r-- | libs/kstd/kstd/bits/shared_ptr.hpp | 15 |
1 files changed, 11 insertions, 4 deletions
diff --git a/libs/kstd/kstd/bits/shared_ptr.hpp b/libs/kstd/kstd/bits/shared_ptr.hpp index ce4bfb7f..f4aed34e 100644 --- a/libs/kstd/kstd/bits/shared_ptr.hpp +++ b/libs/kstd/kstd/bits/shared_ptr.hpp @@ -266,11 +266,18 @@ namespace kstd : pointer(nullptr) , control(nullptr) { - if (other.control != nullptr && other.control->shared_count != 0) + if (other.control) { - pointer = other.pointer; - control = other.control; - ++(control->shared_count); + auto count = other.control->shared_count.load(std::memory_order::relaxed); + while (count != 0 && !other.control->shared_count.compare_exchange_weak( + count, count + 1, std::memory_order::acquire, std::memory_order::relaxed)) + ; + + if (count != 0) + { + pointer = other.pointer; + control = other.control; + } } } |
