Skip to content

Commit

Permalink
Fix
Browse files Browse the repository at this point in the history
  • Loading branch information
TilakMaddy committed Oct 18, 2024
1 parent 1acd400 commit 345c897
Show file tree
Hide file tree
Showing 7 changed files with 249 additions and 34 deletions.
65 changes: 47 additions & 18 deletions aderyn_core/src/detect/low/reverts_and_requries_in_loops.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
use std::{collections::BTreeMap, error::Error};

use crate::ast::{NodeID, NodeType};
use crate::ast::NodeID;

use crate::{
capture,
context::{browser::GetClosestAncestorOfTypeX, workspace_context::WorkspaceContext},
detect::detector::{IssueDetector, IssueDetectorNamePool, IssueSeverity},
context::{
browser::{ExtractIdentifiers, ExtractRevertStatements},
graph::{CallGraph, CallGraphDirection, CallGraphVisitor},
workspace_context::WorkspaceContext,
},
detect::{
detector::{IssueDetector, IssueDetectorNamePool, IssueSeverity},
helpers::get_explore_centers_of_loops,
},
};
use eyre::Result;

Expand All @@ -18,21 +25,15 @@ pub struct RevertsAndRequiresInLoopsDetector {

impl IssueDetector for RevertsAndRequiresInLoopsDetector {
fn detect(&mut self, context: &WorkspaceContext) -> Result<bool, Box<dyn Error>> {
// Collect all require statements
let requires_and_reverts = context
.identifiers()
.into_iter()
.filter(|&id| id.name == "revert" || id.name == "require")
.collect::<Vec<_>>();

for item in requires_and_reverts {
if let Some(for_loop) = item.closest_ancestor_of_type(context, NodeType::ForStatement) {
capture!(self, context, for_loop);
}
if let Some(while_loop) =
item.closest_ancestor_of_type(context, NodeType::WhileStatement)
{
capture!(self, context, while_loop);
let loop_explore_centers = get_explore_centers_of_loops(context);

for l in loop_explore_centers {
let callgraph = CallGraph::new(context, &[l], CallGraphDirection::Inward)?;
let mut tracker = RevertAndRequireTracker::default();
callgraph.accept(context, &mut tracker)?;

if tracker.has_require_or_revert || tracker.has_revert_statement {
capture!(self, context, l);
}
}

Expand Down Expand Up @@ -60,6 +61,34 @@ impl IssueDetector for RevertsAndRequiresInLoopsDetector {
}
}

#[derive(Default)]
struct RevertAndRequireTracker {
has_require_or_revert: bool,
has_revert_statement: bool,
}

impl CallGraphVisitor for RevertAndRequireTracker {
fn visit_any(&mut self, node: &crate::ast::ASTNode) -> eyre::Result<()> {
// Check for revert() and require() calls
let identifiers = ExtractIdentifiers::from(node).extracted;

let requires_and_reverts_are_present =
identifiers.iter().any(|id| id.name == "revert" || id.name == "require");

if requires_and_reverts_are_present {
self.has_require_or_revert = true;
}

// Check for revert statements
let revert_statements = ExtractRevertStatements::from(node).extracted;

if !revert_statements.is_empty() {
self.has_revert_statement = true;
}
Ok(())
}
}

#[cfg(test)]
mod reevrts_and_requires_in_loops {
use serial_test::serial;
Expand Down
98 changes: 97 additions & 1 deletion reports/ccip-functions-report.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 27 additions & 3 deletions reports/prb-math-report.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions reports/report.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions reports/report.sarif

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

42 changes: 39 additions & 3 deletions reports/sablier-aderyn-toml-nested-root.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 345c897

Please sign in to comment.