Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit ed3d487

Browse files
committedJul 3, 2024
stir the hash state a little to avoid prefix collisions
1 parent 5ca124f commit ed3d487

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed
 

‎std/src/path.rs

+9-4
Original file line numberDiff line numberDiff line change
@@ -3098,15 +3098,19 @@ impl Hash for Path {
30983098
let bytes = &bytes[prefix_len..];
30993099

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

31033106
for i in 0..bytes.len() {
31043107
let is_sep = if verbatim { is_verbatim_sep(bytes[i]) } else { is_sep_byte(bytes[i]) };
31053108
if is_sep {
31063109
if i > component_start {
31073110
let to_hash = &bytes[component_start..i];
3111+
chunk_bits = chunk_bits.wrapping_add(to_hash.len());
3112+
chunk_bits = chunk_bits.rotate_right(2);
31083113
h.write(to_hash);
3109-
bytes_hashed += to_hash.len();
31103114
}
31113115

31123116
// skip over separator and optionally a following CurDir item
@@ -3127,11 +3131,12 @@ impl Hash for Path {
31273131

31283132
if component_start < bytes.len() {
31293133
let to_hash = &bytes[component_start..];
3134+
chunk_bits = chunk_bits.wrapping_add(to_hash.len());
3135+
chunk_bits = chunk_bits.rotate_right(2);
31303136
h.write(to_hash);
3131-
bytes_hashed += to_hash.len();
31323137
}
31333138

3134-
h.write_usize(bytes_hashed);
3139+
h.write_usize(chunk_bits);
31353140
}
31363141
}
31373142

0 commit comments

Comments
 (0)
Please sign in to comment.