1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-29 03:57:44 +00:00

Fix incorrect usage of CString because it didn't come from us (#3279)

This commit is contained in:
Hanif Ariffin 2022-03-30 00:53:09 -07:00 committed by GitHub
parent 1e6b248a77
commit 5c85f5a9d4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -70,7 +70,15 @@ use libc::{
}; };
use std::borrow::Cow; use std::borrow::Cow;
use std::convert::{AsRef, From}; use std::convert::{AsRef, From};
#[cfg(unix)] #[cfg(any(
target_vendor = "apple",
target_os = "freebsd",
target_os = "netbsd",
target_os = "openbsd",
target_os = "linux"
))]
use std::ffi::CStr;
#[cfg(not(windows))]
use std::ffi::CString; use std::ffi::CString;
use std::io::Error as IOError; use std::io::Error as IOError;
#[cfg(unix)] #[cfg(unix)]
@ -308,13 +316,6 @@ impl MountInfo {
} }
} }
#[cfg(any(
target_vendor = "apple",
target_os = "freebsd",
target_os = "netbsd",
target_os = "openbsd"
))]
use std::ffi::CStr;
#[cfg(any( #[cfg(any(
target_os = "freebsd", target_os = "freebsd",
target_vendor = "apple", target_vendor = "apple",
@ -704,9 +705,10 @@ where
0 => Ok(buffer), 0 => Ok(buffer),
_ => { _ => {
let errno = IOError::last_os_error().raw_os_error().unwrap_or(0); let errno = IOError::last_os_error().raw_os_error().unwrap_or(0);
Err(CString::from_raw(strerror(errno)) Err(CStr::from_ptr(strerror(errno))
.into_string() .to_str()
.unwrap_or_else(|_| "Unknown Error".to_owned())) .map_err(|_| "Error message contains invalid UTF-8".to_owned())?
.to_owned())
} }
} }
} }