Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix UB in transmute #34181

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/libstd/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@
#![feature(core_float)]
#![feature(core_intrinsics)]
#![feature(dropck_parametricity)]
#![feature(drop_types_in_const)]
#![feature(float_extras)]
#![feature(float_from_str_radix)]
#![feature(fnbox)]
Expand Down
18 changes: 5 additions & 13 deletions src/libstd/sys/common/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,11 @@ mod imp {
use prelude::v1::*;

use libc::c_char;
use mem;
use ffi::CStr;

use sys_common::mutex::Mutex;

static mut GLOBAL_ARGS_PTR: usize = 0;
static mut GLOBAL_ARGS: Option<Vec<Vec<u8>>> = None;
static LOCK: Mutex = Mutex::new();

pub unsafe fn init(argc: isize, argv: *const *const u8) {
Expand All @@ -59,32 +58,25 @@ mod imp {
}).collect();

LOCK.lock();
let ptr = get_global_ptr();
assert!((*ptr).is_none());
(*ptr) = Some(box args);
assert!(GLOBAL_ARGS.is_none());
GLOBAL_ARGS = Some(args);
LOCK.unlock();
}

pub unsafe fn cleanup() {
LOCK.lock();
*get_global_ptr() = None;
GLOBAL_ARGS = None;
LOCK.unlock();
}

pub fn clone() -> Option<Vec<Vec<u8>>> {
unsafe {
LOCK.lock();
let ptr = get_global_ptr();
let ret = (*ptr).as_ref().map(|s| (**s).clone());
let ret = GLOBAL_ARGS.clone();
LOCK.unlock();
return ret
}
}

fn get_global_ptr() -> *mut Option<Box<Vec<Vec<u8>>>> {
unsafe { mem::transmute(&GLOBAL_ARGS_PTR) }
}

}

#[cfg(any(target_os = "macos",
Expand Down