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 std::collections::HashSet;
use tree_sitter::Node;
const STDLIB_NORETURN_FUNCTIONS: &[&str] = &["exit", "_Exit", "abort", "quick_exit", "longjmp"];
pub struct Msc37C;
impl Msc37C {
pub fn new() -> Self {
Self
}
fn is_void_type(&self, type_node: &Node, source: &str) -> bool {
let type_text = get_node_text(type_node, source);
type_text.trim() == "void"
}
fn has_void_specifier(&self, func_def: &Node, source: &str) -> bool {
for i in 0..func_def.child_count() {
if let Some(child) = func_def.child(i) {
if get_node_text(&child, source).trim() == "void" {
return true;
}
}
}
false
}
fn is_main_function(&self, declarator: &Node, source: &str) -> bool {
if let Some(func_declarator) = self.find_function_declarator(declarator) {
if let Some(name_node) = func_declarator.child_by_field_name("declarator") {
let name = get_node_text(&name_node, source);
return name.trim() == "main";
}
}
false
}
fn find_function_declarator<'a>(&self, node: &Node<'a>) -> Option<Node<'a>> {
if node.kind() == "function_declarator" {
return Some(*node);
}
for i in 0..node.child_count() {
if let Some(child) = node.child(i) {
if let Some(found) = self.find_function_declarator(&child) {
return Some(found);
}
}
}
None
}
fn has_return_statement(&self, node: &Node) -> bool {
query::find_first_descendant(*node, |n| n.kind() == "return_statement").is_some()
}
fn collect_noreturn_function_names(root: &Node, source: &str) -> HashSet<String> {
let mut names = HashSet::new();
for node in query::find_descendants_of_kinds(*root, &["declaration", "function_definition"])
{
let mut cursor = node.walk();
let is_noreturn = node.children(&mut cursor).any(|c| {
c.kind() == "type_qualifier" && get_node_text(&c, source).trim() == "_Noreturn"
});
if !is_noreturn {
continue;
}
if let Some(declarator) = node.child_by_field_name("declarator") {
if let Some(func_declarator) = Self::find_function_declarator_static(&declarator) {
if let Some(name_node) = func_declarator.child_by_field_name("declarator") {
names.insert(get_node_text(&name_node, source).trim().to_string());
}
}
}
}
names
}
fn find_function_declarator_static<'a>(node: &Node<'a>) -> Option<Node<'a>> {
if node.kind() == "function_declarator" {
return Some(*node);
}
for i in 0..node.child_count() {
if let Some(child) = node.child(i) {
if let Some(found) = Self::find_function_declarator_static(&child) {
return Some(found);
}
}
}
None
}
fn is_noreturn_call_statement(
&self,
node: &Node,
source: &str,
noreturn_names: &HashSet<String>,
) -> bool {
if node.kind() != "expression_statement" {
return false;
}
let Some(call) = node.child(0).filter(|c| c.kind() == "call_expression") else {
return false;
};
let Some(function) = call.child_by_field_name("function") else {
return false;
};
let name = get_node_text(&function, source).trim().to_string();
STDLIB_NORETURN_FUNCTIONS.contains(&name.as_str()) || noreturn_names.contains(&name)
}
fn ends_with_return(
&self,
compound_stmt: &Node,
source: &str,
noreturn_names: &HashSet<String>,
) -> bool {
self.stmt_returns(compound_stmt, source, noreturn_names)
}
fn stmt_returns(&self, root: &Node, source: &str, noreturn_names: &HashSet<String>) -> bool {
enum Op<'a> {
Eval(Node<'a>),
And,
}
let mut ops: Vec<Op> = vec![Op::Eval(*root)];
let mut values: Vec<bool> = Vec::new();
while let Some(op) = ops.pop() {
match op {
Op::Eval(node) => match node.kind() {
"return_statement" => values.push(true),
"compound_statement" => {
let mut last_stmt = None;
for i in 0..node.child_count() {
if let Some(child) = node.child(i) {
let kind = child.kind();
if kind == "{"
|| kind == "}"
|| kind == "comment"
|| kind.starts_with("preproc_")
{
continue;
}
last_stmt = Some(child);
}
}
match last_stmt {
Some(stmt)
if self.is_noreturn_call_statement(
&stmt,
source,
noreturn_names,
) =>
{
values.push(true);
}
Some(stmt) => ops.push(Op::Eval(stmt)),
None => values.push(false),
}
}
"if_statement" => {
match (
node.child_by_field_name("consequence"),
node.child_by_field_name("alternative"),
) {
(Some(consequence), Some(alternative)) => {
ops.push(Op::And);
ops.push(Op::Eval(alternative));
ops.push(Op::Eval(consequence));
}
_ => values.push(false),
}
}
"switch_statement" => {
values.push(self.has_return_statement(&node));
}
"else_clause" => {
let inner = (0..node.child_count())
.filter_map(|i| node.child(i))
.find(|child| child.kind() != "else");
match inner {
Some(child) => ops.push(Op::Eval(child)),
None => values.push(false),
}
}
_ => values.push(false),
},
Op::And => {
let b = values.pop().unwrap_or(false);
let a = values.pop().unwrap_or(false);
values.push(a && b);
}
}
}
values.pop().unwrap_or(false)
}
fn check_function_definition(
&self,
node: &Node,
source: &str,
noreturn_names: &HashSet<String>,
violations: &mut Vec<RuleViolation>,
) {
if node.kind() != "function_definition" {
return;
}
let type_node = match node.child_by_field_name("type") {
Some(n) => n,
None => return,
};
if self.is_void_type(&type_node, source) || self.has_void_specifier(node, source) {
return;
}
let type_text = get_node_text(&type_node, source);
let type_trimmed = type_text.trim();
if matches!(
type_trimmed,
"else" | "if" | "while" | "for" | "do" | "switch" | "case" | "default" | "return"
) {
return;
}
let declarator = match node.child_by_field_name("declarator") {
Some(d) => d,
None => return,
};
if self.is_main_function(&declarator, source) {
return;
}
let body = match node.child_by_field_name("body") {
Some(b) => b,
None => return,
};
if !self.has_return_statement(&body) {
violations.push(RuleViolation {
rule_id: self.rule_id().to_string(),
severity: self.severity(),
message: "Non-void function has no return statement. Control reaching the end of a non-void function without returning a value is undefined behavior.".to_string(),
file_path: String::new(),
line: node.start_position().row + 1,
column: node.start_position().column + 1,
suggestion: Some(
"Add a return statement on all execution paths of this function".to_string()
),
..Default::default()
});
return;
}
if !self.ends_with_return(&body, source, noreturn_names) {
violations.push(RuleViolation {
rule_id: self.rule_id().to_string(),
severity: self.severity(),
message: "Non-void function may reach end without returning a value. Ensure all execution paths have explicit return statements.".to_string(),
file_path: String::new(),
line: node.start_position().row + 1,
column: node.start_position().column + 1,
suggestion: Some(
"Add return statements to ensure all execution paths return a value".to_string()
),
..Default::default()
});
}
}
}
impl CertRule for Msc37C {
fn rule_id(&self) -> &'static str {
"MSC37-C"
}
fn description(&self) -> &'static str {
"Ensure that control never reaches the end of a non-void function"
}
fn severity(&self) -> Severity {
Severity::Medium
}
fn category(&self) -> RuleCategory {
RuleCategory::Rule
}
fn cert_id(&self) -> &'static str {
"MSC37-C"
}
fn scan(&self, node: &Node, source: &str, violations: &mut Vec<RuleViolation>) {
self.check_node(node, source, violations);
}
}
impl Msc37C {
fn check_node(&self, node: &Node, source: &str, violations: &mut Vec<RuleViolation>) {
let noreturn_names = Self::collect_noreturn_function_names(node, source);
for func in query::find_descendants_of_kind(*node, "function_definition") {
self.check_function_definition(&func, source, &noreturn_names, violations);
}
}
}