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 Fio10C;
impl CertRule for Fio10C {
fn rule_id(&self) -> &'static str {
"FIO10-C"
}
fn description(&self) -> &'static str {
"Take care when using the rename() function"
}
fn severity(&self) -> Severity {
Severity::Medium
}
fn category(&self) -> RuleCategory {
RuleCategory::Recommendation
}
fn cert_id(&self) -> &'static str {
"FIO10-C"
}
fn check(&self, node: &Node, source: &str) -> Vec<RuleViolation> {
let mut violations = Vec::new();
self.find_unhandled_rename(node, source, &mut violations);
violations
}
}
impl Fio10C {
fn find_unhandled_rename(
&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 == "rename" {
if !self.is_properly_handled(node, source) {
violations.push(RuleViolation {
rule_id: self.rule_id().to_string(),
message: "rename() called without handling destination file existence. \
Behavior is implementation-defined (POSIX removes dest, Windows fails)."
.to_string(),
severity: self.severity(),
line: node.start_position().row + 1,
column: node.start_position().column + 1,
file_path: String::new(),
suggestion: Some(
"Either: (1) Call remove(dest) before rename(), OR \
(2) Check if dest exists with file_exists()/access()/stat() \
and handle accordingly"
.to_string(),
),
requires_manual_review: None,
});
}
}
}
}
for i in 0..node.child_count() {
if let Some(child) = node.child(i) {
self.find_unhandled_rename(&child, source, violations);
}
}
}
fn is_properly_handled(&self, rename_node: &Node, source: &str) -> bool {
if self.has_preceding_remove(rename_node, source) {
return true;
}
if self.is_wrapped_in_existence_check(rename_node, source) {
return true;
}
if self.has_preceding_existence_check_with_remove(rename_node, source) {
return true;
}
if self.has_return_value_check(rename_node) {
return true;
}
false
}
fn has_preceding_remove(&self, rename_node: &Node, source: &str) -> bool {
let mut current = rename_node.parent();
while let Some(parent) = current {
if parent.kind() == "expression_statement" || parent.kind() == "if_statement" {
if let Some(grandparent) = parent.parent() {
let parent_id = parent.id();
let mut found_parent = false;
for i in (0..grandparent.child_count()).rev() {
if let Some(sibling) = grandparent.child(i) {
if found_parent {
if self.contains_remove_call(&sibling, source) {
return true;
}
}
if sibling.id() == parent_id {
found_parent = true;
}
}
}
}
break;
}
current = parent.parent();
}
false
}
fn contains_remove_call(&self, node: &Node, source: &str) -> bool {
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 == "remove" || func_name == "unlink" {
return true;
}
}
}
for i in 0..node.child_count() {
if let Some(child) = node.child(i) {
if self.contains_remove_call(&child, source) {
return true;
}
}
}
false
}
fn is_wrapped_in_existence_check(&self, rename_node: &Node, source: &str) -> bool {
let mut current = rename_node.parent();
while let Some(node) = current {
if node.kind() == "if_statement" {
if let Some(condition) = node.child_by_field_name("condition") {
let condition_text = get_node_text(&condition, source);
if self.is_existence_check(&condition_text) {
return true;
}
}
}
current = node.parent();
}
false
}
fn has_preceding_existence_check_with_remove(&self, rename_node: &Node, source: &str) -> bool {
let mut current = rename_node.parent();
while let Some(parent) = current {
if parent.kind() == "expression_statement" || parent.kind() == "if_statement" {
if let Some(grandparent) = parent.parent() {
let parent_id = parent.id();
let mut found_parent = false;
for i in (0..grandparent.child_count()).rev() {
if let Some(sibling) = grandparent.child(i) {
if found_parent {
if sibling.kind() == "if_statement" {
if let Some(condition) =
sibling.child_by_field_name("condition")
{
let condition_text = get_node_text(&condition, source);
if self.is_existence_check(&condition_text) {
if self.contains_remove_call(&sibling, source) {
return true;
}
}
}
}
}
if sibling.id() == parent_id {
found_parent = true;
}
}
}
}
break;
}
current = parent.parent();
}
false
}
fn has_return_value_check(&self, rename_node: &Node) -> bool {
let mut current = rename_node.parent();
while let Some(node) = current {
match node.kind() {
"parenthesized_expression" | "binary_expression" | "unary_expression" => {
current = node.parent();
continue;
}
"if_statement" | "while_statement" => {
if let Some(condition) = node.child_by_field_name("condition") {
if rename_node.start_byte() >= condition.start_byte()
&& rename_node.end_byte() <= condition.end_byte()
{
return true;
}
}
return false;
}
"expression_statement" | "compound_statement" | "function_definition" => {
return false;
}
_ => {
current = node.parent();
continue;
}
}
}
false
}
fn is_existence_check(&self, text: &str) -> bool {
text.contains("file_exists")
|| text.contains("access")
|| text.contains("_access")
|| text.contains("stat")
|| text.contains("lstat")
|| text.contains("fstat")
|| text.contains("PathFileExists")
}
}