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 Env34C;
impl CertRule for Env34C {
fn rule_id(&self) -> &'static str {
"ENV34-C"
}
fn description(&self) -> &'static str {
"Do not store pointers returned by certain functions"
}
fn severity(&self) -> Severity {
Severity::Medium
}
fn category(&self) -> RuleCategory {
RuleCategory::Rule
}
fn cert_id(&self) -> &'static str {
"ENV34-C"
}
fn check(&self, node: &Node, source: &str) -> Vec<RuleViolation> {
let mut violations = Vec::new();
self.check_node(node, source, &mut violations);
violations
}
}
impl Env34C {
fn check_node(&self, node: &Node, source: &str, violations: &mut Vec<RuleViolation>) {
if node.kind() == "assignment_expression" {
self.check_assignment(node, source, violations);
} else if node.kind() == "init_declarator" {
self.check_init_declarator(node, source, violations);
}
for i in 0..node.child_count() {
if let Some(child) = node.child(i) {
self.check_node(&child, source, violations);
}
}
}
fn check_assignment(&self, node: &Node, source: &str, violations: &mut Vec<RuleViolation>) {
if let Some(right) = node.child_by_field_name("right") {
if self.is_affected_function_call(&right, source) {
if let Some(left) = node.child_by_field_name("left") {
if self.is_const_variable_assignment(&left, source) {
return;
}
}
let func_name = self.get_function_name_from_call(&right, source);
violations.push(RuleViolation {
rule_id: self.rule_id().to_string(),
message: format!(
"Storing pointer returned by '{}' is prohibited. The data referenced \
may be overwritten by subsequent calls. Use strdup(), malloc()/strcpy(), \
or consume the value immediately.",
func_name
),
severity: self.severity(),
line: node.start_position().row + 1,
column: node.start_position().column + 1,
file_path: String::new(),
suggestion: Some(format!(
"Instead of 'ptr = {}()', use 'ptr = strdup({}())' and remember to free() later, \
or use 'const char *ptr' for immediate use only",
func_name, func_name
)),
requires_manual_review: None,
});
}
}
}
fn is_const_variable_assignment(&self, left_node: &Node, source: &str) -> bool {
let var_name = get_node_text(left_node, source);
matches!(var_name, "temp" | "tmp" | "ptr" | "p")
}
fn check_init_declarator(
&self,
node: &Node,
source: &str,
violations: &mut Vec<RuleViolation>,
) {
let is_const = self.is_const_pointer_declarator(node, source);
if is_const {
return;
}
for i in 0..node.child_count() {
if let Some(child) = node.child(i) {
if child.kind() == "=" {
if let Some(init) = node.child(i + 1) {
if self.is_affected_function_call(&init, source) {
let func_name = self.get_function_name_from_call(&init, source);
violations.push(RuleViolation {
rule_id: self.rule_id().to_string(),
message: format!(
"Storing pointer returned by '{}' in non-const pointer is prohibited. \
The data referenced may be overwritten by subsequent calls.",
func_name
),
severity: self.severity(),
line: node.start_position().row + 1,
column: node.start_position().column + 1,
file_path: String::new(),
suggestion: Some(format!(
"Instead of 'char *var = {}()', use 'char *var = strdup({}())' \
and free() later, or use 'const char *var' for immediate use only",
func_name, func_name
)),
requires_manual_review: None,
});
}
}
}
}
}
}
fn is_const_pointer_declarator(&self, node: &Node, source: &str) -> bool {
if let Some(parent) = node.parent() {
if parent.kind() == "declaration" {
for i in 0..parent.child_count() {
if let Some(child) = parent.child(i) {
if child.kind() == "type_qualifier" {
let text = get_node_text(&child, source);
if text == "const" {
return true;
}
}
}
}
}
}
false
}
fn is_affected_function_call(&self, node: &Node, source: &str) -> bool {
if node.kind() != "call_expression" {
return false;
}
if let Some(function) = node.child_by_field_name("function") {
let func_name = get_node_text(&function, source);
self.is_affected_function(&func_name)
} else {
false
}
}
fn get_function_name_from_call(&self, node: &Node, source: &str) -> String {
if node.kind() == "call_expression" {
if let Some(function) = node.child_by_field_name("function") {
return get_node_text(&function, source).to_string();
}
}
String::from("unknown")
}
fn is_affected_function(&self, name: &str) -> bool {
matches!(
name,
"getenv" | "asctime" | "localeconv" | "setlocale" | "strerror"
)
}
}