Skip to content

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

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

DeepakRoy58 wants to merge 2 commits 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

.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

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

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.

== 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

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

Copy link
Copy Markdown

GNU testsuite comparison:

Congrats! The gnu test tests/env/env is no longer failing!
Congrats! The gnu test tests/misc/usage_vs_getopt is no longer failing!

@codspeed

codspeed Bot commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

Merging this PR will improve performance by 20.31%

2 warnings, 1 note

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

⚠️ 1 benchmark with a significant change ran on different hardware

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

Learn more about Macro Runners

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

⚡ 1 improved benchmark
✅ 412 untouched benchmarks
⏩ 70 skipped benchmarks1

Performance Changes

Mode Benchmark BASE HEAD Efficiency
⚡ Memory cp_recursive_deep_tree[(120, 4)] 699.2 KB 581.2 KB +20.31%

Tip

Curious why performance improved? Comment @codspeedbot explain why performance improved on this PR, or directly use the CodSpeed MCP with your agent.


Comparing DeepakRoy58:fix/issue-16325-symlink-loop-missing (f068705) with main (c44859c)2

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

  2. No successful run was found on main (316d43b) during the generation of this report, so c44859c was used instead as the comparison base. There might be some changes unrelated to this pull request in this report. ↩

@sylvestre
sylvestre force-pushed the fix/issue-16325-symlink-loop-missing branch from f068705 to 4e94c40 Compare November 18, 2026 14:34

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