Skip to content

Conversation

@SaathwikDasari
Copy link
Contributor

Description

Fixes #9845

The previous implementation of fold attempted to read input line-by-line using read_until. When processing infinite streams without newlines (such as /dev/zero), this caused the internal buffer to grow indefinitely, leading to a hang or OOM (Out of Memory) crash.

Changes

  • Refactored fold_file to use a fixed-size stack buffer (8KB) and Read::read instead of BufRead::read_until.
  • Added logic to process and flush the buffer in chunks, allowing infinite streams to be processed continuously without exhausting memory.
  • Added a regression test using /dev/null (guarded by #[cfg(unix)] to avoid Windows CI failures).

Verification

  • Verified locally that fold /dev/zero no longer hangs and streams output correctly.
  • Verified cargo test --package uu_fold passes.

@github-actions
Copy link

github-actions bot commented Jan 1, 2026

GNU testsuite comparison:

GNU test failed: tests/fold/fold-characters. tests/fold/fold-characters is passing on 'main'. Maybe you have to rebase?

@codspeed-hq
Copy link

codspeed-hq bot commented Jan 1, 2026

CodSpeed Performance Report

Merging #9966 will degrade performance by 6.42%

Comparing SaathwikDasari:fix-fold-dev-zero (9a67924) with main (f60072d)

Summary

❌ 1 regression
✅ 129 untouched
⏩ 30 skipped1

⚠️ Please fix the performance issues or acknowledge them on CodSpeed.

Benchmarks breakdown

Benchmark BASE HEAD Efficiency
fold_many_lines[100000] 80.4 ms 85.9 ms -6.42%

Footnotes

  1. 30 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports.

@github-actions
Copy link

github-actions bot commented Jan 1, 2026

GNU testsuite comparison:

GNU test failed: tests/fold/fold-characters. tests/fold/fold-characters is passing on 'main'. Maybe you have to rebase?

@github-actions
Copy link

github-actions bot commented Jan 1, 2026

GNU testsuite comparison:

GNU test failed: tests/fold/fold-characters. tests/fold/fold-characters is passing on 'main'. Maybe you have to rebase?
GNU test failed: tests/sort/sort-stale-thread-mem. tests/sort/sort-stale-thread-mem is passing on 'main'. Maybe you have to rebase?
Skip an intermittent issue tests/timeout/timeout (fails in this run but passes in the 'main' branch)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fold: fold /dev/zero should not finish

1 participant