From 6eff41e7af9a10bdfda539097c0f411122ee81bb Mon Sep 17 00:00:00 2001 From: apio Date: Tue, 14 Feb 2023 20:03:29 +0100 Subject: [PATCH] Option: Use __builtin_launder to ensure that the compiler doesn't label this as UB --- luna/include/luna/Option.h | 16 +++------------- 1 file changed, 3 insertions(+), 13 deletions(-) diff --git a/luna/include/luna/Option.h b/luna/include/luna/Option.h index bbe09698..ce5a57dd 100644 --- a/luna/include/luna/Option.h +++ b/luna/include/luna/Option.h @@ -138,26 +138,16 @@ template class Option private: struct Storage { - u8 buffer[sizeof(T)]; - - T* fetch_ptr() - { - return (T*)buffer; - } + alignas(T) u8 buffer[sizeof(T)]; T& fetch_reference() { - return *fetch_ptr(); - } - - const T* fetch_ptr() const - { - return (const T*)buffer; + return *__builtin_launder(reinterpret_cast(&buffer)); } const T& fetch_reference() const { - return *fetch_ptr(); + return *__builtin_launder(reinterpret_cast(&buffer)); } void store_reference(const T& ref)