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 Str05C;
impl Str05C {
pub fn new() -> Self {
Self
}
fn check_declaration(&self, node: &Node, source: &str, violations: &mut Vec<RuleViolation>) {
if node.kind() != "declaration" {
return;
}
for i in 0..node.child_count() {
if let Some(child) = node.child(i) {
if child.kind() == "init_declarator" {
self.check_init_declarator(&child, node, source, violations);
}
}
}
}
fn check_init_declarator(
&self,
init_decl_node: &Node,
decl_node: &Node,
source: &str,
violations: &mut Vec<RuleViolation>,
) {
let declarator = match init_decl_node.child_by_field_name("declarator") {
Some(d) => d,
None => return,
};
let value = match init_decl_node.child_by_field_name("value") {
Some(v) => v,
None => return,
};
if !self.is_string_literal(&value) {
return;
}
if !self.is_pointer_declarator(&declarator) {
return;
}
if self.is_const_qualified_pointee(decl_node, source) {
return;
}
let var_name = self.get_declarator_identifier(&declarator, source);
violations.push(RuleViolation {
rule_id: self.rule_id().to_string(),
severity: self.severity(),
message: format!(
"Pointer variable '{}' is assigned a string literal without const qualification. String literals are immutable and should be declared as 'const char *' or 'const wchar_t *'.",
var_name
),
file_path: String::new(),
line: init_decl_node.start_position().row + 1,
column: init_decl_node.start_position().column + 1,
suggestion: Some(
"Add 'const' qualifier to the pointee type (e.g., change 'char *' to 'const char *')."
.to_string(),
),
..Default::default()
});
}
fn is_string_literal(&self, node: &Node) -> bool {
matches!(
node.kind(),
"string_literal" | "concatenated_string" | "L\"...\""
)
}
fn is_pointer_declarator(&self, declarator: &Node) -> bool {
if declarator.kind() == "pointer_declarator" {
return true;
}
for i in 0..declarator.child_count() {
if let Some(child) = declarator.child(i) {
if self.is_pointer_declarator(&child) {
return true;
}
}
}
false
}
fn is_const_qualified_pointee(&self, decl_node: &Node, source: &str) -> bool {
for i in 0..decl_node.child_count() {
if let Some(child) = decl_node.child(i) {
if child.kind() == "type_qualifier" {
let text = get_node_text(&child, source);
if text.contains("const") {
return true;
}
}
if child.kind() == "primitive_type" || child.kind() == "type_identifier" {
let text = get_node_text(&child, source);
if text.contains("const") {
return true;
}
}
}
}
false
}
fn get_declarator_identifier(&self, declarator: &Node, source: &str) -> String {
if declarator.kind() == "identifier" {
return get_node_text(declarator, source).to_string();
}
for i in 0..declarator.child_count() {
if let Some(child) = declarator.child(i) {
let result = self.get_declarator_identifier(&child, source);
if !result.is_empty() {
return result;
}
}
}
String::from("(unknown)")
}
}
impl CertRule for Str05C {
fn rule_id(&self) -> &'static str {
"STR05-C"
}
fn description(&self) -> &'static str {
"Use pointers to const when referring to string literals"
}
fn severity(&self) -> Severity {
Severity::Low
}
fn category(&self) -> RuleCategory {
RuleCategory::Rule
}
fn cert_id(&self) -> &'static str {
"STR05-C"
}
fn check(&self, node: &Node, source: &str) -> Vec<RuleViolation> {
let mut violations = Vec::new();
self.check_node(node, source, &mut violations);
violations
}
}
impl Str05C {
fn check_node(&self, node: &Node, source: &str, violations: &mut Vec<RuleViolation>) {
self.check_declaration(node, source, violations);
for i in 0..node.child_count() {
if let Some(child) = node.child(i) {
self.check_node(&child, source, violations);
}
}
}
}