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