Skip to content

Commit

Permalink
Auto merge of #99609 - workingjubilee:lossy-unix-strerror, r=thomcc
Browse files Browse the repository at this point in the history
Recover error strings on Unix from_lossy_utf8

Some language settings can result in unreliable UTF-8 being produced.
This can result in failing to emit the error string, panicking instead.
from_lossy_utf8 allows us to assume these strings usually will be fine.

This fixes #99535.
  • Loading branch information
bors committed Sep 25, 2022
2 parents e20fabb + bcf780e commit 8e9c93d
Showing 1 changed file with 3 additions and 1 deletion.
4 changes: 3 additions & 1 deletion library/std/src/sys/unix/os.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,9 @@ pub fn error_string(errno: i32) -> String {
}

let p = p as *const _;
str::from_utf8(CStr::from_ptr(p).to_bytes()).unwrap().to_owned()
// We can't always expect a UTF-8 environment. When we don't get that luxury,
// it's better to give a low-quality error message than none at all.
String::from_utf8_lossy(CStr::from_ptr(p).to_bytes()).into()
}
}

Expand Down

0 comments on commit 8e9c93d

Please sign in to comment.