Skip to content

Commit ba43261

Browse files
Rollup merge of rust-lang#127583 - Nilstrieb:invalid-utf8, r=joboet
Deal with invalid UTF-8 from `gai_strerror` When the system is using a non-UTF-8 locale, the value will indeed not be UTF-8. That sucks for everyone involved, but is no reason for panic. We can "handle" this gracefully by just using from lossy, replacing the invalid UTF-8 with � and keeping the accidentally valid UTF-8. Good luck when debugging, but at least it's not a crash. We already do this for `strerror_r`. fixes rust-lang#127563
2 parents 2fff48d + 321dbf8 commit ba43261

File tree

1 file changed

+3
-2
lines changed

1 file changed

+3
-2
lines changed

std/src/sys/pal/unix/net.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ use crate::io::{self, BorrowedBuf, BorrowedCursor, IoSlice, IoSliceMut};
44
use crate::mem;
55
use crate::net::{Shutdown, SocketAddr};
66
use crate::os::unix::io::{AsFd, AsRawFd, BorrowedFd, FromRawFd, IntoRawFd, RawFd};
7-
use crate::str;
87
use crate::sys::fd::FileDesc;
98
use crate::sys::pal::unix::IsMinusOne;
109
use crate::sys_common::net::{getsockopt, setsockopt, sockaddr_to_addr};
@@ -47,7 +46,9 @@ pub fn cvt_gai(err: c_int) -> io::Result<()> {
4746

4847
#[cfg(not(target_os = "espidf"))]
4948
let detail = unsafe {
50-
str::from_utf8(CStr::from_ptr(libc::gai_strerror(err)).to_bytes()).unwrap().to_owned()
49+
// We can't always expect a UTF-8 environment. When we don't get that luxury,
50+
// it's better to give a low-quality error message than none at all.
51+
CStr::from_ptr(libc::gai_strerror(err)).to_string_lossy()
5152
};
5253

5354
#[cfg(target_os = "espidf")]

0 commit comments

Comments
 (0)