Skip to content

grep: report dangling symlinks and directory loops with -R - #222

Open
yunusarda-sudo wants to merge 5 commits into
uutils:mainfrom
yunusarda-sudo:fix-grep-R-dangling-symlink-loop
Open

yunusarda-sudo wants to merge 5 commits into
uutils:mainfrom
yunusarda-sudo:fix-grep-R-dangling-symlink-loop

Conversation

@yunusarda-sudo

Copy link
Copy Markdown
Contributor

Fixes #212

Problem

When recursing with -R (--dereference-recursive), grep previously ignored walk errors (let Ok(entry) = entry else { continue; }), which caused dangling symlinks and recursive directory cycles to be silently skipped without warning or setting error exit code 2.

Solution

  • Inspect errors returned by WalkDir in Searcher::process_dir.
  • Set self.had_error = true so that the error is reflected in the exit status.
  • If err.loop_ancestor().is_some(), call report_recursive_loop to emit grep: <path>: warning: recursive directory loop to stderr.
  • Else if err.io_error() is present, call report_io_error to emit grep: <path>: <err> to stderr.
  • Suppress diagnostics when -s / --no-messages is enabled.
  • Added integration test dereference_recursive_reports_dangling_link_and_loop in tests/test_grep.rs.

@codecov

codecov Bot commented Nov 18, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 95.00000% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 97.27%. Comparing base (333120c) to head (e978540).

Files with missing lines Patch % Lines
src/searcher.rs 92.30% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #222      +/-   ##
==========================================
+ Coverage   97.25%   97.27%   +0.02%     
==========================================
  Files           6        6              
  Lines        2147     2165      +18     
  Branches      235      238       +3     
==========================================
+ Hits         2088     2106      +18     
  Misses         59       59              
Flag Coverage Δ
macOS_latest 97.86% <95.00%> (+0.01%) ⬆️
ubuntu_latest 97.86% <95.00%> (+0.01%) ⬆️
windows_latest 0.00% <0.00%> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@codspeed

codspeed Bot commented Nov 18, 2026 •

Copy link
Copy Markdown

Merging this PR will not alter performance

1 warning

⚠️ 3 benchmarks are dominated by system calls

System calls cannot be simulated consistently and are left out of the measure. The reported value understates the real cost. Switch to the Walltime instrument to measure them.

Learn more about system calls

See affected benchmarks

✅ 10 untouched benchmarks
⏩ 17 skipped benchmarks1


Comparing yunusarda-sudo:fix-grep-R-dangling-symlink-loop (e978540) with main (333120c)

Open in CodSpeed

Footnotes

  1. 17 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
Copy Markdown

GNU grep testsuite comparison:

Test results comparison:
  Current:   TOTAL: 128 / PASSED: 76 / FAILED: 31 / SKIPPED: 21
  Reference: TOTAL: 128 / PASSED: 75 / FAILED: 32 / SKIPPED: 21

Changes from main branch:
  TOTAL: +0
  PASSED: +1
  FAILED: -1

Test improvements (1):
  + symlink

Comment thread tests/test_grep.rs

let mut c = scene.cmd(env!("CARGO_BIN_EXE_grep"));
c.args(&["-R", "match", "tree"])
.fails_with_code(2)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could be stdout_is("tree/sub/hit.txt:match here\n")?
it would also catch the loop being walked twice

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated: it now uses stdout_is("tree/sub/hit.txt:match here\n").

@github-actions

Copy link
Copy Markdown

GNU grep testsuite comparison:

Test results comparison:
  Current:   TOTAL: 128 / PASSED: 76 / FAILED: 31 / SKIPPED: 21
  Reference: TOTAL: 128 / PASSED: 75 / FAILED: 32 / SKIPPED: 21

Changes from main branch:
  TOTAL: +0
  PASSED: +1
  FAILED: -1

Test improvements (1):
  + symlink

2 similar comments
@github-actions

Copy link
Copy Markdown

GNU grep testsuite comparison:

Test results comparison:
  Current:   TOTAL: 128 / PASSED: 76 / FAILED: 31 / SKIPPED: 21
  Reference: TOTAL: 128 / PASSED: 75 / FAILED: 32 / SKIPPED: 21

Changes from main branch:
  TOTAL: +0
  PASSED: +1
  FAILED: -1

Test improvements (1):
  + symlink

@github-actions

Copy link
Copy Markdown

GNU grep testsuite comparison:

Test results comparison:
  Current:   TOTAL: 128 / PASSED: 76 / FAILED: 31 / SKIPPED: 21
  Reference: TOTAL: 128 / PASSED: 75 / FAILED: 32 / SKIPPED: 21

Changes from main branch:
  TOTAL: +0
  PASSED: +1
  FAILED: -1

Test improvements (1):
  + symlink

Comment thread src/searcher.rs Outdated
if err.loop_ancestor().is_some() {
self.writer.report_recursive_loop(path);
} else {
let fallback_err = io::Error::other("walk error");

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

walkdir always has an io error when it isn't a loop, so this fallback can't happen, no?
else if let Some(io_err) = err.io_error() was fine, no need for a fake "walk error" just for coverage

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, the fallback could not happen. It now uses else if let Some(io_err) = err.io_error() only.

@yunusarda-sudo
yunusarda-sudo force-pushed the fix-grep-R-dangling-symlink-loop branch from 0271a1c to e978540 Compare November 22, 2026 13:53
@github-actions

Copy link
Copy Markdown

GNU grep testsuite comparison:

Test results comparison:
  Current:   TOTAL: 128 / PASSED: 77 / FAILED: 30 / SKIPPED: 21
  Reference: TOTAL: 128 / PASSED: 76 / FAILED: 31 / SKIPPED: 21

Changes from main branch:
  TOTAL: +0
  PASSED: +1
  FAILED: -1

Test improvements (1):
  + symlink

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.

grep - grep -R silently skips dangling symlinks and directory loops

2 participants