-
Notifications
You must be signed in to change notification settings - Fork 107
feat(LibBytes): add address<->bytes32 conversion helpers (EXSC-618) [LibBytes v1.1.0] #2055
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
base: main
Are you sure you want to change the base?
Changes from 1 commit
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 |
|---|---|---|
| @@ -1,14 +1,16 @@ | ||
| // SPDX-License-Identifier: LGPL-3.0-only | ||
| pragma solidity ^0.8.17; | ||
|
|
||
| /// @custom:version 1.0.0 | ||
| /// @custom:version 1.1.0 | ||
| library LibBytes { | ||
| // solhint-disable no-inline-assembly | ||
|
|
||
| // LibBytes specific errors | ||
| error SliceOverflow(); | ||
| error SliceOutOfBounds(); | ||
| error AddressOutOfBounds(); | ||
| error HexLengthInsufficient(); | ||
| error NotAnAddress(bytes32 value); | ||
|
|
||
| bytes16 private constant _SYMBOLS = "0123456789abcdef"; | ||
|
|
||
|
|
@@ -121,8 +123,33 @@ | |
| buffer[i] = _SYMBOLS[value & 0xf]; | ||
| value >>= 4; | ||
| } | ||
| // solhint-disable-next-line gas-custom-errors | ||
| require(value == 0, "Strings: hex length insufficient"); | ||
| if (value != 0) revert HexLengthInsufficient(); | ||
|
Contributor
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.
Per Suggestion: either split the
Contributor
Author
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. Not bumping SquidFacet's version here — but you're right that the transitive bytecode change should be surfaced, so I've added a note to the PR description. Reasoning: The only real action item was your "surface it rather than fold it silently" point — done: the description now flags that SquidFacet's compiled bytecode/revert-encoding changes (dead-code only) so whoever next deploys it expects the 1.0.0 bytecode to differ. |
||
| return string(buffer); | ||
| } | ||
|
|
||
| /// @notice Left-zero-pads an address into bytes32. Widening — always lossless. | ||
| /// @param _addr The address to convert. | ||
| /// @return The bytes32 representation of the address. | ||
| function toBytes32(address _addr) internal pure returns (bytes32) { | ||
| return bytes32(uint256(uint160(_addr))); | ||
Check noticeCode scanning / Olympix Integrated Security Performing a narrowing downcast may result in silent overflow due to bit truncation. For more information, visit: http://detectors.olympixdevsectools.com/article/web3-vulnerability/unsafe-downcast Low
Performing a narrowing downcast may result in silent overflow due to bit truncation. For more information, visit: http://detectors.olympixdevsectools.com/article/web3-vulnerability/unsafe-downcast
|
||
|
0xDEnYO marked this conversation as resolved.
Dismissed
|
||
| } | ||
|
|
||
| /// @notice Narrows a left-zero-padded bytes32 to an address, reverting if the | ||
| /// top 96 bits are set (checked downcast). | ||
| /// @param _value The bytes32 value to convert. | ||
| /// @return The address representation of the bytes32. | ||
| function toAddress(bytes32 _value) internal pure returns (address) { | ||
| if (uint256(_value) >> 160 != 0) revert NotAnAddress(_value); | ||
| return address(uint160(uint256(_value))); | ||
Check noticeCode scanning / Olympix Integrated Security Performing a narrowing downcast may result in silent overflow due to bit truncation. For more information, visit: http://detectors.olympixdevsectools.com/article/web3-vulnerability/unsafe-downcast Low
Performing a narrowing downcast may result in silent overflow due to bit truncation. For more information, visit: http://detectors.olympixdevsectools.com/article/web3-vulnerability/unsafe-downcast
|
||
|
0xDEnYO marked this conversation as resolved.
Dismissed
|
||
| } | ||
|
|
||
| /// @notice Narrows a bytes32 to an address by truncation, keeping only the low | ||
| /// 160 bits. Use ONLY where dropping the high bits is intentional. | ||
| /// @param _value The bytes32 value to convert. | ||
| /// @return The address representation of the low 160 bits. | ||
| function toAddressUnchecked( | ||
| bytes32 _value | ||
| ) internal pure returns (address) { | ||
| return address(uint160(uint256(_value))); | ||
Check noticeCode scanning / Olympix Integrated Security Performing a narrowing downcast may result in silent overflow due to bit truncation. For more information, visit: http://detectors.olympixdevsectools.com/article/web3-vulnerability/unsafe-downcast Low
Performing a narrowing downcast may result in silent overflow due to bit truncation. For more information, visit: http://detectors.olympixdevsectools.com/article/web3-vulnerability/unsafe-downcast
|
||
|
0xDEnYO marked this conversation as resolved.
Dismissed
|
||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bare
// TODObreadcrumbs.The 21 breadcrumbs reference "LibBytes v1.1.0" but not the tracking ticket (EXSC-618). Un-ticketed
TODOs tend to become permanent — consider referencing the ticket ID so they're greppable and closable, e.g.// TODO(EXSC-618): migrate to LibBytes.toBytes32. Also worth confirming the repo's solhint config doesn't flagTODO, and noting this runs against the repo's "minimal comments" default (though the migration-breadcrumb rationale is defensible).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done in 0fbe4b5 — created a dedicated tracking ticket (EXSC-626) and rewrote all 21 breadcrumbs to
// TODO(EXSC-626): migrate to LibBytes.…so they're greppable and closable.Used a new ticket rather than EXSC-618 since EXSC-618 closes with this PR, whereas the migrations outlive it (each rides its facet's next version bump).
.solhint.jsonhas nomax-line-lengthrule, so the longer comments are fine.