Skip to content

Commit

Permalink
Use C string literal
Browse files Browse the repository at this point in the history
  • Loading branch information
taiki-e authored and Amanieu committed Sep 18, 2024
1 parent 7e6562e commit 7e82ced
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 40 deletions.
9 changes: 5 additions & 4 deletions crates/std_detect/src/detect/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,26 +134,27 @@ cfg_if::cfg_if! {

#[inline]
fn initialize(mut value: Initializer) -> Initializer {
const RUST_STD_DETECT_UNSTABLE: &[u8] = b"RUST_STD_DETECT_UNSTABLE\0";
use core::ffi::CStr;
const RUST_STD_DETECT_UNSTABLE: &CStr = c"RUST_STD_DETECT_UNSTABLE";
cfg_if::cfg_if! {
if #[cfg(windows)] {
use alloc::vec;
#[link(name = "kernel32")]
extern "system" {
fn GetEnvironmentVariableA(name: *const u8, buffer: *mut u8, size: u32) -> u32;
}
let len = unsafe { GetEnvironmentVariableA(RUST_STD_DETECT_UNSTABLE.as_ptr(), core::ptr::null_mut(), 0) };
let len = unsafe { GetEnvironmentVariableA(RUST_STD_DETECT_UNSTABLE.as_ptr().cast::<u8>(), core::ptr::null_mut(), 0) };
if len > 0 {
// +1 to include the null terminator.
let mut env = vec![0; len as usize + 1];
let len = unsafe { GetEnvironmentVariableA(RUST_STD_DETECT_UNSTABLE.as_ptr(), env.as_mut_ptr(), len + 1) };
let len = unsafe { GetEnvironmentVariableA(RUST_STD_DETECT_UNSTABLE.as_ptr().cast::<u8>(), env.as_mut_ptr(), len + 1) };
if len > 0 {
disable_features(&env[..len as usize], &mut value);
}
}
} else {
let env = unsafe {
libc::getenv(RUST_STD_DETECT_UNSTABLE.as_ptr() as *const libc::c_char)
libc::getenv(RUST_STD_DETECT_UNSTABLE.as_ptr())
};
if !env.is_null() {
let len = unsafe { libc::strlen(env) };
Expand Down
63 changes: 32 additions & 31 deletions crates/std_detect/src/detect/os/darwin/aarch64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
//! <https://developer.apple.com/documentation/kernel/1387446-sysctlbyname/determining_instruction_set_characteristics>

use crate::detect::{cache, Feature};
use core::ffi::CStr;

#[inline]
fn _sysctlbyname(name: &str) -> bool {
fn _sysctlbyname(name: &CStr) -> bool {
use libc;

let mut enabled: i32 = 0;
Expand All @@ -14,7 +15,7 @@ fn _sysctlbyname(name: &str) -> bool {

let ret = unsafe {
libc::sysctlbyname(
name.as_ptr() as *const i8,
name.as_ptr(),
enabled_ptr,
&mut enabled_len,
core::ptr::null_mut(),
Expand All @@ -38,35 +39,35 @@ pub(crate) fn detect_features() -> cache::Initializer {
}
};

let asimd = _sysctlbyname("hw.optional.AdvSIMD\0");
let pmull = _sysctlbyname("hw.optional.arm.FEAT_PMULL\0");
let fp = _sysctlbyname("hw.optional.floatingpoint\0");
let fp16 = _sysctlbyname("hw.optional.arm.FEAT_FP16\0");
let crc = _sysctlbyname("hw.optional.armv8_crc32\0");
let lse = _sysctlbyname("hw.optional.arm.FEAT_LSE\0");
let lse2 = _sysctlbyname("hw.optional.arm.FEAT_LSE2\0");
let rdm = _sysctlbyname("hw.optional.arm.FEAT_RDM\0");
let rcpc = _sysctlbyname("hw.optional.arm.FEAT_LRCPC\0");
let rcpc2 = _sysctlbyname("hw.optional.arm.FEAT_LRCPC2\0");
let dotprod = _sysctlbyname("hw.optional.arm.FEAT_DotProd\0");
let fhm = _sysctlbyname("hw.optional.arm.FEAT_FHM\0");
let flagm = _sysctlbyname("hw.optional.arm.FEAT_FlagM\0");
let ssbs = _sysctlbyname("hw.optional.arm.FEAT_SSBS\0");
let sb = _sysctlbyname("hw.optional.arm.FEAT_SB\0");
let paca = _sysctlbyname("hw.optional.arm.FEAT_PAuth\0");
let dpb = _sysctlbyname("hw.optional.arm.FEAT_DPB\0");
let dpb2 = _sysctlbyname("hw.optional.arm.FEAT_DPB2\0");
let frintts = _sysctlbyname("hw.optional.arm.FEAT_FRINTTS\0");
let i8mm = _sysctlbyname("hw.optional.arm.FEAT_I8MM\0");
let bf16 = _sysctlbyname("hw.optional.arm.FEAT_BF16\0");
let bti = _sysctlbyname("hw.optional.arm.FEAT_BTI\0");
let fcma = _sysctlbyname("hw.optional.arm.FEAT_FCMA\0");
let aes = _sysctlbyname("hw.optional.arm.FEAT_AES\0");
let sha1 = _sysctlbyname("hw.optional.arm.FEAT_SHA1\0");
let sha2 = _sysctlbyname("hw.optional.arm.FEAT_SHA256\0");
let sha3 = _sysctlbyname("hw.optional.arm.FEAT_SHA3\0");
let sha512 = _sysctlbyname("hw.optional.arm.FEAT_SHA512\0");
let jsconv = _sysctlbyname("hw.optional.arm.FEAT_JSCVT\0");
let asimd = _sysctlbyname(c"hw.optional.AdvSIMD");
let pmull = _sysctlbyname(c"hw.optional.arm.FEAT_PMULL");
let fp = _sysctlbyname(c"hw.optional.floatingpoint");
let fp16 = _sysctlbyname(c"hw.optional.arm.FEAT_FP16");
let crc = _sysctlbyname(c"hw.optional.armv8_crc32");
let lse = _sysctlbyname(c"hw.optional.arm.FEAT_LSE");
let lse2 = _sysctlbyname(c"hw.optional.arm.FEAT_LSE2");
let rdm = _sysctlbyname(c"hw.optional.arm.FEAT_RDM");
let rcpc = _sysctlbyname(c"hw.optional.arm.FEAT_LRCPC");
let rcpc2 = _sysctlbyname(c"hw.optional.arm.FEAT_LRCPC2");
let dotprod = _sysctlbyname(c"hw.optional.arm.FEAT_DotProd");
let fhm = _sysctlbyname(c"hw.optional.arm.FEAT_FHM");
let flagm = _sysctlbyname(c"hw.optional.arm.FEAT_FlagM");
let ssbs = _sysctlbyname(c"hw.optional.arm.FEAT_SSBS");
let sb = _sysctlbyname(c"hw.optional.arm.FEAT_SB");
let paca = _sysctlbyname(c"hw.optional.arm.FEAT_PAuth");
let dpb = _sysctlbyname(c"hw.optional.arm.FEAT_DPB");
let dpb2 = _sysctlbyname(c"hw.optional.arm.FEAT_DPB2");
let frintts = _sysctlbyname(c"hw.optional.arm.FEAT_FRINTTS");
let i8mm = _sysctlbyname(c"hw.optional.arm.FEAT_I8MM");
let bf16 = _sysctlbyname(c"hw.optional.arm.FEAT_BF16");
let bti = _sysctlbyname(c"hw.optional.arm.FEAT_BTI");
let fcma = _sysctlbyname(c"hw.optional.arm.FEAT_FCMA");
let aes = _sysctlbyname(c"hw.optional.arm.FEAT_AES");
let sha1 = _sysctlbyname(c"hw.optional.arm.FEAT_SHA1");
let sha2 = _sysctlbyname(c"hw.optional.arm.FEAT_SHA256");
let sha3 = _sysctlbyname(c"hw.optional.arm.FEAT_SHA3");
let sha512 = _sysctlbyname(c"hw.optional.arm.FEAT_SHA512");
let jsconv = _sysctlbyname(c"hw.optional.arm.FEAT_JSCVT");

enable_feature(Feature::asimd, asimd);
enable_feature(Feature::pmull, pmull);
Expand Down
5 changes: 1 addition & 4 deletions crates/std_detect/src/detect/os/linux/aarch64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,7 @@ pub(crate) fn detect_features() -> cache::Initializer {
// https://reviews.llvm.org/D114523
let mut arch = [0_u8; libc::PROP_VALUE_MAX as usize];
let len = unsafe {
libc::__system_property_get(
b"ro.arch\0".as_ptr() as *const libc::c_char,
arch.as_mut_ptr() as *mut libc::c_char,
)
libc::__system_property_get(c"ro.arch".as_ptr(), arch.as_mut_ptr() as *mut libc::c_char)
};
// On Exynos, ro.arch is not available on Android 12+, but it is fine
// because Android 9+ includes the fix.
Expand Down
2 changes: 1 addition & 1 deletion crates/std_detect/src/detect/os/linux/auxvec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ fn getauxval(key: usize) -> Result<usize, ()> {
use libc;
pub type F = unsafe extern "C" fn(usize) -> usize;
unsafe {
let ptr = libc::dlsym(libc::RTLD_DEFAULT, "getauxval\0".as_ptr() as *const _);
let ptr = libc::dlsym(libc::RTLD_DEFAULT, c"getauxval".as_ptr());
if ptr.is_null() {
return Err(());
}
Expand Down

0 comments on commit 7e82ced

Please sign in to comment.