use super::super::{CertRule, RuleViolation};
use crate::manifest::{RuleCategory, Severity};
use crate::utility::cert_c::ast_utils::get_node_text;
use tree_sitter::Node;
pub struct Mem11C;
impl CertRule for Mem11C {
fn rule_id(&self) -> &'static str {
"MEM11-C"
}
fn description(&self) -> &'static str {
"Do not assume infinite heap space"
}
fn severity(&self) -> Severity {
Severity::Low
}
fn category(&self) -> RuleCategory {
RuleCategory::Rule
}
fn cert_id(&self) -> &'static str {
"MEM11-C"
}
fn check(&self, node: &Node, source: &str) -> Vec<RuleViolation> {
let mut violations = Vec::new();
self.check_unbounded_allocations(node, source, &mut violations);
violations
}
}
impl Mem11C {
fn check_unbounded_allocations(
&self,
node: &Node,
source: &str,
violations: &mut Vec<RuleViolation>,
) {
match node.kind() {
"while_statement" | "do_statement" | "for_statement"
if self.contains_memory_allocation(node, source)
&& !self.has_bound_check(node, source) => {
violations.push(RuleViolation {
rule_id: self.rule_id().to_string(),
message: "Memory allocation in loop without upper bound check. \
Unbounded memory allocation can exhaust heap space. \
Consider using databases, file storage, or implementing \
a maximum size limit for data structures."
.to_string(),
severity: self.severity(),
line: node.start_position().row + 1,
column: node.start_position().column + 1,
file_path: String::new(),
suggestion: Some(
"Add a counter to limit iterations or use persistent storage \
(database/file) instead of accumulating unbounded data in memory"
.to_string(),
),
requires_manual_review: Some(true),
});
}
_ => {}
}
for i in 0..node.child_count() {
if let Some(child) = node.child(i) {
self.check_unbounded_allocations(&child, source, violations);
}
}
}
fn contains_memory_allocation(&self, node: &Node, source: &str) -> bool {
if node.kind() == "call_expression" {
if let Some(function) = node.child_by_field_name("function") {
let func_name = get_node_text(&function, source);
if func_name == "malloc" || func_name == "calloc" || func_name == "realloc" {
return true;
}
}
}
for i in 0..node.child_count() {
if let Some(child) = node.child(i) {
if self.contains_memory_allocation(&child, source) {
return true;
}
}
}
false
}
fn has_bound_check(&self, node: &Node, source: &str) -> bool {
if self.has_counter_with_limit(node, source) {
return true;
}
false
}
fn has_counter_with_limit(&self, node: &Node, source: &str) -> bool {
let mut has_increment = false;
let mut has_comparison = false;
self.search_counter_patterns(node, source, &mut has_increment, &mut has_comparison);
has_increment && has_comparison
}
fn search_counter_patterns(
&self,
node: &Node,
source: &str,
has_increment: &mut bool,
has_comparison: &mut bool,
) {
let node_text = get_node_text(node, source);
if node.kind() == "update_expression" {
*has_increment = true;
} else if node.kind() == "assignment_expression" {
if node_text.contains("+=") || node_text.contains("= ") && node_text.contains(" + 1") {
*has_increment = true;
}
}
if node.kind() == "binary_expression" {
if let Some(operator) = node.child_by_field_name("operator") {
let op = get_node_text(&operator, source);
if op == ">=" || op == ">" || op == "<" || op == "<=" {
if node_text.contains("count")
|| node_text.contains("limit")
|| (node_text.contains("MAX") && node_text.contains(">="))
|| (node_text.contains("MAX") && node_text.contains(">"))
{
*has_comparison = true;
}
}
}
}
if node.kind() == "if_statement" {
if let Some(body) = node.child_by_field_name("consequence") {
let body_text = get_node_text(&body, source);
if body_text.contains("break") {
if let Some(condition) = node.child_by_field_name("condition") {
let cond_text = get_node_text(&condition, source);
if cond_text.contains(">=")
|| cond_text.contains(">")
|| cond_text.contains("MAX")
|| cond_text.contains("limit")
{
*has_comparison = true;
}
}
}
}
}
for i in 0..node.child_count() {
if let Some(child) = node.child(i) {
self.search_counter_patterns(&child, source, has_increment, has_comparison);
}
}
}
}