Skip to content

realpath, readlink: keep two-link symlink loop name with -m - #16564

Open
DeepakRoy58 wants to merge 1 commit into
uutils:mainfrom
DeepakRoy58:fix/issue-16325-symlink-loop-missing
Open

DeepakRoy58 wants to merge 1 commit into
uutils:mainfrom
DeepakRoy58:fix/issue-16325-symlink-loop-missing

Conversation

@DeepakRoy58

Copy link
Copy Markdown
Contributor

What changed

With -m, GNU does not fail on a two-link symlink loop. It keeps the loop's
name instead:

  • realpath -m a prints the absolute path of a
  • realpath -m a/x prints the absolute path of a/x

uutils previously reported Too many levels of symbolic links for both. The
shared canonicalize function now detects a two-link loop in missing mode and
returns the loop name plus any trailing components.

Why

GNU treats the loop as an unresolved component when missing components are
allowed. This matches the reported issue and avoids a spurious failure.

Scope

This change only affects two-link symlink loops in missing mode. Growing loops
such as foo -> foo/bar still fail, as before. Other modes and path cases are
unchanged.

Overlapping PRs

PRs #16153, #16034, and #15388 also touch canonicalize. This change only
affects symlink loops and does not alter their dangling-symlink,
non-directory-component, or permission-error handling.

Compared with GNU

GNU coreutils 9.12 on Arch Linux:

  • two-link loop a -> b, b -> a:
    • realpath -m a -> /tmp/.../a, exit 0
    • realpath -m a/x -> /tmp/.../a/x, exit 0
    • readlink -m a -> /tmp/.../a, exit 0
    • readlink -m a/x -> /tmp/.../a/x, exit 0

uutils after the fix produces the same results.

Tests

  • cargo fmt --all: passed
  • cargo clippy -p uucore -p uu_realpath -p uu_readlink: passed
  • cargo test --features unix --test tests -- symlink_loop_missing: 2 passed
  • cargo test --features unix --test tests -- test_realpath:: test_readlink::: 65 passed

closes #16325

Comment thread src/uucore/src/lib/features/fs.rs Outdated
.and_then(|p| p.parent())
.map(PathBuf::from)
== Some(result.clone())
&& link_path.file_name() == base_symlink.as_ref().and_then(|p| p.file_name())

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.

this also triggers without any loop: with a -> b and b -> sub/a, realpath -m a now prints $PWD/a instead of $PWD/sub/a
please add a test for it

Comment thread src/uucore/src/lib/features/fs.rs Outdated
miss_mode: MissingHandling,
res_mode: ResolveMode,
) -> IOResult<PathBuf> {
let loop_component = original.as_ref().components().find_map(|c| match c {

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.

why only the first component of the argument? realpath -m "$PWD/a" or realpath -m dir/a still fail with ELOOP

Comment thread src/uucore/src/lib/features/fs.rs Outdated
result.pop();
// GNU keeps the name of a two-link symlink loop when missing
// components are allowed (-m), instead of reporting ELOOP.
if miss_mode == MissingHandling::Missing

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.

this is quite complex for what it does. Could we keep a set of the links already visited and, in Missing mode, stop on a revisit instead?
That should also cover the p -> a case from the issue.

Comment thread src/uucore/src/lib/features/fs.rs Outdated
== Some(result.clone())
&& link_path.file_name() == base_symlink.as_ref().and_then(|p| p.file_name())
{
let mut loop_result = base_symlink.as_ref().unwrap().clone();

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.

please avoid the unwrap(), if let Some(base) = &base_symlink would do

Comment thread src/uucore/src/lib/features/fs.rs Outdated
Ok(result)
}

#[derive(Clone, PartialEq)]

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.

is this derive needed? i don't see where it is used

let expect = path_concat!(at.root_dir_resolved(), "a") + "\n";
assert_eq!(actual, expect);

let (at, mut ucmd) = at_and_ucmd!();

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.

same setup twice, one scene with two ucmd() calls would do. Please also add a case with an absolute path

@sylvestre

Copy link
Copy Markdown
Contributor

thanks for the PR!
This can't land yet: the check keys on the first component of the argument. It misses absolute paths and returns a wrong path for a plain a -> b -> sub/a chain (details inline).
Please also squash the fixup commit into the first one.

@github-actions

github-actions Bot commented Nov 17, 2026 •

Copy link
Copy Markdown

GNU testsuite comparison:

GNU test failed: tests/readlink/readlink-fp-loop. tests/readlink/readlink-fp-loop is passing on 'main'. Maybe you have to rebase?

@codspeed

codspeed Bot commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

Merging this PR will regress 2 benchmarks

3 warnings, 1 note

⚠️ 121 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

⚠️ 5 benchmarks with a significant change ran on different hardware

Their base and head results were measured on machines with a different CPU, core count or memory, so the changes might not come from the code. Run base and head on the same runner type.

Learn more about Macro Runners

See affected benchmarks

⚠️ 3 benchmarks with a significant change ran in a different software environment

Their base and head results were measured with a different OS or toolchain, such as compiler, runtime or library versions, so the changes might come from this difference rather than from the code. Open an affected benchmark to see what changed.

See affected benchmarks

ℹ️ 1 benchmark has no flamegraph

The flamegraph wasn't generated: most of the time was spent in runtime internals, which would leave nothing useful to analyze.

See affected benchmarks

⚡ 3 improved benchmarks
❌ 2 regressed benchmarks
✅ 408 untouched benchmarks
⏩ 70 skipped benchmarks1

Warning

Please fix the performance issues or acknowledge them on CodSpeed.

Performance Changes

Mode Benchmark BASE HEAD Efficiency
❌ CPU Simulation true_consecutive_calls 294.7 ns 350.2 ns -15.86%
❌ CPU Simulation df_deep_directory 888.3 µs 934.1 µs -4.9%
⚡ Memory socket_addrs_direct[100000] 68 KB 8.7 KB ×7.9
⚡ Memory hostname_ip_lookup[100000] 66.4 KB 9.1 KB ×7.3
⚡ CPU Simulation false_consecutive_calls 350.2 ns 294.7 ns +18.85%

Tip

Investigate this regression by commenting @codspeedbot fix this regression on this PR, or directly use the CodSpeed MCP with your agent.


Comparing DeepakRoy58:fix/issue-16325-symlink-loop-missing (81fbfc1) with main (9311390)

Open in CodSpeed

Footnotes

  1. 70 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. ↩

@sylvestre
sylvestre force-pushed the fix/issue-16325-symlink-loop-missing branch from f068705 to 4e94c40 Compare November 18, 2026 14:34
@DeepakRoy58
DeepakRoy58 force-pushed the fix/issue-16325-symlink-loop-missing branch 2 times, most recently from 629045d to 02b82a2 Compare November 18, 2026 21:20
@github-actions

github-actions Bot commented Nov 19, 2026 •

Copy link
Copy Markdown

Binary size comparison:

Individual binary size comparison VS main (threshold: >=5% AND >=4 KB).

Total size of compared binaries: 155.27 MB (-116 KB, -0.07%)

Significant per-binary changes:
  env     1.20 MB ->    1.28 MB  (+80 KB, +6.51%)

@sylvestre
sylvestre force-pushed the fix/issue-16325-symlink-loop-missing branch from 02b82a2 to 81fbfc1 Compare November 20, 2026 07:09

This branch has not been deployed

No deployments
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.

realpath -m / readlink -m fail on a symlink loop

2 participants