diff --git a/Tools/SmartCheck/Solidity/Rules/SOLIDITY_MSGVALUE_EQUALS_ZERO.md b/Tools/SmartCheck/Solidity/Rules/SOLIDITY_MSGVALUE_EQUALS_ZERO.md new file mode 100644 index 0000000000000000000000000000000000000000..554c9b7df59a18a72bdf20b20297291a4d6a8535 --- /dev/null +++ b/Tools/SmartCheck/Solidity/Rules/SOLIDITY_MSGVALUE_EQUALS_ZERO.md @@ -0,0 +1,116 @@ +# Analysis of Smart Contract Security Vulnerabilities and Tools  +     <br/>    <br/> + <br/> + + +## SOLIDITY_MSGVALUE_EQUALS_ZERO +### Rule Description +<p> + The <code>msg.value == 0</code> condition check is meaningless in most cases. +</p> + +### Solidity-Rules + +  + +``` +expression + [expression[1]/environmentalVariable[text()[1] = "msg.value"]] + [comparison[text()[1] = "=="]] + [expression[2]/primaryExpression//decimalNumber[text()[1] = "0"]] + [not(ancestor::functionDefinition[text()[1] = "constructor"])] + [not(ancestor::functionDefinition/identifier[text()[1] + = (ancestor::contractDefinition/identifier)]) + ] +``` + +### Sample Code + +``` +pragma solidity 0.4.24; + +contract MsgValue { + + constructor() public { + require(msg.value == 0); + } + + function myFunc() public returns(uint) { + // <yes> <report> SOLIDITY_MSGVALUE_EQUALS_ZERO 1df89a + require(msg.value == 0); + // <yes> <report> SOLIDITY_MSGVALUE_EQUALS_ZERO 1df89a + if(msg.value == 0) { + return(1); + } + // <yes> <report> SOLIDITY_MSGVALUE_EQUALS_ZERO 1df89a + assert(msg.value == 0); + } + + function() { + // <yes> <report> SOLIDITY_MSGVALUE_EQUALS_ZERO 1df89a + require(msg.value == 0); + } +} + +contract MsgValue2 { + + function MsgValue2() { + require(msg.value == 0); + } +} +``` + +### Abstract Syntax Tree + +[Click Here](https://astexplorer.net/#/gist/2b6b1e6d8d56a08d4a1173aa579e2c2a/2979556ee23227a659d4cec029bf24fbff291654) to view the AST for the above code. Code generated from AST Explorer using _solidity-parser-antlr-0.4.11_ + +### Code Result + +``` +SOLIDITY_MSGVALUE_EQUALS_ZERO +patternId: 1df89a +severity: 1 +line: 11 +column: 16 +content: msg.value==0 + +ruleId: SOLIDITY_MSGVALUE_EQUALS_ZERO +patternId: 1df89a +severity: 1 +line: 13 +column: 11 +content: msg.value==0 + +ruleId: SOLIDITY_MSGVALUE_EQUALS_ZERO +patternId: 1df89a +severity: 1 +line: 17 +column: 15 +content: msg.value==0 + +ruleId: SOLIDITY_MSGVALUE_EQUALS_ZERO +patternId: 1df89a +severity: 1 +line: 22 +column: 16 +content: msg.value==0 + +ruleId: SOLIDITY_VISIBILITY +patternId: 910067 +severity: 1 +line: 20 +column: 4 +content: function(){require(msg.value==0);} + +ruleId: SOLIDITY_VISIBILITY +patternId: 910067 +severity: 1 +line: 28 +column: 4 +content: functionMsgValue2(){require(msg.value==0);} + +SOLIDITY_VISIBILITY :2 +SOLIDITY_MSGVALUE_EQUALS_ZERO :4 + + +```