mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
Merge pull request #4590 from ethereum/msgValueModifier
Warn if modifier uses msg.value in non-payable function
This commit is contained in:
@@ -10537,11 +10537,18 @@ BOOST_AUTO_TEST_CASE(non_payable_throw)
|
||||
contract C {
|
||||
uint public a;
|
||||
function f() public returns (uint) {
|
||||
return msgvalue();
|
||||
}
|
||||
function msgvalue() internal returns (uint) {
|
||||
return msg.value;
|
||||
}
|
||||
function() external {
|
||||
update();
|
||||
}
|
||||
function update() internal {
|
||||
a = msg.value + 1;
|
||||
}
|
||||
|
||||
}
|
||||
)";
|
||||
compileAndRun(sourceCode, 0, "C");
|
||||
@@ -10564,6 +10571,9 @@ BOOST_AUTO_TEST_CASE(no_nonpayable_circumvention_by_modifier)
|
||||
if (false) _; // avoid the function, we should still not accept ether
|
||||
}
|
||||
function f() tryCircumvent public returns (uint) {
|
||||
return msgvalue();
|
||||
}
|
||||
function msgvalue() internal returns (uint) {
|
||||
return msg.value;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,4 @@
|
||||
contract C {
|
||||
modifier costs(uint _amount) { require(msg.value >= _amount); _; }
|
||||
function f() costs(1 ether) public payable {}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
contract C {
|
||||
modifier costs(uint _amount) { require(msg.value >= _amount); _; }
|
||||
function f() costs(1 ether) public pure {}
|
||||
}
|
||||
// ----
|
||||
// TypeError: (101-115): Function declared as pure, but this expression (potentially) reads from the environment or state and thus requires "view".
|
||||
@@ -0,0 +1,6 @@
|
||||
contract C {
|
||||
modifier costs(uint _amount) { require(msg.value >= _amount); _; }
|
||||
function f() costs(1 ether) public view {}
|
||||
}
|
||||
// ----
|
||||
// TypeError: (101-115): This modifier uses "msg.value" and thus the function has to be payable or internal.
|
||||
+1
-1
@@ -4,4 +4,4 @@ contract C {
|
||||
}
|
||||
}
|
||||
// ----
|
||||
// Warning: (52-61): "msg.value" used in non-payable function. Do you want to add the "payable" modifier to this function?
|
||||
// TypeError: (52-61): "msg.value" can only be used in payable public functions. Make the function "payable" or use an internal function to avoid this error.
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
contract C {
|
||||
modifier m(uint _amount, uint _avail) { require(_avail >= _amount); _; }
|
||||
function f() m(1 ether, msg.value) public pure {}
|
||||
}
|
||||
// ----
|
||||
// TypeError: (118-127): Function declared as pure, but this expression (potentially) reads from the environment or state and thus requires "view".
|
||||
@@ -0,0 +1,6 @@
|
||||
contract C {
|
||||
modifier m(uint _amount, uint _avail) { require(_avail >= _amount); _; }
|
||||
function f() m(1 ether, msg.value) public view {}
|
||||
}
|
||||
// ----
|
||||
// TypeError: (118-127): "msg.value" can only be used in payable public functions. Make the function "payable" or use an internal function to avoid this error.
|
||||
Reference in New Issue
Block a user