@@ -536,20 +536,28 @@ pub mod os {
536
536
}
537
537
538
538
pub unsafe fn get ( & ' static self , init : fn ( ) -> T ) -> Option < & ' static T > {
539
- let ptr = self . os . get ( ) as * mut Value < T > ;
539
+ // SAFETY: No mutable references are ever handed out meaning getting
540
+ // the value is ok.
541
+ let ptr = unsafe { self . os . get ( ) as * mut Value < T > } ;
540
542
if ptr as usize > 1 {
541
- if let Some ( ref value) = ( * ptr) . inner . get ( ) {
543
+ // SAFETY: the check ensured the pointer is safe (its destructor
544
+ // is not running) + it is coming from a trusted source (self).
545
+ if let Some ( ref value) = unsafe { ( * ptr) . inner . get ( ) } {
542
546
return Some ( value) ;
543
547
}
544
548
}
545
- self . try_initialize ( init)
549
+ // SAFETY: At this point we are sure we have no value and so
550
+ // initializing (or trying to) is safe.
551
+ unsafe { self . try_initialize ( init) }
546
552
}
547
553
548
554
// `try_initialize` is only called once per os thread local variable,
549
555
// except in corner cases where thread_local dtors reference other
550
556
// thread_local's, or it is being recursively initialized.
551
557
unsafe fn try_initialize ( & ' static self , init : fn ( ) -> T ) -> Option < & ' static T > {
552
- let ptr = self . os . get ( ) as * mut Value < T > ;
558
+ // SAFETY: No mutable references are ever handed out meaning getting
559
+ // the value is ok.
560
+ let ptr = unsafe { self . os . get ( ) as * mut Value < T > } ;
553
561
if ptr as usize == 1 {
554
562
// destructor is running
555
563
return None ;
@@ -560,7 +568,11 @@ pub mod os {
560
568
// local copy, so do that now.
561
569
let ptr: Box < Value < T > > = box Value { inner : LazyKeyInner :: new ( ) , key : self } ;
562
570
let ptr = Box :: into_raw ( ptr) ;
563
- self . os . set ( ptr as * mut u8 ) ;
571
+ // SAFETY: At this point we are sure there is no value inside
572
+ // ptr so setting it will not affect anyone else.
573
+ unsafe {
574
+ self . os . set ( ptr as * mut u8 ) ;
575
+ }
564
576
ptr
565
577
} else {
566
578
// recursive initialization
0 commit comments