Skip to content

std::process::Debug's non-ascii escaping behavior doesn't match its documentation #165315

Closed

Description

@qwaz

The doc says:

impl fmt::Debug for Command {
/// Format the program and arguments of a Command for display. Any
/// non-utf8 data is lossily converted using the utf8 replacement
/// character.

Despite its claim that non-utf8 characters are converted to the utf8 replacement character, the Debug implementation prints many characters as \xNN.

use std::ffi::OsStr;
use std::os::unix::ffi::OsStrExt;
use std::os::unix::process::CommandExt;
use std::process::Command;

fn main() {
    let s = |b: &'static [u8]| OsStr::from_bytes(b);
    let mut cmd = Command::new(s(b"prog\xFF"));
    cmd.arg0(s(b"arg0\xFF"))
        .arg(s(b"arg1\xFF"))
        .env(s(b"KEY\xFF"), s(b"val\xFF"))
        .current_dir(s(b"cwd\xFF"));
    println!("{cmd:?}");
}

Output:

cd "cwd\xff" && KEY�="val\xFF" ["prog\xff"] "arg0\xff" "arg1\xFF"
  • Program, argv[0], and cwd uses CStr's Debug and print lowercase \xff.
  • The remaining arguments and env values go through OsStr's Debug and print uppercase \xFF.
  • The env key goes through to_string_lossy and converted to UTF-8 replacement character as documented.

Tested Environment (rustc --version --verbose)

rustc 1.99.0 (b940084d7 2026-09-28)
binary: rustc
commit-hash: b940084d7eb6a299eb4bfeb8e34901bc051e7ac4
commit-date: 2026-09-28
host: x86_64-unknown-linux-gnu
release: 1.99.0
LLVM version: 23.1.1

Reporting on behalf of Microsoft FORGE Lab.

Activity

  1. added
    C-bug
    on Nov 2, 2026
  2. added
    needs-triage
    on Nov 2, 2026
  3. added
    A-docs
    T-libs
    A-process
    and removed
    needs-triage
    on Nov 2, 2026
  4. maxdexh commented on Nov 2, 2026

    @maxdexh
    Member

    Since this is a debug implementation, I'd say let's just update the docs.

  5. added a commit that references this issue on Nov 8, 2026
    89b80c7
  6. added a commit that references this issue on Nov 8, 2026
    259da33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-docsA-processC-bugT-libs

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions