Skip to content

Commit 324418f

Browse files
committed
Don't die in try_unsafe_borrow if tls isn't ready
If there's no TLS key just yet, then there's nothing to unsafely borrow, so continue returning None. This prevents causing the runtime to abort itself when logging before the runtime is fully initialized. Closes #9487
1 parent 7535479 commit 324418f

File tree

2 files changed

+30
-6
lines changed

2 files changed

+30
-6
lines changed

src/libstd/rt/local_ptr.rs

+10-6
Original file line numberDiff line numberDiff line change
@@ -129,12 +129,16 @@ pub unsafe fn unsafe_borrow<T>() -> *mut T {
129129
}
130130

131131
pub unsafe fn try_unsafe_borrow<T>() -> Option<*mut T> {
132-
let key = tls_key();
133-
let void_ptr = tls::get(key);
134-
if void_ptr.is_null() {
135-
None
136-
} else {
137-
Some(void_ptr as *mut T)
132+
match maybe_tls_key() {
133+
Some(key) => {
134+
let void_ptr = tls::get(key);
135+
if void_ptr.is_null() {
136+
None
137+
} else {
138+
Some(void_ptr as *mut T)
139+
}
140+
}
141+
None => None
138142
}
139143
}
140144

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// exec-env:RUST_LOG=std::ptr
12+
// xfail-fast this would cause everything to print forever on windows...
13+
14+
// In issue #9487, it was realized that std::ptr was invoking the logging
15+
// infrastructure, and when std::ptr was used during runtime initialization,
16+
// this caused some serious problems. The problems have since been fixed, but
17+
// this test will trigger "output during runtime initialization" to make sure
18+
// that the bug isn't re-introduced.
19+
20+
fn main() {}

0 commit comments

Comments
 (0)