From 75a92b0ffd0946c17a27a58e6e02abe96cd3fa00 Mon Sep 17 00:00:00 2001 From: Erik Kundt Date: Thu, 26 Jul 2018 21:45:24 +0200 Subject: Warns if modifier uses msg.value in non-payable function. --- .../syntaxTests/memberLookup/msg_value_modifier_payable.sol | 4 ++++ .../syntaxTests/memberLookup/msg_value_modifier_pure.sol | 6 ++++++ .../syntaxTests/memberLookup/msg_value_modifier_view.sol | 6 ++++++ test/libsolidity/syntaxTests/viewPureChecker/msg_value_modifier.sol | 6 ++++++ .../syntaxTests/viewPureChecker/msg_value_modifier_view.sol | 6 ++++++ 5 files changed, 28 insertions(+) create mode 100644 test/libsolidity/syntaxTests/memberLookup/msg_value_modifier_payable.sol create mode 100644 test/libsolidity/syntaxTests/memberLookup/msg_value_modifier_pure.sol create mode 100644 test/libsolidity/syntaxTests/memberLookup/msg_value_modifier_view.sol create mode 100644 test/libsolidity/syntaxTests/viewPureChecker/msg_value_modifier.sol create mode 100644 test/libsolidity/syntaxTests/viewPureChecker/msg_value_modifier_view.sol (limited to 'test/libsolidity') diff --git a/test/libsolidity/syntaxTests/memberLookup/msg_value_modifier_payable.sol b/test/libsolidity/syntaxTests/memberLookup/msg_value_modifier_payable.sol new file mode 100644 index 00000000..6e93626f --- /dev/null +++ b/test/libsolidity/syntaxTests/memberLookup/msg_value_modifier_payable.sol @@ -0,0 +1,4 @@ +contract C { + modifier costs(uint _amount) { require(msg.value >= _amount); _; } + function f() costs(1 ether) public payable {} +} diff --git a/test/libsolidity/syntaxTests/memberLookup/msg_value_modifier_pure.sol b/test/libsolidity/syntaxTests/memberLookup/msg_value_modifier_pure.sol new file mode 100644 index 00000000..398c127d --- /dev/null +++ b/test/libsolidity/syntaxTests/memberLookup/msg_value_modifier_pure.sol @@ -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". diff --git a/test/libsolidity/syntaxTests/memberLookup/msg_value_modifier_view.sol b/test/libsolidity/syntaxTests/memberLookup/msg_value_modifier_view.sol new file mode 100644 index 00000000..49a3139c --- /dev/null +++ b/test/libsolidity/syntaxTests/memberLookup/msg_value_modifier_view.sol @@ -0,0 +1,6 @@ +contract C { + modifier costs(uint _amount) { require(msg.value >= _amount); _; } + function f() costs(1 ether) public view {} +} +// ---- +// Warning: (101-115): This modifier uses "msg.value" and thus the function should be payable. diff --git a/test/libsolidity/syntaxTests/viewPureChecker/msg_value_modifier.sol b/test/libsolidity/syntaxTests/viewPureChecker/msg_value_modifier.sol new file mode 100644 index 00000000..160b20a7 --- /dev/null +++ b/test/libsolidity/syntaxTests/viewPureChecker/msg_value_modifier.sol @@ -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". diff --git a/test/libsolidity/syntaxTests/viewPureChecker/msg_value_modifier_view.sol b/test/libsolidity/syntaxTests/viewPureChecker/msg_value_modifier_view.sol new file mode 100644 index 00000000..4cad4439 --- /dev/null +++ b/test/libsolidity/syntaxTests/viewPureChecker/msg_value_modifier_view.sol @@ -0,0 +1,6 @@ +contract C { + modifier m(uint _amount, uint _avail) { require(_avail >= _amount); _; } + function f() m(1 ether, msg.value) public view {} +} +// ---- +// Warning: (118-127): "msg.value" used in non-payable function. Do you want to add the "payable" modifier to this function? -- cgit v1.2.3 From 431c2fbcf370926d6c3e7e93667d8472f2f6b73b Mon Sep 17 00:00:00 2001 From: chriseth Date: Thu, 16 Aug 2018 17:28:49 +0200 Subject: Turn warning into error. --- test/libsolidity/syntaxTests/memberLookup/msg_value_modifier_view.sol | 2 +- .../397_warns_msg_value_in_non_payable_public_function.sol | 2 +- .../libsolidity/syntaxTests/viewPureChecker/msg_value_modifier_view.sol | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) (limited to 'test/libsolidity') diff --git a/test/libsolidity/syntaxTests/memberLookup/msg_value_modifier_view.sol b/test/libsolidity/syntaxTests/memberLookup/msg_value_modifier_view.sol index 49a3139c..8430c5c3 100644 --- a/test/libsolidity/syntaxTests/memberLookup/msg_value_modifier_view.sol +++ b/test/libsolidity/syntaxTests/memberLookup/msg_value_modifier_view.sol @@ -3,4 +3,4 @@ contract C { function f() costs(1 ether) public view {} } // ---- -// Warning: (101-115): This modifier uses "msg.value" and thus the function should be payable. +// TypeError: (101-115): This modifier uses "msg.value" and thus the function has to be payable or internal. diff --git a/test/libsolidity/syntaxTests/nameAndTypeResolution/397_warns_msg_value_in_non_payable_public_function.sol b/test/libsolidity/syntaxTests/nameAndTypeResolution/397_warns_msg_value_in_non_payable_public_function.sol index 4e1f62e1..c56ad25f 100644 --- a/test/libsolidity/syntaxTests/nameAndTypeResolution/397_warns_msg_value_in_non_payable_public_function.sol +++ b/test/libsolidity/syntaxTests/nameAndTypeResolution/397_warns_msg_value_in_non_payable_public_function.sol @@ -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. diff --git a/test/libsolidity/syntaxTests/viewPureChecker/msg_value_modifier_view.sol b/test/libsolidity/syntaxTests/viewPureChecker/msg_value_modifier_view.sol index 4cad4439..613b0198 100644 --- a/test/libsolidity/syntaxTests/viewPureChecker/msg_value_modifier_view.sol +++ b/test/libsolidity/syntaxTests/viewPureChecker/msg_value_modifier_view.sol @@ -3,4 +3,4 @@ contract C { function f() m(1 ether, msg.value) public view {} } // ---- -// Warning: (118-127): "msg.value" used in non-payable function. Do you want to add the "payable" modifier to this function? +// 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. -- cgit v1.2.3 From b7c6e53d3d74b649ee6275254d30162aef403e17 Mon Sep 17 00:00:00 2001 From: Leonardo Alt Date: Mon, 20 Aug 2018 15:13:38 +0200 Subject: Fix endToEnd test --- test/libsolidity/SolidityEndToEndTest.cpp | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'test/libsolidity') diff --git a/test/libsolidity/SolidityEndToEndTest.cpp b/test/libsolidity/SolidityEndToEndTest.cpp index 2bf20126..f058458d 100644 --- a/test/libsolidity/SolidityEndToEndTest.cpp +++ b/test/libsolidity/SolidityEndToEndTest.cpp @@ -10528,11 +10528,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"); @@ -10555,6 +10562,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; } } -- cgit v1.2.3