Skip to content

Printing io::Error panics if current UNIX locale doesn't use UTF-8 encoding #99535

Closed
@complexspaces

Description

@complexspaces

I tried this code:

fn main() {
    let result = std::fs::create_dir("/tmp/does_not_exist/new_dir_name");
    println!("{:?}", result);
}

With the test binary built:

export LANG=zh_CN; ./target/debug/binary_name

This crash was encountered on a "stock" Fedora 36 installation. The issue is highly dependent on how your Linux system's locale is configured. It seems that it is up to the distro to configure which locale encoding set is used, in this specific case for Chinese. Given that GB-2312 used to be a mandated standard, it is unsurprising to see it in the wild.

I expected to see this happen: The error message would be printed in any form.

Instead, this happened: A panic kills the app and no error is printed.

AFAICT, this error comes from an assumption that the standard library made to assume all characters returned by strerror_r will use UTF-8 encoding: https://github.com/rust-lang/rust/blob/master/library/std/src/sys/unix/os.rs#L113-L130

If you look at other larger software projects and how they use strerror_r, in this case Chromium and Firefox:

I am not sure what the "most correct" solution is for the standard library given that all Rust strings must be UTF-8, but I think that it would be ideal to not crash a thread (or app, depending on the unwind mode) if an end-user has their locale set to something that uses a non-unicode representation.

Meta

rustc --version --verbose:

rustc 1.61.0 (fe5b13d68 2022-05-18)
binary: rustc
commit-hash: fe5b13d681f25ee6474be29d748c65adcd91f69e
commit-date: 2022-05-18
host: x86_64-unknown-linux-gnu
release: 1.61.0
LLVM version: 14.0.0
Backtrace

thread '<unnamed>' panicked at 'called `Result::unwrap()` on an `Err` value: Utf8Error { valid_up_to: 2, error_len: Some(1) }', library/std/src/sys/unix/os.rs:128:54
stack backtrace:
   0:     0x7f3a4d4c6ac2 - std::backtrace_rs::backtrace::libunwind::trace::h22893a5306c091b4
                               at /rustc/fe5b13d681f25ee6474be29d748c65adcd91f69e/library/std/src/../../backtrace/src/backtrace/libunwind.rs:93:5
   1:     0x7f3a4d4c6ac2 - std::backtrace_rs::backtrace::trace_unsynchronized::h29c3bc6f9e91819d
                               at /rustc/fe5b13d681f25ee6474be29d748c65adcd91f69e/library/std/src/../../backtrace/src/backtrace/mod.rs:66:5
   2:     0x7f3a4d4c6ac2 - std::sys_common::backtrace::_print_fmt::he497d8a0ec903793
                               at /rustc/fe5b13d681f25ee6474be29d748c65adcd91f69e/library/std/src/sys_common/backtrace.rs:66:5
   3:     0x7f3a4d4c6ac2 - <std::sys_common::backtrace::_print::DisplayBacktrace as core::fmt::Display>::fmt::h9c2a9d2774d81873
                               at /rustc/fe5b13d681f25ee6474be29d748c65adcd91f69e/library/std/src/sys_common/backtrace.rs:45:22
   4:     0x7f3a4bc712cc - core::fmt::write::hba4337c43d992f49
                               at /rustc/fe5b13d681f25ee6474be29d748c65adcd91f69e/library/core/src/fmt/mod.rs:1194:17
   5:     0x7f3a4d4c0251 - std::io::Write::write_fmt::heb73de6e02cfabed
                               at /rustc/fe5b13d681f25ee6474be29d748c65adcd91f69e/library/std/src/io/mod.rs:1655:15
   6:     0x7f3a4d4c8755 - std::sys_common::backtrace::_print::h63c8b24acdd8e8ce
                               at /rustc/fe5b13d681f25ee6474be29d748c65adcd91f69e/library/std/src/sys_common/backtrace.rs:48:5
   7:     0x7f3a4d4c8755 - std::sys_common::backtrace::print::h426700d6240cdcc2
                               at /rustc/fe5b13d681f25ee6474be29d748c65adcd91f69e/library/std/src/sys_common/backtrace.rs:35:9
   8:     0x7f3a4d4c8755 - std::panicking::default_hook::{{closure}}::hc9a76eed0b18f82b
                               at /rustc/fe5b13d681f25ee6474be29d748c65adcd91f69e/library/std/src/panicking.rs:295:22
   9:     0x7f3a4d4c84c4 - std::panicking::default_hook::h2e88d02087fae196
                               at /rustc/fe5b13d681f25ee6474be29d748c65adcd91f69e/library/std/src/panicking.rs:314:9
  10:     0x7f3a4c745788 - op_crash_reporting::enable_panic_hook::{{closure}}::{{closure}}::h724bf2747ea56a1c
  11:     0x7f3a4b74882a - op_executor::SenderBuilder<Request>::build::{{closure}}::{{closure}}::hf89d1cadb6419e73
  12:     0x7f3a4d4c8cfb - std::panicking::rust_panic_with_hook::habfdcc2e90f9fd4c
                               at /rustc/fe5b13d681f25ee6474be29d748c65adcd91f69e/library/std/src/panicking.rs:702:17
  13:     0x7f3a4d4c8b34 - std::panicking::begin_panic_handler::{{closure}}::he054b2a83a51d2cd
                               at /rustc/fe5b13d681f25ee6474be29d748c65adcd91f69e/library/std/src/panicking.rs:588:13
  14:     0x7f3a4d4c6fd4 - std::sys_common::backtrace::__rust_end_short_backtrace::ha48b94ab49b30915
                               at /rustc/fe5b13d681f25ee6474be29d748c65adcd91f69e/library/std/src/sys_common/backtrace.rs:138:18
  15:     0x7f3a4d4c889d - rust_begin_unwind
                               at /rustc/fe5b13d681f25ee6474be29d748c65adcd91f69e/library/std/src/panicking.rs:584:5
  16:     0x7f3a4b62b213 - core::panicking::panic_fmt::h366d3a309ae17c94
                               at /rustc/fe5b13d681f25ee6474be29d748c65adcd91f69e/library/core/src/panicking.rs:143:14
  17:     0x7f3a4b62b303 - core::result::unwrap_failed::hddd78f4658ac7d0f
                               at /rustc/fe5b13d681f25ee6474be29d748c65adcd91f69e/library/core/src/result.rs:1785:5
  18:     0x7f3a4d4cbc5e - core::result::Result<T,E>::unwrap::h2ba9400889a980d6
                               at /rustc/fe5b13d681f25ee6474be29d748c65adcd91f69e/library/core/src/result.rs:1078:23
  19:     0x7f3a4d4cbc5e - std::sys::unix::os::error_string::h64fe777d5d9922ad
                               at /rustc/fe5b13d681f25ee6474be29d748c65adcd91f69e/library/std/src/sys/unix/os.rs:128:9
  20:     0x7f3a4d4bcfc2 - std::io::error::<impl core::fmt::Debug for std::io::error::repr_bitpacked::Repr>::fmt::h38748b43fb2fb69c
                               at /rustc/fe5b13d681f25ee6474be29d748c65adcd91f69e/library/std/src/io/error.rs:836:36
  21:     0x7f3a4bc6f28f - core::fmt::builders::DebugTuple::field::{{closure}}::h28f8fba9ff4c5ec8
                               at /rustc/fe5b13d681f25ee6474be29d748c65adcd91f69e/library/core/src/fmt/builders.rs:332:17
  22:     0x7f3a4bc6f28f - core::result::Result<T,E>::and_then::h9a138925c1fad505
                               at /rustc/fe5b13d681f25ee6474be29d748c65adcd91f69e/library/core/src/result.rs:1332:22
  23:     0x7f3a4bc6f28f - core::fmt::builders::DebugTuple::field::h82683a4707af3361
                               at /rustc/fe5b13d681f25ee6474be29d748c65adcd91f69e/library/core/src/fmt/builders.rs:319:23
  24:     0x7f3a4bf53a36 - <notify::error::ErrorKind as core::fmt::Debug>::fmt::hc30b2bf96845208f
  25:     0x7f3a4bc712cc - core::fmt::write::hba4337c43d992f49
                               at /rustc/fe5b13d681f25ee6474be29d748c65adcd91f69e/library/core/src/fmt/mod.rs:1194:17
  26:     0x7f3a4b7420e5 - <op_log::loggable::LogDisplay<T> as core::fmt::Display>::fmt::h8b68bd9486878f91
  27:     0x7f3a4bc712cc - core::fmt::write::hba4337c43d992f49
                               at /rustc/fe5b13d681f25ee6474be29d748c65adcd91f69e/library/core/src/fmt/mod.rs:1194:17
  28:     0x7f3a4bc014c4 - core::fmt::Write::write_fmt::hf11e68ede6b8308a
                               at /rustc/fe5b13d681f25ee6474be29d748c65adcd91f69e/library/core/src/fmt/mod.rs:186:9
  29:     0x7f3a4bc014c4 - alloc::fmt::format::h4114a1f369d70d00
                               at /rustc/fe5b13d681f25ee6474be29d748c65adcd91f69e/library/alloc/src/fmt.rs:597:5
  30:     0x7f3a4b8b3ef1 - op_log::loggable::result::ResultExt::log_err_msg::h0a38de7e80b960e8
  31:     0x7f3a4ba07dd5 - op_ssh_config::SshConfig::new::h7079eec1183c1f85
  32:     0x7f3a4b7baf5a - op_app::app::App::new::h98a497264aec65bc
  33:     0x7f3a4b9c779a - op_executor::SenderBuilder<Request>::build::he5c713296a344328
  34:     0x7f3a4b846b8b - core_node::init_core::hde82e7bc30331296
  35:     0x7f3a4ba35e1d - neon::types::error::convert_panics::hd03f753ff0e515e2
  36:     0x7f3a4bb8f632 - neon::sys::fun::call_boxed::h21d0cfe35e5836b5
  37:     0x558fa4ad82cb - <unknown>

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: This is a bug.O-unixOperating system: Unix-like

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions