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 Con38C;
impl CertRule for Con38C {
fn rule_id(&self) -> &'static str {
"CON38-C"
}
fn description(&self) -> &'static str {
"Preserve thread safety and liveness when using condition variables"
}
fn severity(&self) -> Severity {
Severity::Low
}
fn category(&self) -> RuleCategory {
RuleCategory::Rule
}
fn cert_id(&self) -> &'static str {
"CON38-C"
}
fn check(&self, node: &Node, source: &str) -> Vec<RuleViolation> {
let mut violations = Vec::new();
self.check_node(node, source, &mut violations);
violations
}
}
impl Con38C {
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_signal_function(&func_name) {
self.report_violation(node, source, violations);
}
}
}
for i in 0..node.child_count() {
if let Some(child) = node.child(i) {
self.check_node(&child, source, violations);
}
}
}
fn is_signal_function(&self, func_name: &str) -> bool {
matches!(func_name.trim(), "cnd_signal" | "pthread_cond_signal")
}
fn report_violation(&self, node: &Node, source: &str, violations: &mut Vec<RuleViolation>) {
let line = node.start_position().row + 1;
let column = node.start_position().column + 1;
let (cond_var, is_array_access) = self
.get_condition_variable(node, source)
.unwrap_or_else(|| ("condition variable".to_string(), false));
if is_array_access {
return;
}
violations.push(RuleViolation {
rule_id: self.rule_id().to_string(),
severity: self.severity(),
message: format!(
"Use of cnd_signal() with '{}' may cause deadlock if multiple threads wait with different predicates",
cond_var
),
file_path: String::new(),
line,
column,
suggestion: Some(
"Consider using cnd_broadcast() to wake all waiting threads and ensure liveness. \
Alternatively, use unique condition variables per thread if signaling specific threads.".to_string()
),
..Default::default()
});
}
fn get_condition_variable(&self, call_node: &Node, source: &str) -> Option<(String, bool)> {
if let Some(args_node) = call_node.child_by_field_name("arguments") {
for i in 0..args_node.child_count() {
if let Some(child) = args_node.child(i) {
if child.kind() != "(" && child.kind() != ")" && child.kind() != "," {
let arg_text = get_node_text(&child, source);
let is_array_access = child.kind() == "subscript_expression"
|| self.contains_subscript(&child);
let var_name = arg_text.trim().trim_start_matches('&').to_string();
return Some((var_name, is_array_access));
}
}
}
}
None
}
fn contains_subscript(&self, node: &Node) -> bool {
if node.kind() == "subscript_expression" {
return true;
}
for i in 0..node.child_count() {
if let Some(child) = node.child(i) {
if child.kind() == "subscript_expression" || self.contains_subscript(&child) {
return true;
}
}
}
false
}
}