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 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 {
if node.kind() == "return_statement" {
return true;
}
for i in 0..node.child_count() {
if let Some(child) = node.child(i) {
if self.has_return_statement(&child) {
return true;
}
}
}
false
}
fn ends_with_return(&self, compound_stmt: &Node) -> bool {
let mut last_stmt = None;
for i in 0..compound_stmt.child_count() {
if let Some(child) = compound_stmt.child(i) {
let kind = child.kind();
if kind == "{" || kind == "}" || kind == "comment" || kind.starts_with("preproc_") {
continue;
}
last_stmt = Some(child);
}
}
if let Some(stmt) = last_stmt {
match stmt.kind() {
"return_statement" => true,
"if_statement" | "switch_statement" => {
self.all_branches_return(&stmt)
}
"compound_statement" => self.ends_with_return(&stmt),
_ => false,
}
} else {
false
}
}
fn all_branches_return(&self, node: &Node) -> bool {
match node.kind() {
"if_statement" => {
if let Some(consequence) = node.child_by_field_name("consequence") {
let consequence_returns = self.statement_returns(&consequence);
if let Some(alternative) = node.child_by_field_name("alternative") {
let alternative_returns = self.statement_returns(&alternative);
return consequence_returns && alternative_returns;
}
}
false
}
"switch_statement" => {
self.has_return_statement(node)
}
_ => false,
}
}
fn statement_returns(&self, stmt: &Node) -> bool {
match stmt.kind() {
"return_statement" => true,
"compound_statement" => self.ends_with_return(stmt),
"if_statement" | "switch_statement" => self.all_branches_return(stmt),
"else_clause" => {
for i in 0..stmt.child_count() {
if let Some(child) = stmt.child(i) {
if child.kind() != "else" {
return self.statement_returns(&child);
}
}
}
false
}
_ => false,
}
}
fn check_function_definition(
&self,
node: &Node,
source: &str,
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) {
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 check(&self, node: &Node, source: &str) -> Vec<RuleViolation> {
let mut violations = Vec::new();
self.check_node(node, source, &mut violations);
violations
}
}
impl Msc37C {
fn check_node(&self, node: &Node, source: &str, violations: &mut Vec<RuleViolation>) {
self.check_function_definition(node, source, violations);
for i in 0..node.child_count() {
if let Some(child) = node.child(i) {
self.check_node(&child, source, violations);
}
}
}
}