Skip to content

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

Closed
ghost wants to merge 2 commits into
incomingfrom
unknown repository
Closed

ghost wants to merge 2 commits into
incomingfrom
unknown repository

Conversation

@ghost

@ghost ghost commented Sep 3, 2012

Copy link
Copy Markdown

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

ghost commented Sep 3, 2012

Copy link
Copy Markdown
Author

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
U007D pushed a commit to U007D/rust-mos that referenced this pull request Aug 21, 2026
3364: Fix union classification r=matklad a=matklad



bors r+
🤖

Co-authored-by: Aleksey Kladov <[email protected]>
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