← Back to Pull requests

cmp, diffutils: handle stdout write errors without panicking (#165) #442

Open
yunusarda-sudo opened this pull request with 213 lines diff across 3 files
View on GitHub
yunusarda-sudo yunusarda-sudo commented on Nov 29, 2026 17:20 UTC
Author

When writing to a full or unbuffered stdout device (such as /dev/full), cmp and the multicall binary diffutils panicked inside Rust's println! macro with failed printing to stdout: No space left on device (os error 28) (exit status 134).

Changes


  • In src/cmp.rs: Write differences and verbose output directly to the locked stdout writer, reporting write errors as cmp: standard output: <error> with exit status 2 (matching GNU cmp and diff).

  • Safely handle stdout write errors in cmp --help without panicking.

  • In src/main.rs: Safely write usage text without panicking when stdout is full.

  • Add regression tests in tests/by-util/test_cmp.rs for default cmp, verbose cmp (-l), help output, and the multicall binary.
  • Fixes #165

    Diff: 213 lines (+173 / -40) across 3 files Reviewer: sylvestre Review duration: 4464m
    All Vouch review confidence checks passed
    Residual risk 0.03 = Change Risk (0.38) × (1 − Review Confidence (0.92)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.38 · 213 lines diff across 3 files
    0.38
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.93 · 5 substantive comment(s)
    0.93
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 50%
    92%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.321
    Real PR Conversation Timeline
    13 items
    sylvestre sylvestre commented on code on Nov 29, 2026 18:16 UTC
    Code Review
    src/main.rs:1
    @@ -43,10 +43,20 @@ fn name(binary_path: &Path) -> &OsStr {
     const VERSION: &str = env!("CARGO_PKG_VERSION");
     
     fn usage(name: &str) {
    -    println!("{name} {VERSION} (multi-call binary)\n");
    -    println!("Usage: {name} [function [arguments...]]\n");
    -    println!("Currently defined functions:\n");
    -    println!("    cmp, diff\n");
    +    let s = format!(
    +        "{name} {VERSION} (multi-call binary)\n\nUsage: {name} [function [arguments...]]\n\nCurrently defined functions:\n\n    cmp, diff\n"
    +    );
    +    let mut out = std::io::stdout().lock();
    +    let written = std::io::Write::write_all(&mut out, s.as_bytes())
    +        .and_then(|()| std::io::Write::flush(&mut out));
    +    if let Err(e) = written {
    +        if e.kind() != std::io::ErrorKind::BrokenPipe {

    the write error is printed but we still exit 0 via second_arg_error, no?
    should be 2 like cmp/diff

    sylvestre sylvestre commented on code on Nov 29, 2026 18:16 UTC
    Code Review
    src/main.rs:1
    @@ -43,10 +43,20 @@ fn name(binary_path: &Path) -> &OsStr {
     const VERSION: &str = env!("CARGO_PKG_VERSION");
     
     fn usage(name: &str) {
    -    println!("{name} {VERSION} (multi-call binary)\n");
    -    println!("Usage: {name} [function [arguments...]]\n");
    -    println!("Currently defined functions:\n");
    -    println!("    cmp, diff\n");
    +    let s = format!(
    +        "{name} {VERSION} (multi-call binary)\n\nUsage: {name} [function [arguments...]]\n\nCurrently defined functions:\n\n    cmp, diff\n"
    +    );
    +    let mut out = std::io::stdout().lock();
    +    let written = std::io::Write::write_all(&mut out, s.as_bytes())
    +        .and_then(|()| std::io::Write::flush(&mut out));
    +    if let Err(e) = written {
    +        if e.kind() != std::io::ErrorKind::BrokenPipe {
    +            let _ = std::io::Write::write_fmt(
    +                &mut std::io::stderr().lock(),
    +                format_args!("{name}: standard output: {e}\n"),

    this keeps the "(os error 28)" suffix, cmp and diff strip it
    could you please reuse format_failure_to_read_input_file?

    sylvestre sylvestre commented on code on Nov 29, 2026 18:16 UTC
    Code Review
    src/main.rs:1
    @@ -43,10 +43,20 @@ fn name(binary_path: &Path) -> &OsStr {
     const VERSION: &str = env!("CARGO_PKG_VERSION");
     
     fn usage(name: &str) {
    -    println!("{name} {VERSION} (multi-call binary)\n");
    -    println!("Usage: {name} [function [arguments...]]\n");
    -    println!("Currently defined functions:\n");
    -    println!("    cmp, diff\n");
    +    let s = format!(
    +        "{name} {VERSION} (multi-call binary)\n\nUsage: {name} [function [arguments...]]\n\nCurrently defined functions:\n\n    cmp, diff\n"
    +    );
    +    let mut out = std::io::stdout().lock();
    +    let written = std::io::Write::write_all(&mut out, s.as_bytes())
    +        .and_then(|()| std::io::Write::flush(&mut out));

    please use std::io::Write; instead of the fully qualified calls

    sylvestre sylvestre commented on code on Nov 29, 2026 18:16 UTC
    Code Review
    tests/by-util/test_cmp.rs:1
    @@ -421,3 +421,61 @@ fn cmp_difference_term_follows_messages_locale(
             .fails_with_code(1)
             .stdout_is(format!("a b differ: {term} 1, line 1\n"));
     }
    +
    +#[cfg(target_os = "linux")]
    +#[test]
    +fn write_error() {
    +    use std::fs::File;
    +    let (at, ts) = at_and_ts!();
    +    at.write("a", "left\n");
    +    at.write("b", "right\n");
    +
    +    ts.ucmd()
    +        .args(&["a", "b"])
    +        .set_stdout(File::create("/dev/full").unwrap())
    +        .fails_with_code(2)
    +        .stderr_is("cmp: standard output: No space left on device\n");
    +}
    +
    +#[cfg(target_os = "linux")]
    +#[test]
    +fn write_error_verbose() {
    +    use std::fs::File;
    +    let (at, ts) = at_and_ts!();
    +    at.write("a", "left\n");
    +    at.write("b", "right\n");
    +
    +    ts.ucmd()
    +        .args(&["-l", "a", "b"])
    +        .set_stdout(File::create("/dev/full").unwrap())
    +        .fails_with_code(2)
    +        .stderr_is("cmp: standard output: No space left on device\n");
    +}
    +
    +#[cfg(target_os = "linux")]
    +#[test]
    +fn write_error_help() {
    +    use std::fs::File;
    +    let (_at, ts) = at_and_ts!();
    +
    +    ts.ucmd()
    +        .arg("--help")
    +        .set_stdout(File::create("/dev/full").unwrap())
    +        .fails_with_code(2)
    +        .stderr_is("cmp: standard output: No space left on device\n");
    +}
    +
    +#[cfg(target_os = "linux")]
    +#[test]
    +fn multicall_write_error() {
    +    use std::fs::File;
    +    use std::process::Command;
    +
    +    let output = Command::new(crate::TESTS_BINARY)
    +        .stdout(File::create("/dev/full").unwrap())
    +        .output()
    +        .unwrap();
    +    assert_ne!(output.status.code(), Some(134));

    too weak, please check the exit code and the stderr message

    sylvestre sylvestre commented on code on Nov 29, 2026 18:16 UTC
    Code Review
    src/cmp.rs:1
    @@ -698,39 +717,54 @@ fn report_eof(
     }
     
     #[inline]
    -fn report_difference(
    +fn write_difference<W: Write>(
    +    out: &mut W,
         from_byte: u8,
         to_byte: u8,
         at_byte: BytesLimitU64,
         at_line: u64,
         params: &Params,
    -) {
    +) -> io::Result<()> {
         if params.quiet {
    -        return;
    +        return Ok(());
         }
     
         let term = if is_posix_locale("LC_MESSAGES") && !params.print_bytes {
             "char"
         } else {
             "byte"
         };
    -    print!(
    +    write!(
    +        out,
             "{} {} differ: {term} {}, line {}",
             params.from.to_string_lossy(),
             params.to.to_string_lossy(),
             at_byte,
             at_line
    -    );
    +    )?;
         if params.print_bytes {
    -        print!(
    +        write!(
    +            out,
                 " is {:>3o} {} {:>3o} {}",
                 from_byte,
                 format_visible_byte(from_byte),
                 to_byte,
                 format_visible_byte(to_byte)
    -        );
    +        )?;
    +    }
    +    writeln!(out)
    +}
    +
    +fn stdout_error(executable: &std::ffi::OsStr, err: &io::Error) -> String {

    returning an empty string for broken pipe and testing is_empty() in two places is fragile
    could be a dedicated error variant, no?

    sylvestre sylvestre commented on code on Nov 29, 2026 18:16 UTC
    Code Review
    src/cmp.rs:1
    @@ -698,39 +717,54 @@ fn report_eof(
     }
     
     #[inline]
    -fn report_difference(
    +fn write_difference<W: Write>(
    +    out: &mut W,
         from_byte: u8,
         to_byte: u8,
         at_byte: BytesLimitU64,
         at_line: u64,
         params: &Params,
    -) {
    +) -> io::Result<()> {
         if params.quiet {
    -        return;
    +        return Ok(());
         }
     
         let term = if is_posix_locale("LC_MESSAGES") && !params.print_bytes {
             "char"
         } else {
             "byte"
         };
    -    print!(
    +    write!(
    +        out,
             "{} {} differ: {term} {}, line {}",
             params.from.to_string_lossy(),
             params.to.to_string_lossy(),
             at_byte,
             at_line
    -    );
    +    )?;
         if params.print_bytes {
    -        print!(
    +        write!(
    +            out,
                 " is {:>3o} {} {:>3o} {}",
                 from_byte,
                 format_visible_byte(from_byte),
                 to_byte,
                 format_visible_byte(to_byte)
    -        );
    +        )?;
    +    }
    +    writeln!(out)
    +}
    +
    +fn stdout_error(executable: &std::ffi::OsStr, err: &io::Error) -> String {
    +    if err.kind() == io::ErrorKind::BrokenPipe {
    +        String::new()
    +    } else {
    +        format_failure_to_read_input_file(
    +            &OsString::from(executable),

    params.executable is already an OsString, why take &OsStr and convert back?

    yunusarda-sudo yunusarda-sudo commented on code on Nov 30, 2026 05:00 UTC
    Code Review
    src/main.rs:1
    @@ -43,10 +43,20 @@ fn name(binary_path: &Path) -> &OsStr {
     const VERSION: &str = env!("CARGO_PKG_VERSION");
     
     fn usage(name: &str) {
    -    println!("{name} {VERSION} (multi-call binary)\n");
    -    println!("Usage: {name} [function [arguments...]]\n");
    -    println!("Currently defined functions:\n");
    -    println!("    cmp, diff\n");
    +    let s = format!(
    +        "{name} {VERSION} (multi-call binary)\n\nUsage: {name} [function [arguments...]]\n\nCurrently defined functions:\n\n    cmp, diff\n"
    +    );
    +    let mut out = std::io::stdout().lock();
    +    let written = std::io::Write::write_all(&mut out, s.as_bytes())
    +        .and_then(|()| std::io::Write::flush(&mut out));
    +    if let Err(e) = written {
    +        if e.kind() != std::io::ErrorKind::BrokenPipe {

    Fixed, usage write errors now exit 2.

    yunusarda-sudo yunusarda-sudo commented on code on Nov 30, 2026 05:00 UTC
    Code Review
    src/main.rs:1
    @@ -43,10 +43,20 @@ fn name(binary_path: &Path) -> &OsStr {
     const VERSION: &str = env!("CARGO_PKG_VERSION");
     
     fn usage(name: &str) {
    -    println!("{name} {VERSION} (multi-call binary)\n");
    -    println!("Usage: {name} [function [arguments...]]\n");
    -    println!("Currently defined functions:\n");
    -    println!("    cmp, diff\n");
    +    let s = format!(
    +        "{name} {VERSION} (multi-call binary)\n\nUsage: {name} [function [arguments...]]\n\nCurrently defined functions:\n\n    cmp, diff\n"
    +    );
    +    let mut out = std::io::stdout().lock();
    +    let written = std::io::Write::write_all(&mut out, s.as_bytes())
    +        .and_then(|()| std::io::Write::flush(&mut out));
    +    if let Err(e) = written {
    +        if e.kind() != std::io::ErrorKind::BrokenPipe {
    +            let _ = std::io::Write::write_fmt(
    +                &mut std::io::stderr().lock(),
    +                format_args!("{name}: standard output: {e}\n"),

    Reused format_failure_to_read_input_file, the suffix is gone.

    yunusarda-sudo yunusarda-sudo commented on code on Nov 30, 2026 05:00 UTC
    Code Review
    src/main.rs:1
    @@ -43,10 +43,20 @@ fn name(binary_path: &Path) -> &OsStr {
     const VERSION: &str = env!("CARGO_PKG_VERSION");
     
     fn usage(name: &str) {
    -    println!("{name} {VERSION} (multi-call binary)\n");
    -    println!("Usage: {name} [function [arguments...]]\n");
    -    println!("Currently defined functions:\n");
    -    println!("    cmp, diff\n");
    +    let s = format!(
    +        "{name} {VERSION} (multi-call binary)\n\nUsage: {name} [function [arguments...]]\n\nCurrently defined functions:\n\n    cmp, diff\n"
    +    );
    +    let mut out = std::io::stdout().lock();
    +    let written = std::io::Write::write_all(&mut out, s.as_bytes())
    +        .and_then(|()| std::io::Write::flush(&mut out));

    Done, it now uses use std::io::Write.

    yunusarda-sudo yunusarda-sudo commented on code on Nov 30, 2026 05:00 UTC
    Code Review
    tests/by-util/test_cmp.rs:1
    @@ -421,3 +421,61 @@ fn cmp_difference_term_follows_messages_locale(
             .fails_with_code(1)
             .stdout_is(format!("a b differ: {term} 1, line 1\n"));
     }
    +
    +#[cfg(target_os = "linux")]
    +#[test]
    +fn write_error() {
    +    use std::fs::File;
    +    let (at, ts) = at_and_ts!();
    +    at.write("a", "left\n");
    +    at.write("b", "right\n");
    +
    +    ts.ucmd()
    +        .args(&["a", "b"])
    +        .set_stdout(File::create("/dev/full").unwrap())
    +        .fails_with_code(2)
    +        .stderr_is("cmp: standard output: No space left on device\n");
    +}
    +
    +#[cfg(target_os = "linux")]
    +#[test]
    +fn write_error_verbose() {
    +    use std::fs::File;
    +    let (at, ts) = at_and_ts!();
    +    at.write("a", "left\n");
    +    at.write("b", "right\n");
    +
    +    ts.ucmd()
    +        .args(&["-l", "a", "b"])
    +        .set_stdout(File::create("/dev/full").unwrap())
    +        .fails_with_code(2)
    +        .stderr_is("cmp: standard output: No space left on device\n");
    +}
    +
    +#[cfg(target_os = "linux")]
    +#[test]
    +fn write_error_help() {
    +    use std::fs::File;
    +    let (_at, ts) = at_and_ts!();
    +
    +    ts.ucmd()
    +        .arg("--help")
    +        .set_stdout(File::create("/dev/full").unwrap())
    +        .fails_with_code(2)
    +        .stderr_is("cmp: standard output: No space left on device\n");
    +}
    +
    +#[cfg(target_os = "linux")]
    +#[test]
    +fn multicall_write_error() {
    +    use std::fs::File;
    +    use std::process::Command;
    +
    +    let output = Command::new(crate::TESTS_BINARY)
    +        .stdout(File::create("/dev/full").unwrap())
    +        .output()
    +        .unwrap();
    +    assert_ne!(output.status.code(), Some(134));

    The test now checks for exit 2 and the stderr message.

    yunusarda-sudo yunusarda-sudo commented on code on Nov 30, 2026 05:00 UTC
    Code Review
    src/cmp.rs:1
    @@ -698,39 +717,54 @@ fn report_eof(
     }
     
     #[inline]
    -fn report_difference(
    +fn write_difference<W: Write>(
    +    out: &mut W,
         from_byte: u8,
         to_byte: u8,
         at_byte: BytesLimitU64,
         at_line: u64,
         params: &Params,
    -) {
    +) -> io::Result<()> {
         if params.quiet {
    -        return;
    +        return Ok(());
         }
     
         let term = if is_posix_locale("LC_MESSAGES") && !params.print_bytes {
             "char"
         } else {
             "byte"
         };
    -    print!(
    +    write!(
    +        out,
             "{} {} differ: {term} {}, line {}",
             params.from.to_string_lossy(),
             params.to.to_string_lossy(),
             at_byte,
             at_line
    -    );
    +    )?;
         if params.print_bytes {
    -        print!(
    +        write!(
    +            out,
                 " is {:>3o} {} {:>3o} {}",
                 from_byte,
                 format_visible_byte(from_byte),
                 to_byte,
                 format_visible_byte(to_byte)
    -        );
    +        )?;
    +    }
    +    writeln!(out)
    +}
    +
    +fn stdout_error(executable: &std::ffi::OsStr, err: &io::Error) -> String {

    Replaced the empty string with a CmpError::BrokenPipe variant.

    yunusarda-sudo yunusarda-sudo commented on code on Nov 30, 2026 05:00 UTC
    Code Review
    src/cmp.rs:1
    @@ -698,39 +717,54 @@ fn report_eof(
     }
     
     #[inline]
    -fn report_difference(
    +fn write_difference<W: Write>(
    +    out: &mut W,
         from_byte: u8,
         to_byte: u8,
         at_byte: BytesLimitU64,
         at_line: u64,
         params: &Params,
    -) {
    +) -> io::Result<()> {
         if params.quiet {
    -        return;
    +        return Ok(());
         }
     
         let term = if is_posix_locale("LC_MESSAGES") && !params.print_bytes {
             "char"
         } else {
             "byte"
         };
    -    print!(
    +    write!(
    +        out,
             "{} {} differ: {term} {}, line {}",
             params.from.to_string_lossy(),
             params.to.to_string_lossy(),
             at_byte,
             at_line
    -    );
    +    )?;
         if params.print_bytes {
    -        print!(
    +        write!(
    +            out,
                 " is {:>3o} {} {:>3o} {}",
                 from_byte,
                 format_visible_byte(from_byte),
                 to_byte,
                 format_visible_byte(to_byte)
    -        );
    +        )?;
    +    }
    +    writeln!(out)
    +}
    +
    +fn stdout_error(executable: &std::ffi::OsStr, err: &io::Error) -> String {
    +    if err.kind() == io::ErrorKind::BrokenPipe {
    +        String::new()
    +    } else {
    +        format_failure_to_read_input_file(
    +            &OsString::from(executable),

    It now takes &OsString.

    Reviewers
    Assignees
    yunusarda-sudo yunusarda-sudo
    Labels
    risk: low confidence: 92.3% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 38% (MEDIUM) · Higher = larger blast radius
    38%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 92.3% · Lower = rushed or shallow review
    92.3%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 2.9% · Safe to merge
    2.9%