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 Fio14C;
impl CertRule for Fio14C {
fn rule_id(&self) -> &'static str {
"FIO14-C"
}
fn description(&self) -> &'static str {
"Understand the difference between text mode and binary mode with file streams"
}
fn severity(&self) -> Severity {
Severity::Medium
}
fn category(&self) -> RuleCategory {
RuleCategory::Rule
}
fn cert_id(&self) -> &'static str {
"FIO14-C"
}
fn check(&self, node: &Node, source: &str) -> Vec<RuleViolation> {
let mut violations = Vec::new();
self.check_node(node, source, &mut violations);
violations
}
}
impl Fio14C {
fn check_node(&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 == "fseek" {
self.check_fseek_call(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_fseek_call(&self, node: &Node, source: &str, violations: &mut Vec<RuleViolation>) {
if let Some(arguments) = node.child_by_field_name("arguments") {
let args = self.extract_arguments(&arguments, source);
if args.len() >= 3 {
let offset_text = get_node_text(&args[1], source);
let whence_text = get_node_text(&args[2], source);
if whence_text == "SEEK_END" {
let start_point = node.start_position();
violations.push(RuleViolation {
rule_id: self.rule_id().to_string(),
severity: Severity::Medium,
message: "fseek() with SEEK_END should not be used on binary streams (undefined behavior with null padding)".to_string(),
file_path: String::new(),
line: start_point.row + 1,
column: start_point.column + 1,
suggestion: Some(
"Use SEEK_SET or SEEK_CUR instead, or ensure stream is in text mode"
.to_string(),
),
..Default::default()
});
}
if whence_text == "SEEK_SET" && self.is_literal_nonzero_offset(&offset_text) {
let start_point = node.start_position();
violations.push(RuleViolation {
rule_id: self.rule_id().to_string(),
severity: Severity::Medium,
message: format!(
"fseek() with arbitrary offset '{}' and SEEK_SET may not work correctly in text mode (only 0 or ftell() values allowed)",
offset_text
),
file_path: String::new(),
line: start_point.row + 1,
column: start_point.column + 1,
suggestion: Some(
"Use binary mode for the file, or use offset from ftell()".to_string(),
),
..Default::default()
});
}
}
}
}
fn extract_arguments<'a>(&self, arguments_node: &Node<'a>, _source: &str) -> Vec<Node<'a>> {
let mut args = Vec::new();
for i in 0..arguments_node.child_count() {
if let Some(child) = arguments_node.child(i) {
if child.kind() != "," && child.kind() != "(" && child.kind() != ")" {
args.push(child);
}
}
}
args
}
fn is_literal_nonzero_offset(&self, offset_text: &str) -> bool {
if let Ok(value) = offset_text.trim().parse::<i64>() {
value != 0
} else {
false
}
}
}