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

This commit is contained in:
Hanif Ariffin
2022-03-30 09:53:09 +02:00
committed by GitHub
parent 1e6b248a77
commit 5c85f5a9d4
+13 -11
View File
@@ -70,7 +70,15 @@ use libc::{
};
use std::borrow::Cow;
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::io::Error as IOError;
#[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(
target_os = "freebsd",
target_vendor = "apple",
@@ -704,9 +705,10 @@ where
0 => Ok(buffer),
_ => {
let errno = IOError::last_os_error().raw_os_error().unwrap_or(0);
Err(CString::from_raw(strerror(errno))
.into_string()
.unwrap_or_else(|_| "Unknown Error".to_owned()))
Err(CStr::from_ptr(strerror(errno))
.to_str()
.map_err(|_| "Error message contains invalid UTF-8".to_owned())?
.to_owned())
}
}
}