From 1b5e7cdafea5d2d10bbf8eb798a1711fd3af21ab Mon Sep 17 00:00:00 2001 From: Dominik Maier Date: Tue, 8 Aug 2023 00:33:22 +0200 Subject: [PATCH] Move static ctor out of unsafe context (#294) --- ctor/src/lib.rs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/ctor/src/lib.rs b/ctor/src/lib.rs index b91b0b3..7101e0c 100644 --- a/ctor/src/lib.rs +++ b/ctor/src/lib.rs @@ -262,8 +262,12 @@ pub fn ctor(_attribute: TokenStream, function: TokenStream) -> TokenStream { : unsafe extern "C" fn() = { #[cfg_attr(any(target_os = "linux", target_os = "android"), link_section = ".text.startup")] - unsafe extern "C" fn initer() { - #storage_ident = Some(#expr); + extern "C" fn initer() { + let val = Some(#expr); + // Only write the value to `storage_ident` on startup + unsafe { + #storage_ident = val; + } }; initer } ; );