-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Low detector: Multiple placeholders in modifier (#726)
Co-authored-by: Alex Roan <[email protected]>
- Loading branch information
1 parent
52b1d6e
commit 23fd850
Showing
7 changed files
with
186 additions
and
8 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
use std::collections::BTreeMap; | ||
use std::error::Error; | ||
|
||
use crate::ast::NodeID; | ||
|
||
use crate::capture; | ||
use crate::context::browser::ExtractPlaceholderStatements; | ||
use crate::detect::detector::IssueDetectorNamePool; | ||
use crate::{ | ||
context::workspace_context::WorkspaceContext, | ||
detect::detector::{IssueDetector, IssueSeverity}, | ||
}; | ||
use eyre::Result; | ||
|
||
// HOW TO USE THIS TEMPLATE: | ||
// 1. Copy this file and rename it to the snake_case version of the issue you are detecting. | ||
// 2. Rename the TemplateDetector struct and impl to your new issue name. | ||
// 3. Add this file and detector struct to the mod.rs file in the same directory. | ||
// 4. Implement the detect function to find instances of the issue. | ||
|
||
#[derive(Default)] | ||
pub struct MultiplePlaceholdersDetector { | ||
// Keys are: [0] source file name, [1] line number, [2] character location of node. | ||
// Do not add items manually, use `capture!` to add nodes to this BTreeMap. | ||
found_instances: BTreeMap<(String, usize, String), NodeID>, | ||
hints: BTreeMap<(String, usize, String), String>, | ||
} | ||
|
||
impl IssueDetector for MultiplePlaceholdersDetector { | ||
fn detect(&mut self, context: &WorkspaceContext) -> Result<bool, Box<dyn Error>> { | ||
for modifier in context.modifier_definitions() { | ||
let placeholders = ExtractPlaceholderStatements::from(modifier).extracted; | ||
if placeholders.len() > 1 { | ||
capture!(self, context, modifier); | ||
} | ||
} | ||
Ok(!self.found_instances.is_empty()) | ||
} | ||
|
||
fn severity(&self) -> IssueSeverity { | ||
IssueSeverity::Low | ||
} | ||
|
||
fn title(&self) -> String { | ||
String::from("Modifier has multiple placeholders.") | ||
} | ||
|
||
fn description(&self) -> String { | ||
String::from("Design the modifier to only contain 1 placeholder statement. If it's not possible, split the logic into multiple modifiers.") | ||
} | ||
|
||
fn instances(&self) -> BTreeMap<(String, usize, String), NodeID> { | ||
self.found_instances.clone() | ||
} | ||
|
||
fn hints(&self) -> BTreeMap<(String, usize, String), String> { | ||
self.hints.clone() | ||
} | ||
|
||
fn name(&self) -> String { | ||
IssueDetectorNamePool::MultiplePlaceholders.to_string() | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod multiple_placeholder_tests { | ||
use serial_test::serial; | ||
|
||
use crate::detect::{ | ||
detector::IssueDetector, low::multiple_placeholders::MultiplePlaceholdersDetector, | ||
}; | ||
|
||
#[test] | ||
#[serial] | ||
fn multiple_placeholders_test() { | ||
let context = crate::detect::test_utils::load_solidity_source_unit( | ||
"../tests/contract-playground/src/MultiplePlaceholders.sol", | ||
); | ||
|
||
let mut detector = MultiplePlaceholdersDetector::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(), 1); | ||
// assert the severity is low | ||
assert_eq!( | ||
detector.severity(), | ||
crate::detect::detector::IssueSeverity::Low | ||
); | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,22 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity 0.8.19; | ||
|
||
contract MultiplePlaceholders { | ||
address internal owner; | ||
|
||
constructor() { | ||
owner = msg.sender; | ||
} | ||
|
||
modifier checkOwner() { | ||
require(msg.sender == owner, "You are not the owner!"); | ||
_; | ||
_; | ||
} | ||
|
||
// aderyn-ignore-next-line(empty-block) | ||
function restrictedFunction1() external checkOwner {} | ||
|
||
// aderyn-ignore-next-line(empty-block) | ||
function restrictedFunction2() external checkOwner {} | ||
} |