-
Notifications
You must be signed in to change notification settings - Fork 12.1k
Fix bug in Bytes.lastIndexOf when array is empty and position is not 2²⁵⁶-1 #5797
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
Changes from 5 commits
c62f26d
aa1cdb8
29b1b35
edf974e
655c3c0
cfbb2e6
df473af
6eb86f4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'openzeppelin-solidity': patch | ||
--- | ||
|
||
`Bytes`: Fix an issue when calling `lastIndexOf(bytes,byte,uint256)` with an empty buffer and a lookup position that is not 2²⁵⁶-1. | ||
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -58,8 +58,8 @@ library Bytes { | |||||
function lastIndexOf(bytes memory buffer, bytes1 s, uint256 pos) internal pure returns (uint256) { | ||||||
unchecked { | ||||||
uint256 length = buffer.length; | ||||||
// NOTE here we cannot do `i = Math.min(pos + 1, length)` because `pos + 1` could overflow | ||||||
for (uint256 i = Math.min(pos, length - 1) + 1; i > 0; --i) { | ||||||
uint256 end = Math.ternary(pos == type(uint256).max, length, Math.min(pos + 1, length)); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This might be more readable, and just as effective.
Suggested change
@ernestognw @vesselinux @levi-sledd WDYT ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is indeed equivalent and more readable. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @Amxx Ahh, sorry to say but I find this to be even less readable haha. That's maybe because the overflow logic is buried inside the The above said, I appreciate that readability is subjective, so feel free to adopt whichever of the two suggested variants the Contracts team like best. Indeed both are functionally equivalent and that's what matters at the end of the day. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Interesting point of view. imo "burying the logic" should be the goal of the |
||||||
for (uint256 i = end; i > 0; --i) { | ||||||
if (bytes1(_unsafeReadBytesOffset(buffer, i - 1)) == s) { | ||||||
return i - 1; | ||||||
} | ||||||
|
Uh oh!
There was an error while loading. Please reload this page.