Skip to content
Open
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
14 changes: 4 additions & 10 deletions library/std/src/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3809,18 +3809,15 @@ impl Hash for Path {
let bytes = &bytes[prefix_len..];

let mut component_start = 0;
// track some extra state to avoid prefix collisions.
// ["foo", "bar"] and ["foobar"], will have the same payload bytes
// but result in different chunk_bits
let mut chunk_bits: usize = 0;

for i in 0..bytes.len() {
let is_sep = if verbatim { is_verbatim_sep(bytes[i]) } else { is_sep_byte(bytes[i]) };
if is_sep {
if i > component_start {
let to_hash = &bytes[component_start..i];
chunk_bits = chunk_bits.wrapping_add(to_hash.len());
chunk_bits = chunk_bits.rotate_right(2);
// for each path segment, include the length of the segment in the hash
// this avoids cases where the path "foo/bar" is equivalent to "foobar"
h.write_length_prefix(to_hash.len());
h.write(to_hash);
}

Expand All @@ -3842,12 +3839,9 @@ impl Hash for Path {

if component_start < bytes.len() {
let to_hash = &bytes[component_start..];
chunk_bits = chunk_bits.wrapping_add(to_hash.len());
chunk_bits = chunk_bits.rotate_right(2);
h.write_length_prefix(to_hash.len());
h.write(to_hash);
}

h.write_usize(chunk_bits);
}
}

Expand Down
Loading