Skip to content

Commit 7429f39

Browse files
committed
Include non-redundant separators in the hash for Path
1 parent 103806b commit 7429f39

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

library/std/src/path.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3109,17 +3109,30 @@ impl Hash for Path {
31093109
bytes_hashed += to_hash.len();
31103110
}
31113111

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

31163116
let tail = &bytes[component_start..];
31173117

31183118
if !verbatim {
31193119
component_start += match tail {
3120+
// At the end of the path, e.g. `foo/`
3121+
[] => 0,
3122+
// At the end of the path followed by a CurDir component, e.g. `foo/.`
31203123
[b'.'] => 1,
3124+
// Followed by a CurDir component, another separator, and a component e.g. `foo/./bar`
31213125
[b'.', sep @ _, ..] if is_sep_byte(*sep) => 1,
3122-
_ => 0,
3126+
// Followed by another separator and a component, e.g. `foo//bar`
3127+
[sep @ _, ..] if is_sep_byte(*sep) => 1,
3128+
// Otherwise, it's a separator followed by a new component, e.g. `foo/bar`
3129+
// and we should hash the separator to distinguish from `foobar`
3130+
_ => {
3131+
let to_hash = &[b'/' as u8];
3132+
h.write(to_hash);
3133+
bytes_hashed += to_hash.len();
3134+
0
3135+
}
31233136
};
31243137
}
31253138
}

0 commit comments

Comments
 (0)