diff options
| author | Jakub Beránek <berykubik@gmail.com> | 2025-07-18 08:49:28 +0200 |
|---|---|---|
| committer | Jakub Beránek <berykubik@gmail.com> | 2025-07-22 20:22:16 +0200 |
| commit | 81d90d8257e59bc27acd6ec98f3cce3d27de6755 (patch) | |
| tree | 0847d69c61f6f9e90373cc8fae9dcb7f5913581a /library/std_detect | |
| parent | 5e52677a82c6c146469561c97ca4c63a75242102 (diff) | |
| download | rust-81d90d8257e59bc27acd6ec98f3cce3d27de6755.tar.gz rust-81d90d8257e59bc27acd6ec98f3cce3d27de6755.zip | |
Improve error messages of auxv loading
Diffstat (limited to 'library/std_detect')
| -rw-r--r-- | library/std_detect/src/detect/os/linux/auxvec.rs | 17 | ||||
| -rw-r--r-- | library/std_detect/src/detect/os/linux/mod.rs | 10 |
2 files changed, 17 insertions, 10 deletions
diff --git a/library/std_detect/src/detect/os/linux/auxvec.rs b/library/std_detect/src/detect/os/linux/auxvec.rs index 1ed395f45b9..443caaaa186 100644 --- a/library/std_detect/src/detect/os/linux/auxvec.rs +++ b/library/std_detect/src/detect/os/linux/auxvec.rs @@ -119,7 +119,7 @@ pub(crate) fn auxv() -> Result<AuxVec, ()> { { // If calling getauxval fails, try to read the auxiliary vector from // its file: - auxv_from_file("/proc/self/auxv") + auxv_from_file("/proc/self/auxv").map_err(|_| ()) } #[cfg(not(feature = "std_detect_file_io"))] { @@ -157,17 +157,22 @@ fn getauxval(key: usize) -> Result<usize, ()> { /// Tries to read the auxiliary vector from the `file`. If this fails, this /// function returns `Err`. #[cfg(feature = "std_detect_file_io")] -pub(super) fn auxv_from_file(file: &str) -> Result<AuxVec, ()> { +pub(super) fn auxv_from_file(file: &str) -> Result<AuxVec, alloc::string::String> { let file = super::read_file(file)?; + auxv_from_file_bytes(&file) +} +/// Read auxiliary vector from a slice of bytes. +#[cfg(feature = "std_detect_file_io")] +pub(super) fn auxv_from_file_bytes(bytes: &[u8]) -> Result<AuxVec, alloc::string::String> { // See <https://github.com/torvalds/linux/blob/v5.15/include/uapi/linux/auxvec.h>. // // The auxiliary vector contains at most 34 (key,value) fields: from // `AT_MINSIGSTKSZ` to `AT_NULL`, but its number may increase. - let len = file.len(); + let len = bytes.len(); let mut buf = alloc::vec![0_usize; 1 + len / core::mem::size_of::<usize>()]; unsafe { - core::ptr::copy_nonoverlapping(file.as_ptr(), buf.as_mut_ptr() as *mut u8, len); + core::ptr::copy_nonoverlapping(bytes.as_ptr(), buf.as_mut_ptr() as *mut u8, len); } auxv_from_buf(&buf) @@ -176,7 +181,7 @@ pub(super) fn auxv_from_file(file: &str) -> Result<AuxVec, ()> { /// Tries to interpret the `buffer` as an auxiliary vector. If that fails, this /// function returns `Err`. #[cfg(feature = "std_detect_file_io")] -fn auxv_from_buf(buf: &[usize]) -> Result<AuxVec, ()> { +fn auxv_from_buf(buf: &[usize]) -> Result<AuxVec, alloc::string::String> { // Targets with only AT_HWCAP: #[cfg(any( target_arch = "riscv32", @@ -222,7 +227,7 @@ fn auxv_from_buf(buf: &[usize]) -> Result<AuxVec, ()> { } // Suppress unused variable let _ = buf; - Err(()) + Err(alloc::string::String::from("hwcap not found")) } #[cfg(test)] diff --git a/library/std_detect/src/detect/os/linux/mod.rs b/library/std_detect/src/detect/os/linux/mod.rs index 9accd41717b..5ae2aaeab5b 100644 --- a/library/std_detect/src/detect/os/linux/mod.rs +++ b/library/std_detect/src/detect/os/linux/mod.rs @@ -6,14 +6,16 @@ use alloc::vec::Vec; mod auxvec; #[cfg(feature = "std_detect_file_io")] -fn read_file(path: &str) -> Result<Vec<u8>, ()> { - let mut path = Vec::from(path.as_bytes()); +fn read_file(orig_path: &str) -> Result<Vec<u8>, alloc::string::String> { + use alloc::format; + + let mut path = Vec::from(orig_path.as_bytes()); path.push(0); unsafe { let file = libc::open(path.as_ptr() as *const libc::c_char, libc::O_RDONLY); if file == -1 { - return Err(()); + return Err(format!("Cannot open file at {orig_path}")); } let mut data = Vec::new(); @@ -23,7 +25,7 @@ fn read_file(path: &str) -> Result<Vec<u8>, ()> { match libc::read(file, spare.as_mut_ptr() as *mut _, spare.len()) { -1 => { libc::close(file); - return Err(()); + return Err(format!("Error while reading from file at {orig_path}")); } 0 => break, n => data.set_len(data.len() + n as usize), |
