Skip to content

Commit

Permalink
Rollup merge of rust-lang#67666 - lzutao:ptr-null-cmp, r=dtolnay
Browse files Browse the repository at this point in the history
make use of pointer::is_null

r? @Mark-Simulacrum
  • Loading branch information
Dylan-DPC authored Jan 10, 2020
2 parents 2d8d559 + ef74bc2 commit a3bf5cc
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 18 deletions.
2 changes: 1 addition & 1 deletion src/libpanic_unwind/emcc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ pub unsafe fn cleanup(ptr: *mut u8) -> Box<dyn Any + Send> {
pub unsafe fn panic(data: Box<dyn Any + Send>) -> u32 {
let sz = mem::size_of_val(&data);
let exception = __cxa_allocate_exception(sz);
if exception == ptr::null_mut() {
if exception.is_null() {
return uw::_URC_FATAL_PHASE1_ERROR as u32;
}
ptr::write(exception as *mut _, data);
Expand Down
8 changes: 6 additions & 2 deletions src/libstd/sys/hermit/os.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,17 @@ pub fn init_environment(env: *const *const i8) {
unsafe {
ENV = Some(Mutex::new(HashMap::new()));

if env.is_null() {
return;
}

let mut guard = ENV.as_ref().unwrap().lock().unwrap();
let mut environ = env;
while environ != ptr::null() && *environ != ptr::null() {
while !(*environ).is_null() {
if let Some((key, value)) = parse(CStr::from_ptr(*environ).to_bytes()) {
guard.insert(key, value);
}
environ = environ.offset(1);
environ = environ.add(1);
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/libstd/sys/hermit/thread_local.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ static KEYS_LOCK: Mutex = Mutex::new();
static mut LOCALS: *mut BTreeMap<Key, *mut u8> = ptr::null_mut();

unsafe fn keys() -> &'static mut BTreeMap<Key, Option<Dtor>> {
if KEYS == ptr::null_mut() {
if KEYS.is_null() {
KEYS = Box::into_raw(Box::new(BTreeMap::new()));
}
&mut *KEYS
}

unsafe fn locals() -> &'static mut BTreeMap<Key, *mut u8> {
if LOCALS == ptr::null_mut() {
if LOCALS.is_null() {
LOCALS = Box::into_raw(Box::new(BTreeMap::new()));
}
&mut *LOCALS
Expand Down
2 changes: 1 addition & 1 deletion src/libstd/sys/sgx/abi/tls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ impl<'a> Drop for ActiveTls<'a> {
any_non_null_dtor = false;
for (value, dtor) in TLS_KEY_IN_USE.iter().filter_map(&value_with_destructor) {
let value = value.replace(ptr::null_mut());
if value != ptr::null_mut() {
if !value.is_null() {
any_non_null_dtor = true;
unsafe { dtor(value) }
}
Expand Down
10 changes: 6 additions & 4 deletions src/libstd/sys/unix/os.rs
Original file line number Diff line number Diff line change
Expand Up @@ -480,11 +480,13 @@ pub fn env() -> Env {
let _guard = env_lock();
let mut environ = *environ();
let mut result = Vec::new();
while environ != ptr::null() && *environ != ptr::null() {
if let Some(key_value) = parse(CStr::from_ptr(*environ).to_bytes()) {
result.push(key_value);
if !environ.is_null() {
while !(*environ).is_null() {
if let Some(key_value) = parse(CStr::from_ptr(*environ).to_bytes()) {
result.push(key_value);
}
environ = environ.add(1);
}
environ = environ.offset(1);
}
return Env { iter: result.into_iter(), _dont_send_or_sync_me: PhantomData };
}
Expand Down
6 changes: 3 additions & 3 deletions src/libstd/sys/vxworks/os.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,15 +226,15 @@ pub fn env() -> Env {
unsafe {
let _guard = env_lock();
let mut environ = *environ();
if environ == ptr::null() {
if environ.is_null() {
panic!("os::env() failure getting env string from OS: {}", io::Error::last_os_error());
}
let mut result = Vec::new();
while *environ != ptr::null() {
while !(*environ).is_null() {
if let Some(key_value) = parse(CStr::from_ptr(*environ).to_bytes()) {
result.push(key_value);
}
environ = environ.offset(1);
environ = environ.add(1);
}
return Env { iter: result.into_iter(), _dont_send_or_sync_me: PhantomData };
}
Expand Down
10 changes: 6 additions & 4 deletions src/libstd/sys/wasi/os.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,13 @@ pub fn env() -> Env {
let _guard = env_lock();
let mut environ = libc::environ;
let mut result = Vec::new();
while environ != ptr::null_mut() && *environ != ptr::null_mut() {
if let Some(key_value) = parse(CStr::from_ptr(*environ).to_bytes()) {
result.push(key_value);
if !environ.is_null() {
while !(*environ).is_null() {
if let Some(key_value) = parse(CStr::from_ptr(*environ).to_bytes()) {
result.push(key_value);
}
environ = environ.add(1);
}
environ = environ.offset(1);
}
return Env { iter: result.into_iter(), _dont_send_or_sync_me: PhantomData };
}
Expand Down
2 changes: 1 addition & 1 deletion src/libstd/sys/windows/os.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ pub fn error_string(mut errnum: i32) -> String {
];
module = c::GetModuleHandleW(NTDLL_DLL.as_ptr());

if module != ptr::null_mut() {
if !module.is_null() {
errnum ^= c::FACILITY_NT_BIT as i32;
flags = c::FORMAT_MESSAGE_FROM_HMODULE;
}
Expand Down

0 comments on commit a3bf5cc

Please sign in to comment.