about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2022-09-25 06:53:14 +0000
committerbors <bors@rust-lang.org>2022-09-25 06:53:14 +0000
commit8e9c93df464b7ada3fc7a1c8ccddd9dcb24ee0a0 (patch)
tree0ddc2ed198eb887005d4fab4a97c8c6aa5c3f508
parente20fabb0d04cb25fe3159d58920856533f1b5cf0 (diff)
parentbcf780e2ba00dbe5a81502ef541dbc0f4fb76ebb (diff)
downloadrust-8e9c93df464b7ada3fc7a1c8ccddd9dcb24ee0a0.tar.gz
rust-8e9c93df464b7ada3fc7a1c8ccddd9dcb24ee0a0.zip
Auto merge of #99609 - workingjubilee:lossy-unix-strerror, r=thomcc
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 rust-lang#99535.
-rw-r--r--library/std/src/sys/unix/os.rs4
1 files changed, 3 insertions, 1 deletions
diff --git a/library/std/src/sys/unix/os.rs b/library/std/src/sys/unix/os.rs
index 3c3770708b1..7c70ddbd9b9 100644
--- a/library/std/src/sys/unix/os.rs
+++ b/library/std/src/sys/unix/os.rs
@@ -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()
     }
 }