openai/codex · error · anyhow::Error

invalid encoding for command line argument

Error message

invalid encoding for command line argument {arg:?}

What it means

A validation guard in build_cmdline: it fires when a command-line argument (program or argument after the first) cannot be encoded to UTF-16 via OsStr::encode_wide, i.e. the OsString contains invalid data for wide conversion on Windows.

Solutions

  1. Ensure command arguments come from valid UTF-8/UTF-16 sources rather than raw bytes that cannot convert
  2. Sanitize or reject arguments with invalid encoding before passing them to the CommandBuilder
  3. On Unix-originated input, validate UTF-8 validity before forwarding to Windows
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at codex-rs/utils/pty/src/win/psuedocon.rs:273 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of openai/codex@339751715c (2026-08-25). Data as JSON: /api/errors/39b140a54e654f26. Report an issue: GitHub.

Appendix: source

Thrown at codex-rs/utils/pty/src/win/psuedocon.rs:273

fn build_cmdline(cmd: &CommandBuilder) -> anyhow::Result<(Vec<u16>, Vec<u16>)> {
    let exe_os: OsString = if cmd.is_default_prog() {
        cmd.get_env("ComSpec")
            .unwrap_or(OsStr::new("cmd.exe"))
            .to_os_string()
    } else {
        let argv = cmd.get_argv();
        let Some(first) = argv.first() else {
            anyhow::bail!("missing program name");
        };
        search_path(cmd, first)
    };

    let mut cmdline = Vec::new();
    append_quoted(&exe_os, &mut cmdline);
    for arg in cmd.get_argv().iter().skip(1) {
        cmdline.push(' ' as u16);
        ensure!(
            !arg.encode_wide().any(|c| c == 0),
            "invalid encoding for command line argument {arg:?}"
        );
        append_quoted(arg, &mut cmdline);
    }
    cmdline.push(0);

    let mut exe: Vec<u16> = exe_os.encode_wide().collect();
    exe.push(0);

    Ok((exe, cmdline))
}

fn search_path(cmd: &CommandBuilder, exe: &OsStr) -> OsString {
    if let Some(path) = cmd.get_env("PATH") {
        let extensions = cmd.get_env("PATHEXT").unwrap_or(OsStr::new(".EXE"));
        for path in env::split_paths(path) {
            let candidate = path.join(exe);

View on GitHub (pinned to 339751715c)