1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
//! EXP16-C: Do not compare function pointers to constant values
//!
//! This rule detects comparisons of function pointers to constant values
//! (other than null pointers). This typically indicates programmer error
//! where the function was intended to be called but the parentheses were omitted.
//!
//! VIOLATIONS:
//! - if (getuid == 0) // Function identifier compared to constant
//! - if (geteuid != 0) // Function identifier compared to constant
//! - if (do_xyz) // Function identifier used as boolean (implicit != 0)
//!
//! COMPLIANT:
//! - if (getuid() == 0) // Function is called, result compared
//! - if (geteuid() != 0) // Function is called, result compared
//! - if (do_xyz()) // Function is called, result used as boolean
use std::collections::HashSet;
use super::super::{CertRule, RuleViolation};
use crate::manifest::{RuleCategory, Severity};
use tree_sitter::Node;
pub struct Exp16C;
impl CertRule for Exp16C {
fn rule_id(&self) -> &'static str {
"EXP16-C"
}
fn description(&self) -> &'static str {
"Do not compare function pointers to constant values"
}
fn severity(&self) -> Severity {
Severity::Low
}
fn category(&self) -> RuleCategory {
RuleCategory::Recommendation
}
fn cert_id(&self) -> &'static str {
"EXP16-C"
}
fn check(&self, node: &Node, source: &str) -> Vec<RuleViolation> {
let mut violations = Vec::new();
let function_names = self.collect_function_names(node, source);
self.check_node(node, source, &function_names, &mut violations);
violations
}
}
impl Exp16C {
/// Collect all function names defined or declared in this translation unit.
fn collect_function_names(&self, node: &Node, source: &str) -> HashSet<String> {
let mut names = HashSet::new();
self.collect_function_names_recursive(node, source, &mut names);
names
}
fn collect_function_names_recursive(
&self,
node: &Node,
source: &str,
names: &mut HashSet<String>,
) {
match node.kind() {
"function_definition" => {
if let Some(name) = self.extract_function_name(node, source) {
names.insert(name);
}
}
"declaration" => {
// Forward declarations: check if declarator contains a function_declarator
if let Some(declarator) = node.child_by_field_name("declarator") {
if self.has_function_declarator(&declarator) {
if let Some(name) = self.extract_declarator_name(&declarator, source) {
names.insert(name);
}
}
}
}
_ => {}
}
for i in 0..node.child_count() {
if let Some(child) = node.child(i) {
self.collect_function_names_recursive(&child, source, names);
}
}
}
fn extract_function_name(&self, func_def: &Node, source: &str) -> Option<String> {
let declarator = func_def.child_by_field_name("declarator")?;
self.extract_declarator_name(&declarator, source)
}
fn extract_declarator_name(&self, node: &Node, source: &str) -> Option<String> {
match node.kind() {
"identifier" => Some(node.utf8_text(source.as_bytes()).ok()?.to_string()),
"function_declarator" | "pointer_declarator" | "parenthesized_declarator" => {
let child = node.child_by_field_name("declarator")?;
self.extract_declarator_name(&child, source)
}
_ => {
// Walk children looking for an identifier
for i in 0..node.child_count() {
if let Some(child) = node.child(i) {
if let Some(name) = self.extract_declarator_name(&child, source) {
return Some(name);
}
}
}
None
}
}
}
fn has_function_declarator(&self, node: &Node) -> bool {
if node.kind() == "function_declarator" {
return true;
}
for i in 0..node.child_count() {
if let Some(child) = node.child(i) {
if self.has_function_declarator(&child) {
return true;
}
}
}
false
}
fn check_node(
&self,
node: &Node,
source: &str,
function_names: &HashSet<String>,
violations: &mut Vec<RuleViolation>,
) {
match node.kind() {
// Check binary expressions for comparisons
"binary_expression" => {
self.check_binary_expression(node, source, function_names, violations);
}
// Check for standalone comparison as expression statement (CWE-482)
// e.g., `x == 5;` where the result is discarded
"expression_statement" => {
self.check_dead_comparison(node, source, violations);
}
// Check if/while conditions for implicit boolean conversion
"if_statement" | "while_statement" => {
if let Some(condition) = node.child_by_field_name("condition") {
self.check_condition_for_implicit_conversion(
&condition,
source,
function_names,
violations,
);
}
}
// Check parenthesized expressions in conditions
"parenthesized_expression" => {
// Check if this is directly inside an if/while (the condition)
if let Some(parent) = node.parent() {
if parent.kind() == "if_statement" || parent.kind() == "while_statement" {
// Check the inner expression
for i in 0..node.child_count() {
if let Some(child) = node.child(i) {
if child.kind() == "identifier" {
self.check_identifier_as_condition(
&child,
source,
function_names,
violations,
);
}
}
}
}
}
}
_ => {}
}
// Recurse into children
for i in 0..node.child_count() {
if let Some(child) = node.child(i) {
self.check_node(&child, source, function_names, violations);
}
}
}
fn check_binary_expression(
&self,
node: &Node,
source: &str,
function_names: &HashSet<String>,
violations: &mut Vec<RuleViolation>,
) {
// Get the operator
let mut operator = None;
let mut left = None;
let mut right = None;
for i in 0..node.child_count() {
if let Some(child) = node.child(i) {
match child.kind() {
"==" | "!=" | "<" | ">" | "<=" | ">=" => {
operator = Some(child.kind());
}
_ => {
if left.is_none() {
left = Some(child);
} else if right.is_none() {
right = Some(child);
}
}
}
}
}
// Only check comparison operators
if let (Some(op), Some(left_node), Some(right_node)) = (operator, left, right) {
if op == "==" || op == "!=" || op == "<" || op == ">" || op == "<=" || op == ">=" {
// Check if left side is a function identifier and right is a constant
if self.is_function_identifier(&left_node, source, function_names)
&& self.is_constant(&right_node, source)
{
let func_name = left_node.utf8_text(source.as_bytes()).unwrap_or("unknown");
violations.push(RuleViolation {
rule_id: self.rule_id().to_string(),
severity: self.severity(),
line: node.start_position().row + 1,
column: node.start_position().column + 1,
file_path: String::new(),
message: format!(
"Function pointer '{}' compared to constant value; did you mean to call the function?",
func_name
),
suggestion: Some(format!(
"Use '{}()' to call the function instead of comparing its address",
func_name
)),
requires_manual_review: None,
});
}
// Check if right side is a function identifier and left is a constant
if self.is_function_identifier(&right_node, source, function_names)
&& self.is_constant(&left_node, source)
{
let func_name = right_node.utf8_text(source.as_bytes()).unwrap_or("unknown");
violations.push(RuleViolation {
rule_id: self.rule_id().to_string(),
severity: self.severity(),
line: node.start_position().row + 1,
column: node.start_position().column + 1,
file_path: String::new(),
message: format!(
"Function pointer '{}' compared to constant value; did you mean to call the function?",
func_name
),
suggestion: Some(format!(
"Use '{}()' to call the function instead of comparing its address",
func_name
)),
requires_manual_review: None,
});
}
}
}
}
/// Detect `x == value;` as a standalone expression statement (CWE-482).
/// The comparison result is discarded — likely meant `x = value;`.
fn check_dead_comparison(
&self,
node: &Node,
source: &str,
violations: &mut Vec<RuleViolation>,
) {
// expression_statement wraps one child expression + ";"
for i in 0..node.child_count() {
if let Some(child) = node.child(i) {
if child.kind() == "binary_expression" {
// Check if operator is == or !=
for j in 0..child.child_count() {
if let Some(op) = child.child(j) {
if op.kind() == "==" {
let expr_text =
child.utf8_text(source.as_bytes()).unwrap_or("unknown");
violations.push(RuleViolation {
rule_id: self.rule_id().to_string(),
severity: self.severity(),
line: child.start_position().row + 1,
column: child.start_position().column + 1,
file_path: String::new(),
message: format!(
"Comparison '{}' used as statement with result discarded; did you mean '=' (assignment)?",
expr_text
),
suggestion: Some(
"Use '=' for assignment instead of '==' for comparison"
.to_string(),
),
requires_manual_review: None,
});
return;
}
}
}
}
}
}
}
fn check_condition_for_implicit_conversion(
&self,
node: &Node,
source: &str,
function_names: &HashSet<String>,
violations: &mut Vec<RuleViolation>,
) {
// Check for parenthesized expression containing just an identifier
if node.kind() == "parenthesized_expression" {
for i in 0..node.child_count() {
if let Some(child) = node.child(i) {
if child.kind() == "identifier" {
self.check_identifier_as_condition(
&child,
source,
function_names,
violations,
);
}
}
}
}
}
fn check_identifier_as_condition(
&self,
node: &Node,
source: &str,
function_names: &HashSet<String>,
violations: &mut Vec<RuleViolation>,
) {
// An identifier used directly in a condition is implicitly compared to 0
if self.is_function_identifier(node, source, function_names) {
let func_name = node.utf8_text(source.as_bytes()).unwrap_or("unknown");
violations.push(RuleViolation {
rule_id: self.rule_id().to_string(),
severity: self.severity(),
line: node.start_position().row + 1,
column: node.start_position().column + 1,
file_path: String::new(),
message: format!(
"Function pointer '{}' used in boolean context; did you mean to call the function?",
func_name
),
suggestion: Some(format!(
"Use '{}()' to call the function instead of testing its address",
func_name
)),
requires_manual_review: None,
});
}
}
fn is_function_identifier(
&self,
node: &Node,
source: &str,
function_names: &HashSet<String>,
) -> bool {
if node.kind() != "identifier" {
return false;
}
let name = node.utf8_text(source.as_bytes()).unwrap_or("");
// If this identifier is being called (parent is call_expression), it's fine
if let Some(parent) = node.parent() {
if parent.kind() == "call_expression" {
if let Some(func) = parent.child_by_field_name("function") {
if func.id() == node.id() {
return false;
}
}
}
}
// Check against function names collected from the AST
if function_names.contains(name) {
return true;
}
// Fallback: well-known standard library functions
const KNOWN_FUNCTIONS: &[&str] = &[
"getuid", "geteuid", "getgid", "getegid", "getpid", "getppid", "fork", "exit", "main",
"printf", "scanf", "malloc", "free", "strlen", "strcpy", "strcat", "strcmp", "memcpy",
"memset", "fopen", "fclose", "fread", "fwrite", "rand", "srand",
];
KNOWN_FUNCTIONS.contains(&name)
}
fn is_constant(&self, node: &Node, source: &str) -> bool {
match node.kind() {
"number_literal" | "char_literal" | "true" | "false" | "null" => true,
"identifier" => {
let text = node.utf8_text(source.as_bytes()).unwrap_or("");
text == "NULL" || text == "nullptr"
}
_ => false,
}
}
}