Skip to content

Commit ef103f3

Browse files
Amxxfrangio
andauthored
Replace some uses of abi.encodePacked with more explicit alternatives (#4296)
Co-authored-by: Francisco <[email protected]>
1 parent 5cc1ea0 commit ef103f3

File tree

8 files changed

+15
-11
lines changed

8 files changed

+15
-11
lines changed

.changeset/flat-bottles-wonder.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'openzeppelin-solidity': minor
3+
---
4+
5+
Replace some uses of `abi.encodePacked` with clearer alternatives (e.g. `bytes.concat`, `string.concat`).

contracts/governance/compatibility/GovernorCompatibilityBravo.sol

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ abstract contract GovernorCompatibilityBravo is IGovernorTimelock, IGovernorComp
155155
for (uint256 i = 0; i < fullcalldatas.length; ++i) {
156156
fullcalldatas[i] = bytes(signatures[i]).length == 0
157157
? calldatas[i]
158-
: abi.encodePacked(bytes4(keccak256(bytes(signatures[i]))), calldatas[i]);
158+
: bytes.concat(abi.encodeWithSignature(signatures[i]), calldatas[i]);
159159
}
160160

161161
return fullcalldatas;

contracts/mocks/ReentrancyAttack.sol

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ pragma solidity ^0.8.19;
55
import "../utils/Context.sol";
66

77
contract ReentrancyAttack is Context {
8-
function callSender(bytes4 data) public {
9-
(bool success, ) = _msgSender().call(abi.encodeWithSelector(data));
8+
function callSender(bytes calldata data) public {
9+
(bool success, ) = _msgSender().call(data);
1010
require(success, "ReentrancyAttack: failed call");
1111
}
1212
}

contracts/mocks/ReentrancyMock.sol

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,7 @@ contract ReentrancyMock is ReentrancyGuard {
3333

3434
function countAndCall(ReentrancyAttack attacker) public nonReentrant {
3535
_count();
36-
bytes4 func = bytes4(keccak256("callback()"));
37-
attacker.callSender(func);
36+
attacker.callSender(abi.encodeCall(this.callback, ()));
3837
}
3938

4039
function _count() private {

contracts/token/ERC1155/extensions/ERC1155URIStorage.sol

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ abstract contract ERC1155URIStorage is ERC1155 {
4242
function uri(uint256 tokenId) public view virtual override returns (string memory) {
4343
string memory tokenURI = _tokenURIs[tokenId];
4444

45-
// If token URI is set, concatenate base URI and tokenURI (via abi.encodePacked).
46-
return bytes(tokenURI).length > 0 ? string(abi.encodePacked(_baseURI, tokenURI)) : super.uri(tokenId);
45+
// If token URI is set, concatenate base URI and tokenURI (via string.concat).
46+
return bytes(tokenURI).length > 0 ? string.concat(_baseURI, tokenURI) : super.uri(tokenId);
4747
}
4848

4949
/**

contracts/token/ERC721/ERC721.sol

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ abstract contract ERC721 is Context, ERC165, IERC721, IERC721Metadata, IERC721Er
9797
_requireMinted(tokenId);
9898

9999
string memory baseURI = _baseURI();
100-
return bytes(baseURI).length > 0 ? string(abi.encodePacked(baseURI, tokenId.toString())) : "";
100+
return bytes(baseURI).length > 0 ? string.concat(baseURI, tokenId.toString()) : "";
101101
}
102102

103103
/**

contracts/token/ERC721/extensions/ERC721URIStorage.sol

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ abstract contract ERC721URIStorage is IERC4906, ERC721 {
3535
if (bytes(base).length == 0) {
3636
return _tokenURI;
3737
}
38-
// If both are set, concatenate the baseURI and tokenURI (via abi.encodePacked).
38+
// If both are set, concatenate the baseURI and tokenURI (via string.concat).
3939
if (bytes(_tokenURI).length > 0) {
40-
return string(abi.encodePacked(base, _tokenURI));
40+
return string.concat(base, _tokenURI);
4141
}
4242

4343
return super.tokenURI(tokenId);

contracts/utils/Strings.sol

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ library Strings {
4747
* @dev Converts a `int256` to its ASCII `string` decimal representation.
4848
*/
4949
function toString(int256 value) internal pure returns (string memory) {
50-
return string(abi.encodePacked(value < 0 ? "-" : "", toString(SignedMath.abs(value))));
50+
return string.concat(value < 0 ? "-" : "", toString(SignedMath.abs(value)));
5151
}
5252

5353
/**

0 commit comments

Comments
 (0)