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 Sig01C;
impl CertRule for Sig01C {
fn rule_id(&self) -> &'static str {
"SIG01-C"
}
fn description(&self) -> &'static str {
"Understand implementation-specific details regarding signal handler persistence"
}
fn severity(&self) -> Severity {
Severity::Low
}
fn category(&self) -> RuleCategory {
RuleCategory::Recommendation
}
fn cert_id(&self) -> &'static str {
"SIG01-C"
}
fn check(&self, node: &Node, source: &str) -> Vec<RuleViolation> {
let mut violations = Vec::new();
self.check_node(node, source, &mut violations);
violations
}
}
impl Sig01C {
fn check_node(&self, node: &Node, source: &str, violations: &mut Vec<RuleViolation>) {
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 == "signal" {
if let Some(args) = node.child_by_field_name("arguments") {
let arg_list = self.get_arguments(&args, source);
if arg_list.len() >= 2 {
let handler_arg = arg_list[1].trim();
if !handler_arg.starts_with("SIG_")
&& handler_arg != "NULL"
&& handler_arg != "0"
{
violations.push(RuleViolation {
rule_id: self.rule_id().to_string(),
severity: Severity::Low,
message: "Use of signal() has implementation-defined behavior regarding handler persistence. Consider using sigaction() for portable behavior.".to_string(),
file_path: String::new(),
line: node.start_position().row + 1,
column: node.start_position().column + 1,
suggestion: Some(
"Replace signal() with sigaction() to ensure consistent, well-defined behavior across platforms. sigaction() provides explicit control over handler persistence via the SA_RESETHAND flag.".to_string()
),
..Default::default()
});
}
}
}
}
}
}
for i in 0..node.child_count() {
if let Some(child) = node.child(i) {
self.check_node(&child, source, violations);
}
}
}
fn get_arguments(&self, args_node: &Node, source: &str) -> Vec<String> {
let mut arguments = Vec::new();
for i in 0..args_node.child_count() {
if let Some(child) = args_node.child(i) {
let kind = child.kind();
if kind != "," && kind != "(" && kind != ")" {
let arg_text = get_node_text(&child, source).to_string();
arguments.push(arg_text);
}
}
}
arguments
}
}