Skip to content

fix str::eq_slice off-by-one error #3364

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

Closed
wants to merge 2 commits into from
Closed

fix str::eq_slice off-by-one error #3364

wants to merge 2 commits into from

Conversation

ghost
Copy link

@ghost ghost commented Sep 3, 2012

The new memcmp-based implementation of str::eq_slice (54a8d69) uses str::as_buf, which, according to its documentation, passes one more than the real length of the string in order to facilitate null-terminator checks. eq_slice doesn't take this into account, and compares the next byte beyond the ends of the slices it is given. For full strings, this is the null terminator, but for slices in the middle of a string, this is whatever the next UTF-8 byte is.

Therefore, code like assert str::view("foobar", 0, 3) == "foo"; fails.

This fix simply subtracts one from the length yielded by as_buf in order to compensate for this.

@ghost ghost closed this Sep 3, 2012
@ghost
Copy link
Author

ghost commented Sep 3, 2012

First pull request, realized I didn't rebase onto incoming. Redoing this before submitting again.

jaisnan pushed a commit to jaisnan/rust-dev that referenced this pull request Jul 29, 2024
Resolves rust-lang#3358 

Related upstream commits: 
- rust-lang@21dc49c587 ptr::metadata:
avoid references to extern types
This pull request was closed.
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.

1 participant