Skip to content

Commit af73c8e

Browse files
committed
stir the hash state a little to avoid prefix collisions
1 parent dd509c7 commit af73c8e

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

library/std/src/path.rs

+10-4
Original file line numberDiff line numberDiff line change
@@ -3098,15 +3098,20 @@ 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 = 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 += to_hash.len();
3112+
chunk_bits = chunk_bits.rotate_right(2);
31083113
h.write(to_hash);
3109-
bytes_hashed += to_hash.len();
3114+
31103115
}
31113116

31123117
// skip over separator and optionally a following CurDir item
@@ -3127,11 +3132,12 @@ impl Hash for Path {
31273132

31283133
if component_start < bytes.len() {
31293134
let to_hash = &bytes[component_start..];
3135+
chunk_bits += to_hash.len();
3136+
chunk_bits = chunk_bits.rotate_right(2);
31303137
h.write(to_hash);
3131-
bytes_hashed += to_hash.len();
31323138
}
31333139

3134-
h.write_usize(bytes_hashed);
3140+
h.write_usize(chunk_bits);
31353141
}
31363142
}
31373143

0 commit comments

Comments
 (0)