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

stop Hash implementation for str from writing extra byte. #25237

Closed
wants to merge 1 commit into from
Closed

stop Hash implementation for str from writing extra byte. #25237

wants to merge 1 commit into from

Conversation

cpjreynolds
Copy link
Contributor

Calling hash() on a str eg:

let teststr: &str = "abc";
teststr.hash(&mut state);

Results in an extra 0xFF byte being written into the hasher after the string.

This is a simple PR that stops the extra byte from being written.

@rust-highfive
Copy link
Contributor

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @alexcrichton (or someone else) soon.

If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. The way Github handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes.

Please see CONTRIBUTING.md for more information.

@huonw
Copy link
Member

huonw commented May 9, 2015

The byte is actually quite important: it is designed to protect against trivial hash-collisions, e.g. without the byte, ("foo", "bar") and ("", "foobar") would feed the same sequence of bytes (b"foobar") into the hasher, likely resulting in the same hash. The 0xFF byte cannot ever occur in a str, and so ensures that cases like the above always result in different byte sequences (b"foo\xFFbar\xFF" vs b"\xFFfoobar\xFF").

Thanks for the PR, but unfortunately we can't accept it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants