Skip to content

File path comparison without canonicalizing #17155

@estebank

Description

@estebank

What it does

When comparing paths, std does this purely on a string equality basis, which will fail with things like symlinks or relative paths. We should be recommending people use std::fs::canonicalize to avoid mistakes.

Advantage

Using canonicalize on a Path makes it so that there are no possibilities of paths that are supposed to be equal ever being considered different.

Drawbacks

No response

Example

    let p = std::path::Path::new("foo/../foo");
    p == Path::new("foo");
    p == "foo";

Could be written as:

    std::fs::canonicalize(p) == Path::new("foo");
    std::fs::canonicalize(p) == "foo";

Comparison with existing lints

No response

Additional Context

This is one of the bugs found in uutils by Canonical's audit: https://corrode.dev/blog/bugs-rust-wont-catch/#string-equality-on-paths-is-not-the-same-as-filesystem-identity

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-lintArea: New lints

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions