use super::super::{CertRule, RuleViolation};
use crate::manifest::{RuleCategory, Severity};
use crate::utility::cert_c::ast_utils::get_node_text;
use lang_parsing_substrate::query;
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 scan(&self, node: &Node, source: &str, violations: &mut Vec<RuleViolation>) {
self.check_unbounded_allocations(node, source, violations);
}
}
impl Mem11C {
fn check_unbounded_allocations(
&self,
node: &Node,
source: &str,
violations: &mut Vec<RuleViolation>,
) {
for loop_node in query::find_descendants_of_kinds(
*node,
&["while_statement", "do_statement", "for_statement"],
) {
if self.contains_memory_allocation(&loop_node, source)
&& !self.has_bound_check(&loop_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: loop_node.start_position().row + 1,
column: loop_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),
});
}
}
}
fn contains_memory_allocation(&self, node: &Node, source: &str) -> bool {
query::find_first_descendant(*node, |n| {
if n.kind() != "call_expression" {
return false;
}
match n.child_by_field_name("function") {
Some(function) => {
let func_name = get_node_text(&function, source);
func_name == "malloc" || func_name == "calloc" || func_name == "realloc"
}
None => false,
}
})
.is_some()
}
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,
) {
for n in query::find_descendants_of_kinds(
*node,
&[
"update_expression",
"assignment_expression",
"binary_expression",
"if_statement",
],
) {
let node_text = get_node_text(&n, source);
if n.kind() == "update_expression" {
*has_increment = true;
} else if n.kind() == "assignment_expression" {
if node_text.contains("+=")
|| node_text.contains("= ") && node_text.contains(" + 1")
{
*has_increment = true;
}
}
if n.kind() == "binary_expression" {
if let Some(operator) = n.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 n.kind() == "if_statement" {
if let Some(body) = n.child_by_field_name("consequence") {
let body_text = get_node_text(&body, source);
if body_text.contains("break") {
if let Some(condition) = n.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;
}
}
}
}
}
}
}
}