Skip to content

find - -printf never flushes stdout, so write errors are lost and output comes after -exec #961

Closed

Description

@sylvestre

Known: #905 (merged PR) — #905 (related: reports -printf write errors, but only for bytes actually flushed; the unflushed tail is still lost)

-printf writes into the line-buffered stdout and never flushes it, except for \c. When
the format does not end in \n, as in the common -printf '%p\0', the last buffered bytes
are written by the Rust runtime after find has already chosen its exit status. A write
error at that point (ENOSPC, EIO) is dropped, so find exits 0 with nothing on stderr.
-print0, which does flush, correctly reports the error. The same missing flush also makes
-printf text appear after the output of a later -exec child.

Reproduction

Rust (incorrect)

$ mkdir t && touch t/a t/b
$ ./target/release/find t -printf '%p\0' > /dev/full
# Output: (none)
# Exit code: 0
$ ./target/release/find t -print0 > /dev/full          # -print0 is fine
find: 'standard output': No space left on device
# Exit code: 1
$ ./target/release/find t -maxdepth 0 -printf 'P:%p ' -exec echo E:{} \;
E:t
P:t
# Exit code: 0

GNU (correct)

$ LC_ALL=C /usr/bin/find t -printf '%p\0' > /dev/full
/usr/bin/find: 'standard output': No space left on device
/usr/bin/find: write error
# Exit code: 1
$ /usr/bin/find t -maxdepth 0 -printf 'P:%p ' -exec echo E:{} \;
P:t E:t
# Exit code: 0

Root cause

Printf::print in src/find/matchers/printf.rs:765-818 writes each component to out. The
only flush() is in the \c branch (line 773):

FormatComponent::Flush => {
    out.flush()?;
    break;
}

Printer (-print / -print0) flushes after every record, which is why its errors are
reported.

Test

In tests/test_find.rs:

#[test]
#[cfg(target_os = "linux")]
fn find_printf_without_newline_reports_full_disk() {
    let dev_full = File::create("/dev/full").unwrap();
    ucmd()
        .args(&["./test_data/simple", "-printf", "%p\\0"])
        .set_stdout(dev_full)
        .fails_with_code(1)
        .stderr_contains("No space left on device");
}

Reported by Anthropic's OSS Scanner (ANT-2026-1MT0ISRS).

Activity

  1. added
    oss-scanner
    gnu-compat
    on Nov 9, 2026
  2. RagedPanda commented on Nov 10, 2026

    @RagedPanda
    Contributor

    I'd like to work on this. I'll open a PR with a fix and tests shortly.

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

    buggnu-compatgood first issueoss-scanner

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions