mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
Allow error string for `require
`.
This commit is contained in:
parent
3da16b3e8a
commit
ae1d040285
@ -51,6 +51,7 @@ m_magicVariables(vector<shared_ptr<MagicVariableDeclaration const>>{
|
||||
make_shared<MagicVariableDeclaration>("mulmod", make_shared<FunctionType>(strings{"uint256", "uint256", "uint256"}, strings{"uint256"}, FunctionType::Kind::MulMod, false, StateMutability::Pure)),
|
||||
make_shared<MagicVariableDeclaration>("now", make_shared<IntegerType>(256)),
|
||||
make_shared<MagicVariableDeclaration>("require", make_shared<FunctionType>(strings{"bool"}, strings{}, FunctionType::Kind::Require, false, StateMutability::Pure)),
|
||||
make_shared<MagicVariableDeclaration>("require", make_shared<FunctionType>(strings{"bool", "string memory"}, strings{}, FunctionType::Kind::Require, false, StateMutability::Pure)),
|
||||
make_shared<MagicVariableDeclaration>("revert", make_shared<FunctionType>(strings(), strings(), FunctionType::Kind::Revert, false, StateMutability::Pure)),
|
||||
make_shared<MagicVariableDeclaration>("revert", make_shared<FunctionType>(strings{"string memory"}, strings(), FunctionType::Kind::Revert, false, StateMutability::Pure)),
|
||||
make_shared<MagicVariableDeclaration>("ripemd160", make_shared<FunctionType>(strings(), strings{"bytes20"}, FunctionType::Kind::RIPEMD160, true, StateMutability::Pure)),
|
||||
|
@ -917,16 +917,42 @@ bool ExpressionCompiler::visit(FunctionCall const& _functionCall)
|
||||
{
|
||||
arguments.front()->accept(*this);
|
||||
utils().convertType(*arguments.front()->annotation().type, *function.parameterTypes().front(), false);
|
||||
if (arguments.size() > 1)
|
||||
{
|
||||
// Users probably expect the second argument to be evaluated
|
||||
// even if the condition is false, as would be the case for an actual
|
||||
// function call.
|
||||
solAssert(arguments.size() == 2, "");
|
||||
solAssert(function.kind() == FunctionType::Kind::Require, "");
|
||||
arguments.at(1)->accept(*this);
|
||||
utils().moveIntoStack(1, arguments.at(1)->annotation().type->sizeOnStack());
|
||||
}
|
||||
// Stack: <error string (unconverted)> <condition>
|
||||
// jump if condition was met
|
||||
m_context << Instruction::ISZERO << Instruction::ISZERO;
|
||||
auto success = m_context.appendConditionalJump();
|
||||
if (function.kind() == FunctionType::Kind::Assert)
|
||||
// condition was not met, flag an error
|
||||
m_context.appendInvalid();
|
||||
else if (arguments.size() > 1)
|
||||
{
|
||||
m_context << u256(0);
|
||||
utils().moveIntoStack(arguments.at(1)->annotation().type->sizeOnStack(), 1);
|
||||
utils().fetchFreeMemoryPointer();
|
||||
utils().abiEncode(
|
||||
{make_shared<IntegerType>(256), arguments.at(1)->annotation().type},
|
||||
{make_shared<IntegerType>(256), make_shared<ArrayType>(DataLocation::Memory, true)}
|
||||
);
|
||||
utils().toSizeAfterFreeMemoryPointer();
|
||||
m_context << Instruction::REVERT;
|
||||
m_context.adjustStackOffset(arguments.at(1)->annotation().type->sizeOnStack());
|
||||
}
|
||||
else
|
||||
m_context.appendRevert();
|
||||
// the success branch
|
||||
m_context << success;
|
||||
if (arguments.size() > 1)
|
||||
utils().popStackElement(*arguments.at(1)->annotation().type);
|
||||
break;
|
||||
}
|
||||
case FunctionType::Kind::GasLeft:
|
||||
|
@ -10456,6 +10456,62 @@ BOOST_AUTO_TEST_CASE(revert_with_cause)
|
||||
ABI_CHECK(callContractFunction("g()"), encodeArgs(0, 0x40, 0xa0, 0, 0x40, 44, "test1234567890123456789012345678901234567890"));
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(require_with_message)
|
||||
{
|
||||
char const* sourceCode = R"(
|
||||
contract D {
|
||||
bool flag = false;
|
||||
string storageError = "abc";
|
||||
function f(uint x) public {
|
||||
require(x > 7, "failed");
|
||||
}
|
||||
function g() public {
|
||||
// As a side-effect of internalFun, the flag will be set to true
|
||||
// (even if the condition is true),
|
||||
// but it will only throw in the next evaluation.
|
||||
bool flagCopy = flag;
|
||||
require(flagCopy == false, internalFun());
|
||||
}
|
||||
function internalFun() returns (string) {
|
||||
flag = true;
|
||||
return "only on second run";
|
||||
}
|
||||
function h() public {
|
||||
require(false, storageError);
|
||||
}
|
||||
}
|
||||
contract C {
|
||||
D d = new D();
|
||||
function forward(address target, bytes data) internal returns (bool success, bytes retval) {
|
||||
uint retsize;
|
||||
assembly {
|
||||
success := call(not(0), target, 0, add(data, 0x20), mload(data), 0, 0)
|
||||
retsize := returndatasize()
|
||||
}
|
||||
retval = new bytes(retsize);
|
||||
assembly {
|
||||
returndatacopy(add(retval, 0x20), 0, returndatasize())
|
||||
}
|
||||
}
|
||||
function f(uint x) public returns (bool, bytes) {
|
||||
return forward(address(d), msg.data);
|
||||
}
|
||||
function g() public returns (bool, bytes) {
|
||||
return forward(address(d), msg.data);
|
||||
}
|
||||
function h() public returns (bool, bytes) {
|
||||
return forward(address(d), msg.data);
|
||||
}
|
||||
}
|
||||
)";
|
||||
compileAndRun(sourceCode, 0, "C");
|
||||
ABI_CHECK(callContractFunction("f(uint256)", 8), encodeArgs(1, 0x40, 0));
|
||||
ABI_CHECK(callContractFunction("f(uint256)", 5), encodeArgs(0, 0x40, 0x80, 0, 0x40, 6, "failed"));
|
||||
ABI_CHECK(callContractFunction("g()"), encodeArgs(1, 0x40, 0));
|
||||
ABI_CHECK(callContractFunction("g()"), encodeArgs(0, 0x40, 0x80, 0, 0x40, 18 , "only on second run"));
|
||||
ABI_CHECK(callContractFunction("h()"), encodeArgs(0, 0x40, 0x80, 0, 0x40, 3, "abc"));
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(negative_stack_height)
|
||||
{
|
||||
// This code was causing negative stack height during code generation
|
||||
|
Loading…
Reference in New Issue
Block a user