Skip to content

Improper custom error handling in expectRevertCustomError when using via-ir #4349

Open
@r0qs

Description

@r0qs

This line will return null and not an Array object if there is no match for a custom error message (see String.prototype.match() documentation). Thus, it will cause const match = error.match(/some-regex/) on line 20 to attempt to access a member of null and throw a type error.

💻 Environment
OpenZeppelin commit: b425a72
Solidity version: 0.8.19 (0.8.19+commit.7dd6d404.Linux.g++)
Relevant Solidity compiler settings: {viaIR: true, optimizer: {enabled: true}}
Hardhat version: 2.14.1
NodeJS version: v18.16.0

📝 Details

You can see an example of such a case here

In this example the error message is: Returned error: VM Exception while processing transaction: revert with unrecognized return data or custom error and there is nothing inside single quotes, so the current regex /'(.*)'/ will not capture anything.

It seems like Hardhat changes the revert messages of errors or it is not handling custom errors properly. Because this comment suggests that you are expecting an error format that is not the one returned when using via-ir:

// The revert message for custom errors looks like:
// VM Exception while processing transaction:
// reverted with custom error 'InvalidAccountNonce("0x70997970C51812dc3A010C7d01b50e0d17dc79C8", 0)

For instance, this test https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/test/proxy/utils/Initializable.test.js#L131, reverts without via-ir with: Error: VM Exception while processing transaction: reverted with custom error 'AlreadyInitialized()', while enabling via-ir, it reverts with: Error: Returned error: VM Exception while processing transaction: revert with unrecognized return data or custom error

🔢 Code to reproduce bug

git clone https://github.com/OpenZeppelin/openzeppelin-contracts.git
cd openzeppelin-contracts
git reset --hard b425a722409fdf4f3e0ec8f42d686f3c0522af19
npm install
sed -i "s|it(\('can lock contract after initialization'\)|it.only(\1|g" test/proxy/utils/Initializable.test.js
# The options COMPILE_MODE=production and IR=true are required to set the compiler settings as described above in the `Environment` section.
COMPILE_VERSION=0.8.19 COMPILE_MODE=production IR=true npm run test

Honestly, I think Hardhat shouldn't change the revert messages but just return them as they are, since they already have it here as ReturnData, but yeah...

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugtestsTest suite and helpers.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions