-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Alex Roan <alex@cyfrin.io>
- Loading branch information
1 parent
57a1328
commit a9cccd8
Showing
13 changed files
with
630 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
use crate::detect::helpers::is_constant_boolean; | ||
use crate::issue_detector; | ||
use eyre::Result; | ||
|
||
issue_detector! { | ||
MisusedBooleanDetector; | ||
|
||
severity: High, | ||
title: "Misused boolean with logical operators", | ||
desc: "The patterns `if (… || true)` and `if (.. && false)` will always evaluate to true and false respectively.", | ||
name: MisusedBoolean, | ||
|
||
|context| { | ||
for binary_operation in context.binary_operations() { | ||
if (binary_operation.operator == "||" || binary_operation.operator == "&&") | ||
&& [ | ||
binary_operation.left_expression.as_ref(), | ||
binary_operation.right_expression.as_ref(), | ||
] | ||
.iter() | ||
.any(|&operand| is_constant_boolean(context, operand)) | ||
{ | ||
grab!(binary_operation); | ||
} | ||
|
||
} | ||
|
||
for if_statement in context.if_statements() | ||
.iter() | ||
.filter(|statement| is_constant_boolean(context, &statement.condition)) { | ||
grab!(if_statement); | ||
} | ||
|
||
} | ||
|
||
} | ||
|
||
#[cfg(test)] | ||
mod misused_boolean_tests { | ||
use crate::detect::{detector::IssueDetector, high::misused_boolean::MisusedBooleanDetector}; | ||
|
||
#[test] | ||
fn test_misused_boolean_by_loading_contract_directly() { | ||
let context = crate::detect::test_utils::load_solidity_source_unit( | ||
"../tests/contract-playground/src/MisusedBoolean.sol", | ||
); | ||
|
||
let mut detector = MisusedBooleanDetector::default(); | ||
let found = detector.detect(&context).unwrap(); | ||
// assert that the detector found an issue | ||
assert!(found); | ||
// assert that the detector found the correct number of instances | ||
assert_eq!(detector.instances().len(), 10); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.