Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include non-redundant separators in Hash for Path #127255

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions library/std/src/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3109,17 +3109,30 @@ impl Hash for Path {
bytes_hashed += to_hash.len();
}

// skip over separator and optionally a following CurDir item
// Skip over separators and, optionally, a following CurDir item
// since components() would normalize these away.
component_start = i + 1;

let tail = &bytes[component_start..];

if !verbatim {
component_start += match tail {
// At the end of the path, e.g. `foo/`
[] => 0,
// At the end of the path followed by a CurDir component, e.g. `foo/.`
[b'.'] => 1,
// Followed by a CurDir component, another separator, and a component e.g. `foo/./bar`
[b'.', sep @ _, ..] if is_sep_byte(*sep) => 1,
_ => 0,
// Followed by another separator and a component, e.g. `foo//bar`
[sep @ _, ..] if is_sep_byte(*sep) => 1,
// Otherwise, it's a separator followed by a new component, e.g. `foo/bar`
// and we should hash the separator to distinguish from `foobar`
_ => {
let to_hash = &[b'/' as u8];
h.write(to_hash);
bytes_hashed += to_hash.len();
0
}
};
}
}
Expand Down
35 changes: 35 additions & 0 deletions library/std/src/path/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1545,6 +1545,20 @@ pub fn test_compare() {
relative_from: Some("")
);

tc!("foo//", "foo",
eq: true,
starts_with: true,
ends_with: true,
relative_from: Some("")
);

tc!("foo///", "foo",
eq: true,
starts_with: true,
ends_with: true,
relative_from: Some("")
);

tc!("foo/.", "foo",
eq: true,
starts_with: true,
Expand All @@ -1559,13 +1573,34 @@ pub fn test_compare() {
relative_from: Some("")
);

tc!("foo/.//bar", "foo/bar",
eq: true,
starts_with: true,
ends_with: true,
relative_from: Some("")
);

tc!("foo//./bar", "foo/bar",
eq: true,
starts_with: true,
ends_with: true,
relative_from: Some("")
);

tc!("foo/bar", "foo",
eq: false,
starts_with: true,
ends_with: false,
relative_from: Some("bar")
);

tc!("foo/bar", "foobar",
eq: false,
starts_with: false,
ends_with: false,
relative_from: None
);
Comment on lines +1597 to +1602
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is the test case reported in the issue.


tc!("foo/bar/baz", "foo/bar",
eq: false,
starts_with: true,
Expand Down
Loading