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 Err04C;
impl CertRule for Err04C {
fn rule_id(&self) -> &'static str {
"ERR04-C"
}
fn description(&self) -> &'static str {
"Choose an appropriate termination strategy"
}
fn severity(&self) -> Severity {
Severity::Medium
}
fn category(&self) -> RuleCategory {
RuleCategory::Recommendation
}
fn cert_id(&self) -> &'static str {
"ERR04-C"
}
fn check(&self, node: &Node, source: &str) -> Vec<RuleViolation> {
let mut violations = Vec::new();
self.check_node(node, source, &mut violations);
violations
}
}
impl Err04C {
fn check_node(&self, node: &Node, source: &str, violations: &mut Vec<RuleViolation>) {
if node.kind() == "call_expression" {
if let Some(func_node) = node.child_by_field_name("function") {
let func_name = get_node_text(&func_node, source);
if self.is_unsafe_termination(&func_name) {
if let Some(function_def) = self.find_containing_function(node) {
if self.has_file_io_operations(&function_def, source) {
self.report_violation(node, &func_name, source, violations);
}
}
}
}
}
for i in 0..node.child_count() {
if let Some(child) = node.child(i) {
self.check_node(&child, source, violations);
}
}
}
fn is_unsafe_termination(&self, func_name: &str) -> bool {
matches!(func_name.trim(), "abort" | "_Exit")
}
fn find_containing_function<'a>(&self, node: &'a Node) -> Option<Node<'a>> {
let mut current = Some(*node);
while let Some(n) = current {
if n.kind() == "function_definition" {
return Some(n);
}
current = n.parent();
}
None
}
fn has_file_io_operations(&self, function_node: &Node, source: &str) -> bool {
self.contains_file_io_call(function_node, source)
}
fn contains_file_io_call(&self, node: &Node, source: &str) -> bool {
if node.kind() == "call_expression" {
if let Some(func_node) = node.child_by_field_name("function") {
let func_name = get_node_text(&func_node, source);
if self.is_file_io_function(&func_name) {
return true;
}
}
}
for i in 0..node.child_count() {
if let Some(child) = node.child(i) {
if self.contains_file_io_call(&child, source) {
return true;
}
}
}
false
}
fn is_file_io_function(&self, name: &str) -> bool {
matches!(
name.trim(),
"fopen"
| "fclose"
| "fprintf"
| "fwrite"
| "fputs"
| "fputc"
| "fread"
| "fgets"
| "fgetc"
| "fscanf"
| "fseek"
| "ftell"
| "freopen"
| "fflush"
| "setvbuf"
| "ungetc"
| "feof"
| "ferror"
| "clearerr"
| "rewind"
| "fsetpos"
| "fgetpos"
)
}
fn report_violation(
&self,
node: &Node,
func_name: &str,
_source: &str,
violations: &mut Vec<RuleViolation>,
) {
let line = node.start_position().row + 1;
let column = node.start_position().column + 1;
violations.push(RuleViolation {
rule_id: self.rule_id().to_string(),
severity: self.severity(),
message: format!(
"Use of {}() after file I/O may leave files in inconsistent state",
func_name
),
file_path: String::new(),
line,
column,
suggestion: Some(
"Use exit() or return from main() instead to ensure proper file cleanup. \
These functions flush buffered data and close file descriptors properly."
.to_string(),
),
..Default::default()
});
}
}