From f506656876d445eb64c00721ca39007d08480e65 Mon Sep 17 00:00:00 2001 From: Thom Chiovoloni Date: Fri, 19 Aug 2022 16:02:48 -0700 Subject: [PATCH 1/2] Align android `sigaddset` impl with the reference impl from Bionic --- .../src/sys/unix/process/process_common.rs | 26 ++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/library/std/src/sys/unix/process/process_common.rs b/library/std/src/sys/unix/process/process_common.rs index bca1b65a7fc05..6ef02e2df775b 100644 --- a/library/std/src/sys/unix/process/process_common.rs +++ b/library/std/src/sys/unix/process/process_common.rs @@ -45,11 +45,31 @@ cfg_if::cfg_if! { } #[allow(dead_code)] pub unsafe fn sigaddset(set: *mut libc::sigset_t, signum: libc::c_int) -> libc::c_int { - use crate::{slice, mem}; + use crate::{ + mem::{align_of, size_of}, + slice, + }; + use libc::{c_ulong, sigset_t}; + + // The implementations from bionic (android libc) type pun `sigset_t` as an + // array of `c_ulong`. This works, but lets add a smoke check to make sure + // that doesn't change. + const _: () = assert!( + align_of::() == align_of::() + && (size_of::() % size_of::()) == 0 + ); - let raw = slice::from_raw_parts_mut(set as *mut u8, mem::size_of::()); let bit = (signum - 1) as usize; - raw[bit / 8] |= 1 << (bit % 8); + if set.is_null() || bit < 0 || bit >= (8 * size_of::()) { + crate::sys::unix::os::set_errno(libc::EINVAL); + return -1; + } + let raw = slice::from_raw_parts_mut( + set as *mut c_ulong, + size_of::() / size_of::(), + ); + const LONG_BIT: usize = size_of::() * 8; + raw[bit / LONG_BIT] |= 1 << (bit % LONG_BIT); return 0; } } else { From 4ecf87619c8e2f6212a46501ea2aff18f5beb8c5 Mon Sep 17 00:00:00 2001 From: Thom Chiovoloni Date: Sun, 21 Aug 2022 01:08:33 -0700 Subject: [PATCH 2/2] Fix redundant comparison --- library/std/src/sys/unix/process/process_common.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/std/src/sys/unix/process/process_common.rs b/library/std/src/sys/unix/process/process_common.rs index 6ef02e2df775b..6985f1d783087 100644 --- a/library/std/src/sys/unix/process/process_common.rs +++ b/library/std/src/sys/unix/process/process_common.rs @@ -60,7 +60,7 @@ cfg_if::cfg_if! { ); let bit = (signum - 1) as usize; - if set.is_null() || bit < 0 || bit >= (8 * size_of::()) { + if set.is_null() || bit >= (8 * size_of::()) { crate::sys::unix::os::set_errno(libc::EINVAL); return -1; }