From 7b94b576f1b6103d5ef1e5db1c43be903631182a Mon Sep 17 00:00:00 2001 From: Michael Colburn Date: Fri, 28 May 2021 21:19:03 -0400 Subject: [PATCH] Add tests for the divide-before-multiply detector --- .../0.4.25/divide_before_multiply.sol | 5 + ...tiply.sol.0.4.25.DivideBeforeMultiply.json | 127 ++++++++++++++++++ .../0.5.16/divide_before_multiply.sol | 5 + ...tiply.sol.0.5.16.DivideBeforeMultiply.json | 127 ++++++++++++++++++ .../0.6.11/divide_before_multiply.sol | 5 + ...tiply.sol.0.6.11.DivideBeforeMultiply.json | 127 ++++++++++++++++++ .../0.7.6/divide_before_multiply.sol | 5 + ...ltiply.sol.0.7.6.DivideBeforeMultiply.json | 127 ++++++++++++++++++ tests/test_detectors.py | 20 +++ 9 files changed, 548 insertions(+) create mode 100644 tests/detectors/divide-before-multiply/0.4.25/divide_before_multiply.sol create mode 100644 tests/detectors/divide-before-multiply/0.4.25/divide_before_multiply.sol.0.4.25.DivideBeforeMultiply.json create mode 100644 tests/detectors/divide-before-multiply/0.5.16/divide_before_multiply.sol create mode 100644 tests/detectors/divide-before-multiply/0.5.16/divide_before_multiply.sol.0.5.16.DivideBeforeMultiply.json create mode 100644 tests/detectors/divide-before-multiply/0.6.11/divide_before_multiply.sol create mode 100644 tests/detectors/divide-before-multiply/0.6.11/divide_before_multiply.sol.0.6.11.DivideBeforeMultiply.json create mode 100644 tests/detectors/divide-before-multiply/0.7.6/divide_before_multiply.sol create mode 100644 tests/detectors/divide-before-multiply/0.7.6/divide_before_multiply.sol.0.7.6.DivideBeforeMultiply.json diff --git a/tests/detectors/divide-before-multiply/0.4.25/divide_before_multiply.sol b/tests/detectors/divide-before-multiply/0.4.25/divide_before_multiply.sol new file mode 100644 index 000000000..baf27a44e --- /dev/null +++ b/tests/detectors/divide-before-multiply/0.4.25/divide_before_multiply.sol @@ -0,0 +1,5 @@ +contract A { + function f(uint a, uint b, uint c) public returns (uint) { + return (a / b) * c; + } +} \ No newline at end of file diff --git a/tests/detectors/divide-before-multiply/0.4.25/divide_before_multiply.sol.0.4.25.DivideBeforeMultiply.json b/tests/detectors/divide-before-multiply/0.4.25/divide_before_multiply.sol.0.4.25.DivideBeforeMultiply.json new file mode 100644 index 000000000..529f9444b --- /dev/null +++ b/tests/detectors/divide-before-multiply/0.4.25/divide_before_multiply.sol.0.4.25.DivideBeforeMultiply.json @@ -0,0 +1,127 @@ +[ + [ + { + "elements": [ + { + "type": "function", + "name": "f", + "source_mapping": { + "start": 14, + "length": 92, + "filename_used": "/GENERIC_PATH", + "filename_relative": "tests/detectors/divide-before-multiply/0.4.25/divide_before_multiply.sol", + "filename_absolute": "/GENERIC_PATH", + "filename_short": "tests/detectors/divide-before-multiply/0.4.25/divide_before_multiply.sol", + "is_dependency": false, + "lines": [ + 2, + 3, + 4 + ], + "starting_column": 2, + "ending_column": 6 + }, + "type_specific_fields": { + "parent": { + "type": "contract", + "name": "A", + "source_mapping": { + "start": 0, + "length": 108, + "filename_used": "/GENERIC_PATH", + "filename_relative": "tests/detectors/divide-before-multiply/0.4.25/divide_before_multiply.sol", + "filename_absolute": "/GENERIC_PATH", + "filename_short": "tests/detectors/divide-before-multiply/0.4.25/divide_before_multiply.sol", + "is_dependency": false, + "lines": [ + 1, + 2, + 3, + 4, + 5, + 6 + ], + "starting_column": 1, + "ending_column": 0 + } + }, + "signature": "f(uint256,uint256,uint256)" + } + }, + { + "type": "node", + "name": "(a / b) * c", + "source_mapping": { + "start": 81, + "length": 18, + "filename_used": "/GENERIC_PATH", + "filename_relative": "tests/detectors/divide-before-multiply/0.4.25/divide_before_multiply.sol", + "filename_absolute": "/GENERIC_PATH", + "filename_short": "tests/detectors/divide-before-multiply/0.4.25/divide_before_multiply.sol", + "is_dependency": false, + "lines": [ + 3 + ], + "starting_column": 9, + "ending_column": 27 + }, + "type_specific_fields": { + "parent": { + "type": "function", + "name": "f", + "source_mapping": { + "start": 14, + "length": 92, + "filename_used": "/GENERIC_PATH", + "filename_relative": "tests/detectors/divide-before-multiply/0.4.25/divide_before_multiply.sol", + "filename_absolute": "/GENERIC_PATH", + "filename_short": "tests/detectors/divide-before-multiply/0.4.25/divide_before_multiply.sol", + "is_dependency": false, + "lines": [ + 2, + 3, + 4 + ], + "starting_column": 2, + "ending_column": 6 + }, + "type_specific_fields": { + "parent": { + "type": "contract", + "name": "A", + "source_mapping": { + "start": 0, + "length": 108, + "filename_used": "/GENERIC_PATH", + "filename_relative": "tests/detectors/divide-before-multiply/0.4.25/divide_before_multiply.sol", + "filename_absolute": "/GENERIC_PATH", + "filename_short": "tests/detectors/divide-before-multiply/0.4.25/divide_before_multiply.sol", + "is_dependency": false, + "lines": [ + 1, + 2, + 3, + 4, + 5, + 6 + ], + "starting_column": 1, + "ending_column": 0 + } + }, + "signature": "f(uint256,uint256,uint256)" + } + } + } + } + ], + "description": "A.f(uint256,uint256,uint256) (tests/detectors/divide-before-multiply/0.4.25/divide_before_multiply.sol#2-4) performs a multiplication on the result of a division:\n\t-(a / b) * c (tests/detectors/divide-before-multiply/0.4.25/divide_before_multiply.sol#3)\n", + "markdown": "[A.f(uint256,uint256,uint256)](tests/detectors/divide-before-multiply/0.4.25/divide_before_multiply.sol#L2-L4) performs a multiplication on the result of a division:\n\t-[(a / b) * c](tests/detectors/divide-before-multiply/0.4.25/divide_before_multiply.sol#L3)\n", + "first_markdown_element": "tests/detectors/divide-before-multiply/0.4.25/divide_before_multiply.sol#L2-L4", + "id": "fe40e2f72824d1270402b09af057ec7364af1ad2f8c48ef4df72fc27012b3186", + "check": "divide-before-multiply", + "impact": "Medium", + "confidence": "Medium" + } + ] +] \ No newline at end of file diff --git a/tests/detectors/divide-before-multiply/0.5.16/divide_before_multiply.sol b/tests/detectors/divide-before-multiply/0.5.16/divide_before_multiply.sol new file mode 100644 index 000000000..baf27a44e --- /dev/null +++ b/tests/detectors/divide-before-multiply/0.5.16/divide_before_multiply.sol @@ -0,0 +1,5 @@ +contract A { + function f(uint a, uint b, uint c) public returns (uint) { + return (a / b) * c; + } +} \ No newline at end of file diff --git a/tests/detectors/divide-before-multiply/0.5.16/divide_before_multiply.sol.0.5.16.DivideBeforeMultiply.json b/tests/detectors/divide-before-multiply/0.5.16/divide_before_multiply.sol.0.5.16.DivideBeforeMultiply.json new file mode 100644 index 000000000..4421fa0ef --- /dev/null +++ b/tests/detectors/divide-before-multiply/0.5.16/divide_before_multiply.sol.0.5.16.DivideBeforeMultiply.json @@ -0,0 +1,127 @@ +[ + [ + { + "elements": [ + { + "type": "function", + "name": "f", + "source_mapping": { + "start": 14, + "length": 92, + "filename_used": "/GENERIC_PATH", + "filename_relative": "tests/detectors/divide-before-multiply/0.5.16/divide_before_multiply.sol", + "filename_absolute": "/GENERIC_PATH", + "filename_short": "tests/detectors/divide-before-multiply/0.5.16/divide_before_multiply.sol", + "is_dependency": false, + "lines": [ + 2, + 3, + 4 + ], + "starting_column": 2, + "ending_column": 6 + }, + "type_specific_fields": { + "parent": { + "type": "contract", + "name": "A", + "source_mapping": { + "start": 0, + "length": 108, + "filename_used": "/GENERIC_PATH", + "filename_relative": "tests/detectors/divide-before-multiply/0.5.16/divide_before_multiply.sol", + "filename_absolute": "/GENERIC_PATH", + "filename_short": "tests/detectors/divide-before-multiply/0.5.16/divide_before_multiply.sol", + "is_dependency": false, + "lines": [ + 1, + 2, + 3, + 4, + 5, + 6 + ], + "starting_column": 1, + "ending_column": 0 + } + }, + "signature": "f(uint256,uint256,uint256)" + } + }, + { + "type": "node", + "name": "(a / b) * c", + "source_mapping": { + "start": 81, + "length": 18, + "filename_used": "/GENERIC_PATH", + "filename_relative": "tests/detectors/divide-before-multiply/0.5.16/divide_before_multiply.sol", + "filename_absolute": "/GENERIC_PATH", + "filename_short": "tests/detectors/divide-before-multiply/0.5.16/divide_before_multiply.sol", + "is_dependency": false, + "lines": [ + 3 + ], + "starting_column": 9, + "ending_column": 27 + }, + "type_specific_fields": { + "parent": { + "type": "function", + "name": "f", + "source_mapping": { + "start": 14, + "length": 92, + "filename_used": "/GENERIC_PATH", + "filename_relative": "tests/detectors/divide-before-multiply/0.5.16/divide_before_multiply.sol", + "filename_absolute": "/GENERIC_PATH", + "filename_short": "tests/detectors/divide-before-multiply/0.5.16/divide_before_multiply.sol", + "is_dependency": false, + "lines": [ + 2, + 3, + 4 + ], + "starting_column": 2, + "ending_column": 6 + }, + "type_specific_fields": { + "parent": { + "type": "contract", + "name": "A", + "source_mapping": { + "start": 0, + "length": 108, + "filename_used": "/GENERIC_PATH", + "filename_relative": "tests/detectors/divide-before-multiply/0.5.16/divide_before_multiply.sol", + "filename_absolute": "/GENERIC_PATH", + "filename_short": "tests/detectors/divide-before-multiply/0.5.16/divide_before_multiply.sol", + "is_dependency": false, + "lines": [ + 1, + 2, + 3, + 4, + 5, + 6 + ], + "starting_column": 1, + "ending_column": 0 + } + }, + "signature": "f(uint256,uint256,uint256)" + } + } + } + } + ], + "description": "A.f(uint256,uint256,uint256) (tests/detectors/divide-before-multiply/0.5.16/divide_before_multiply.sol#2-4) performs a multiplication on the result of a division:\n\t-(a / b) * c (tests/detectors/divide-before-multiply/0.5.16/divide_before_multiply.sol#3)\n", + "markdown": "[A.f(uint256,uint256,uint256)](tests/detectors/divide-before-multiply/0.5.16/divide_before_multiply.sol#L2-L4) performs a multiplication on the result of a division:\n\t-[(a / b) * c](tests/detectors/divide-before-multiply/0.5.16/divide_before_multiply.sol#L3)\n", + "first_markdown_element": "tests/detectors/divide-before-multiply/0.5.16/divide_before_multiply.sol#L2-L4", + "id": "a484421df5c9ebf3dc3d8bcec6701ab3065e03b21e294c4972142c4503f2fccb", + "check": "divide-before-multiply", + "impact": "Medium", + "confidence": "Medium" + } + ] +] \ No newline at end of file diff --git a/tests/detectors/divide-before-multiply/0.6.11/divide_before_multiply.sol b/tests/detectors/divide-before-multiply/0.6.11/divide_before_multiply.sol new file mode 100644 index 000000000..baf27a44e --- /dev/null +++ b/tests/detectors/divide-before-multiply/0.6.11/divide_before_multiply.sol @@ -0,0 +1,5 @@ +contract A { + function f(uint a, uint b, uint c) public returns (uint) { + return (a / b) * c; + } +} \ No newline at end of file diff --git a/tests/detectors/divide-before-multiply/0.6.11/divide_before_multiply.sol.0.6.11.DivideBeforeMultiply.json b/tests/detectors/divide-before-multiply/0.6.11/divide_before_multiply.sol.0.6.11.DivideBeforeMultiply.json new file mode 100644 index 000000000..564cac140 --- /dev/null +++ b/tests/detectors/divide-before-multiply/0.6.11/divide_before_multiply.sol.0.6.11.DivideBeforeMultiply.json @@ -0,0 +1,127 @@ +[ + [ + { + "elements": [ + { + "type": "function", + "name": "f", + "source_mapping": { + "start": 14, + "length": 92, + "filename_used": "/GENERIC_PATH", + "filename_relative": "tests/detectors/divide-before-multiply/0.6.11/divide_before_multiply.sol", + "filename_absolute": "/GENERIC_PATH", + "filename_short": "tests/detectors/divide-before-multiply/0.6.11/divide_before_multiply.sol", + "is_dependency": false, + "lines": [ + 2, + 3, + 4 + ], + "starting_column": 2, + "ending_column": 6 + }, + "type_specific_fields": { + "parent": { + "type": "contract", + "name": "A", + "source_mapping": { + "start": 0, + "length": 108, + "filename_used": "/GENERIC_PATH", + "filename_relative": "tests/detectors/divide-before-multiply/0.6.11/divide_before_multiply.sol", + "filename_absolute": "/GENERIC_PATH", + "filename_short": "tests/detectors/divide-before-multiply/0.6.11/divide_before_multiply.sol", + "is_dependency": false, + "lines": [ + 1, + 2, + 3, + 4, + 5, + 6 + ], + "starting_column": 1, + "ending_column": 0 + } + }, + "signature": "f(uint256,uint256,uint256)" + } + }, + { + "type": "node", + "name": "(a / b) * c", + "source_mapping": { + "start": 81, + "length": 18, + "filename_used": "/GENERIC_PATH", + "filename_relative": "tests/detectors/divide-before-multiply/0.6.11/divide_before_multiply.sol", + "filename_absolute": "/GENERIC_PATH", + "filename_short": "tests/detectors/divide-before-multiply/0.6.11/divide_before_multiply.sol", + "is_dependency": false, + "lines": [ + 3 + ], + "starting_column": 9, + "ending_column": 27 + }, + "type_specific_fields": { + "parent": { + "type": "function", + "name": "f", + "source_mapping": { + "start": 14, + "length": 92, + "filename_used": "/GENERIC_PATH", + "filename_relative": "tests/detectors/divide-before-multiply/0.6.11/divide_before_multiply.sol", + "filename_absolute": "/GENERIC_PATH", + "filename_short": "tests/detectors/divide-before-multiply/0.6.11/divide_before_multiply.sol", + "is_dependency": false, + "lines": [ + 2, + 3, + 4 + ], + "starting_column": 2, + "ending_column": 6 + }, + "type_specific_fields": { + "parent": { + "type": "contract", + "name": "A", + "source_mapping": { + "start": 0, + "length": 108, + "filename_used": "/GENERIC_PATH", + "filename_relative": "tests/detectors/divide-before-multiply/0.6.11/divide_before_multiply.sol", + "filename_absolute": "/GENERIC_PATH", + "filename_short": "tests/detectors/divide-before-multiply/0.6.11/divide_before_multiply.sol", + "is_dependency": false, + "lines": [ + 1, + 2, + 3, + 4, + 5, + 6 + ], + "starting_column": 1, + "ending_column": 0 + } + }, + "signature": "f(uint256,uint256,uint256)" + } + } + } + } + ], + "description": "A.f(uint256,uint256,uint256) (tests/detectors/divide-before-multiply/0.6.11/divide_before_multiply.sol#2-4) performs a multiplication on the result of a division:\n\t-(a / b) * c (tests/detectors/divide-before-multiply/0.6.11/divide_before_multiply.sol#3)\n", + "markdown": "[A.f(uint256,uint256,uint256)](tests/detectors/divide-before-multiply/0.6.11/divide_before_multiply.sol#L2-L4) performs a multiplication on the result of a division:\n\t-[(a / b) * c](tests/detectors/divide-before-multiply/0.6.11/divide_before_multiply.sol#L3)\n", + "first_markdown_element": "tests/detectors/divide-before-multiply/0.6.11/divide_before_multiply.sol#L2-L4", + "id": "9010aa3fdf8f7689d4c4d4ea0bcd771668212100e5427e48644ed7337de03a6d", + "check": "divide-before-multiply", + "impact": "Medium", + "confidence": "Medium" + } + ] +] \ No newline at end of file diff --git a/tests/detectors/divide-before-multiply/0.7.6/divide_before_multiply.sol b/tests/detectors/divide-before-multiply/0.7.6/divide_before_multiply.sol new file mode 100644 index 000000000..baf27a44e --- /dev/null +++ b/tests/detectors/divide-before-multiply/0.7.6/divide_before_multiply.sol @@ -0,0 +1,5 @@ +contract A { + function f(uint a, uint b, uint c) public returns (uint) { + return (a / b) * c; + } +} \ No newline at end of file diff --git a/tests/detectors/divide-before-multiply/0.7.6/divide_before_multiply.sol.0.7.6.DivideBeforeMultiply.json b/tests/detectors/divide-before-multiply/0.7.6/divide_before_multiply.sol.0.7.6.DivideBeforeMultiply.json new file mode 100644 index 000000000..23dd10c28 --- /dev/null +++ b/tests/detectors/divide-before-multiply/0.7.6/divide_before_multiply.sol.0.7.6.DivideBeforeMultiply.json @@ -0,0 +1,127 @@ +[ + [ + { + "elements": [ + { + "type": "function", + "name": "f", + "source_mapping": { + "start": 14, + "length": 92, + "filename_used": "/GENERIC_PATH", + "filename_relative": "tests/detectors/divide-before-multiply/0.7.6/divide_before_multiply.sol", + "filename_absolute": "/GENERIC_PATH", + "filename_short": "tests/detectors/divide-before-multiply/0.7.6/divide_before_multiply.sol", + "is_dependency": false, + "lines": [ + 2, + 3, + 4 + ], + "starting_column": 2, + "ending_column": 6 + }, + "type_specific_fields": { + "parent": { + "type": "contract", + "name": "A", + "source_mapping": { + "start": 0, + "length": 108, + "filename_used": "/GENERIC_PATH", + "filename_relative": "tests/detectors/divide-before-multiply/0.7.6/divide_before_multiply.sol", + "filename_absolute": "/GENERIC_PATH", + "filename_short": "tests/detectors/divide-before-multiply/0.7.6/divide_before_multiply.sol", + "is_dependency": false, + "lines": [ + 1, + 2, + 3, + 4, + 5, + 6 + ], + "starting_column": 1, + "ending_column": 0 + } + }, + "signature": "f(uint256,uint256,uint256)" + } + }, + { + "type": "node", + "name": "(a / b) * c", + "source_mapping": { + "start": 81, + "length": 18, + "filename_used": "/GENERIC_PATH", + "filename_relative": "tests/detectors/divide-before-multiply/0.7.6/divide_before_multiply.sol", + "filename_absolute": "/GENERIC_PATH", + "filename_short": "tests/detectors/divide-before-multiply/0.7.6/divide_before_multiply.sol", + "is_dependency": false, + "lines": [ + 3 + ], + "starting_column": 9, + "ending_column": 27 + }, + "type_specific_fields": { + "parent": { + "type": "function", + "name": "f", + "source_mapping": { + "start": 14, + "length": 92, + "filename_used": "/GENERIC_PATH", + "filename_relative": "tests/detectors/divide-before-multiply/0.7.6/divide_before_multiply.sol", + "filename_absolute": "/GENERIC_PATH", + "filename_short": "tests/detectors/divide-before-multiply/0.7.6/divide_before_multiply.sol", + "is_dependency": false, + "lines": [ + 2, + 3, + 4 + ], + "starting_column": 2, + "ending_column": 6 + }, + "type_specific_fields": { + "parent": { + "type": "contract", + "name": "A", + "source_mapping": { + "start": 0, + "length": 108, + "filename_used": "/GENERIC_PATH", + "filename_relative": "tests/detectors/divide-before-multiply/0.7.6/divide_before_multiply.sol", + "filename_absolute": "/GENERIC_PATH", + "filename_short": "tests/detectors/divide-before-multiply/0.7.6/divide_before_multiply.sol", + "is_dependency": false, + "lines": [ + 1, + 2, + 3, + 4, + 5, + 6 + ], + "starting_column": 1, + "ending_column": 0 + } + }, + "signature": "f(uint256,uint256,uint256)" + } + } + } + } + ], + "description": "A.f(uint256,uint256,uint256) (tests/detectors/divide-before-multiply/0.7.6/divide_before_multiply.sol#2-4) performs a multiplication on the result of a division:\n\t-(a / b) * c (tests/detectors/divide-before-multiply/0.7.6/divide_before_multiply.sol#3)\n", + "markdown": "[A.f(uint256,uint256,uint256)](tests/detectors/divide-before-multiply/0.7.6/divide_before_multiply.sol#L2-L4) performs a multiplication on the result of a division:\n\t-[(a / b) * c](tests/detectors/divide-before-multiply/0.7.6/divide_before_multiply.sol#L3)\n", + "first_markdown_element": "tests/detectors/divide-before-multiply/0.7.6/divide_before_multiply.sol#L2-L4", + "id": "9bc6cb85ece6cd57fa946707784180957080ad5f9575abc17af218f0d2801f77", + "check": "divide-before-multiply", + "impact": "Medium", + "confidence": "Medium" + } + ] +] \ No newline at end of file diff --git a/tests/test_detectors.py b/tests/test_detectors.py index 0f65bf137..678799cd6 100644 --- a/tests/test_detectors.py +++ b/tests/test_detectors.py @@ -1199,6 +1199,26 @@ ALL_TESTS = [ "deprecated_calls.sol", "0.4.25", ), + Test( + all_detectors.DivideBeforeMultiply, + "divide_before_multiply.sol", + "0.4.25", + ), + Test( + all_detectors.DivideBeforeMultiply, + "divide_before_multiply.sol", + "0.5.16", + ), + Test( + all_detectors.DivideBeforeMultiply, + "divide_before_multiply.sol", + "0.6.11", + ), + Test( + all_detectors.DivideBeforeMultiply, + "divide_before_multiply.sol", + "0.7.6", + ), ] GENERIC_PATH = "/GENERIC_PATH"