sqc 0.4.13

Software Code Quality - CERT C compliance checker
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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
//! API07-C: Enforce type safety
//!
//! Detects two patterns:
//! 1. **strncpy without null-termination**: `strncpy()` does not guarantee
//!    null-termination of the destination buffer.
//! 2. **Free pointer not at start of buffer (CWE-761)**: `free(p)` where `p`
//!    was modified by pointer arithmetic after allocation. The pointer must
//!    point to the start of the allocated block when passed to `free()`.

use crate::manifest::{RuleCategory, Severity};
use crate::rules::{CertRule, RuleViolation};
use crate::utility::cert_c::ast_utils::get_node_text;
use std::collections::{HashMap, HashSet};
use tree_sitter::Node;

pub struct Api07C;

impl CertRule for Api07C {
    fn rule_id(&self) -> &'static str {
        "API07-C"
    }

    fn description(&self) -> &'static str {
        "Enforce type safety"
    }

    fn severity(&self) -> Severity {
        Severity::Medium
    }

    fn category(&self) -> RuleCategory {
        RuleCategory::Recommendation
    }

    fn cert_id(&self) -> &'static str {
        "API07-C"
    }

    fn check(&self, node: &Node, source: &str) -> Vec<RuleViolation> {
        let mut violations = Vec::new();
        self.check_node(node, source, &mut violations);
        violations
    }
}

impl Api07C {
    fn check_node(&self, node: &Node, source: &str, violations: &mut Vec<RuleViolation>) {
        if node.kind() == "call_expression" {
            self.check_strncpy_call(node, source, violations);
        }

        // Check each function for free-not-at-start and type confusion patterns
        if node.kind() == "function_definition" {
            self.check_free_not_at_start(node, source, violations);
            self.check_type_confusion(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_strncpy_call(&self, node: &Node, source: &str, violations: &mut Vec<RuleViolation>) {
        if let Some(function_node) = node.child_by_field_name("function") {
            let function_name = get_node_text(&function_node, source);
            if function_name == "strncpy" {
                let start_point = node.start_position();
                let call_text = get_node_text(node, source);
                violations.push(RuleViolation {
                    rule_id: self.rule_id().to_string(),
                    severity: Severity::Medium,
                    message: format!(
                        "Use of strncpy() '{}' does not guarantee null-termination, violating type safety",
                        call_text
                    ),
                    file_path: String::new(),
                    line: start_point.row + 1,
                    column: start_point.column + 1,
                    suggestion: Some(
                        "Use strncpy_s() (C11 Annex K) or manually null-terminate the destination buffer".to_string()
                    ),
                    ..Default::default()
                });
            }
        }
    }

    /// CWE-761: Detect free() on a pointer that was modified after allocation.
    ///
    /// Scans the function body text for:
    /// 1. Allocation: `var = malloc(...)` / `var = (T*)malloc(...)` / `var = calloc(...)`
    /// 2. Modification: `var++` / `var--` / `var +=` / `var -=` / `var = var +` / `var = var -`
    /// 3. Free: `free(var)` after modification
    fn check_free_not_at_start(
        &self,
        func_node: &Node,
        source: &str,
        violations: &mut Vec<RuleViolation>,
    ) {
        let func_text = get_node_text(func_node, source);
        let func_start_row = func_node.start_position().row;

        // Find all allocated pointer variables
        let allocated_vars = self.find_allocated_vars(&func_text);
        if allocated_vars.is_empty() {
            return;
        }

        // For each allocated var, check if it's modified then freed
        for var in &allocated_vars {
            let mut modified = false;

            for (line_idx, line) in func_text.lines().enumerate() {
                let trimmed = line.trim();

                // Check for reassignment back to allocation (resets modification tracking)
                if self.is_allocation_of(trimmed, var) {
                    modified = false;
                    continue;
                }

                // Check for pointer modification
                if !modified && self.is_pointer_modification(trimmed, var) {
                    modified = true;
                }

                // Check for free() on modified pointer
                if modified && self.is_free_of(trimmed, var) {
                    violations.push(RuleViolation {
                        rule_id: self.rule_id().to_string(),
                        severity: Severity::High,
                        message: format!(
                            "Calling free({}) after pointer arithmetic. The pointer may not point to the start of the allocated block.",
                            var
                        ),
                        file_path: String::new(),
                        line: func_start_row + line_idx + 1,
                        column: 1,
                        suggestion: Some(
                            "Save the original pointer before modifying it, and pass the original to free().".to_string()
                        ),
                        ..Default::default()
                    });
                    break; // One violation per var per function
                }

                // Reset if var is reassigned to something else entirely
                if modified && self.is_reassignment_of(trimmed, var) {
                    modified = false;
                }
            }
        }
    }

    /// CWE-843: Detect type confusion through void* pointers.
    ///
    /// Pattern: void* data = &smallVar; ... *((int*)data)
    /// where smallVar is a smaller type (char, short) than the cast target (int).
    fn check_type_confusion(
        &self,
        func_node: &Node,
        source: &str,
        violations: &mut Vec<RuleViolation>,
    ) {
        let func_text = get_node_text(func_node, source);
        let func_start_row = func_node.start_position().row;

        // Phase 1: Find void* variable declarations
        let mut void_ptr_vars: HashSet<String> = HashSet::new();
        // Maps variable name → declared type (e.g., "charBuffer" → "char")
        let mut var_types: HashMap<String, String> = HashMap::new();
        // Maps void* var → source type it was assigned from
        let mut void_ptr_source_type: HashMap<String, String> = HashMap::new();

        for line in func_text.lines() {
            let trimmed = line.trim();

            // Detect void* declarations: "void * data" or "void *data"
            if (trimmed.contains("void *") || trimmed.contains("void*"))
                && !trimmed.starts_with("//")
                && !trimmed.starts_with("/*")
            {
                // Extract the variable name after void * / void*
                if let Some(name) = Self::extract_void_ptr_var(trimmed) {
                    void_ptr_vars.insert(name);
                }
            }

            // Track typed variable declarations for type lookup
            for type_name in &["char", "short", "int", "long", "float", "double"] {
                if let Some(var_name) = Self::extract_typed_var(trimmed, type_name) {
                    var_types.insert(var_name, type_name.to_string());
                }
            }
        }

        if void_ptr_vars.is_empty() {
            return;
        }

        // Phase 2: Track assignments and detect confusion
        for (line_idx, line) in func_text.lines().enumerate() {
            let trimmed = line.trim();

            // Track assignments: data = &varName
            for vp in &void_ptr_vars {
                let assign_pat = format!("{} = &", vp);
                if let Some(pos) = trimmed.find(&assign_pat) {
                    let after = &trimmed[pos + assign_pat.len()..];
                    let var_name: String = after
                        .chars()
                        .take_while(|c| c.is_alphanumeric() || *c == '_')
                        .collect();
                    if let Some(src_type) = var_types.get(&var_name) {
                        void_ptr_source_type.insert(vp.clone(), src_type.clone());
                    }
                }
            }

            // Detect dereference with cast: *((int*)data) or *((long*)data)
            for vp in &void_ptr_vars {
                if let Some(src_type) = void_ptr_source_type.get(vp) {
                    if let Some(cast_type) = Self::find_cast_deref(trimmed, vp) {
                        if Self::type_size(&cast_type) > Self::type_size(src_type) {
                            violations.push(RuleViolation {
                                rule_id: self.rule_id().to_string(),
                                severity: Severity::High,
                                message: format!(
                                    "Type confusion: void* '{}' points to {} but is dereferenced as {}*",
                                    vp, src_type, cast_type
                                ),
                                file_path: String::new(),
                                line: func_start_row + line_idx + 1,
                                column: 1,
                                suggestion: Some(
                                    "Ensure the cast type matches the actual pointed-to type"
                                        .to_string(),
                                ),
                                ..Default::default()
                            });
                            break;
                        }
                    }
                }
            }
        }
    }

    fn extract_void_ptr_var(line: &str) -> Option<String> {
        // Match patterns: "void * name", "void *name", "void * name ="
        let patterns = ["void * ", "void *"];
        for pat in &patterns {
            if let Some(pos) = line.find(pat) {
                let after = line[pos + pat.len()..].trim_start();
                let name: String = after
                    .chars()
                    .take_while(|c| c.is_alphanumeric() || *c == '_')
                    .collect();
                if !name.is_empty() && name.chars().next().is_some_and(|c| c.is_alphabetic()) {
                    return Some(name);
                }
            }
        }
        None
    }

    fn extract_typed_var(line: &str, type_name: &str) -> Option<String> {
        // Match: "char varName" or "char varName =" or "char varName;"
        // Avoid matching inside other tokens (e.g., "wchar_t")
        let pat = format!("{} ", type_name);
        if let Some(pos) = line.find(&pat) {
            // Make sure it's at a word boundary (start of line, after space, or after type qualifier)
            if pos > 0 {
                let prev = line.as_bytes()[pos - 1];
                if prev.is_ascii_alphanumeric() || prev == b'_' {
                    return None;
                }
            }
            let after = line[pos + pat.len()..].trim_start();
            let name: String = after
                .chars()
                .take_while(|c| c.is_alphanumeric() || *c == '_')
                .collect();
            if !name.is_empty()
                && name.chars().next().is_some_and(|c| c.is_alphabetic())
                && !matches!(
                    name.as_str(),
                    "const" | "volatile" | "static" | "extern" | "register"
                )
            {
                return Some(name);
            }
        }
        None
    }

    /// Find a cast-dereference pattern like *((int*)var) or *((long *)var)
    fn find_cast_deref(line: &str, var_name: &str) -> Option<String> {
        // Look for patterns: (TYPE*)var or (TYPE *)var followed by ) and preceded by *(
        let var_close = format!("{})", var_name);
        if !line.contains(&var_close) {
            return None;
        }

        // Find cast type: look for (TYPE*) or (TYPE *) before var_name
        let search = format!("*){}", var_name);
        if let Some(pos) = line.find(&search) {
            // Walk backwards from pos to find the opening ( of the cast
            let before = &line[..pos];
            if let Some(paren_pos) = before.rfind('(') {
                let cast_content = before[paren_pos + 1..].trim();
                // Extract type name (everything before the *)
                let type_name: String = cast_content.trim_end_matches('*').trim().to_string();
                if !type_name.is_empty() {
                    return Some(type_name);
                }
            }
        }

        // Also handle (TYPE *) with space before *
        let search2 = format!(" *){}", var_name);
        if let Some(pos) = line.find(&search2) {
            let before = &line[..pos];
            if let Some(paren_pos) = before.rfind('(') {
                let type_name = before[paren_pos + 1..].trim().to_string();
                if !type_name.is_empty() {
                    return Some(type_name);
                }
            }
        }

        None
    }

    fn type_size(type_name: &str) -> usize {
        match type_name {
            "char" | "signed char" | "unsigned char" => 1,
            "short" | "signed short" | "unsigned short" => 2,
            "int" | "signed int" | "unsigned int" | "signed" | "unsigned" => 4,
            "long" | "signed long" | "unsigned long" => 8,
            "float" => 4,
            "double" => 8,
            _ => 0, // Unknown type — don't flag
        }
    }

    fn find_allocated_vars(&self, func_text: &str) -> HashSet<String> {
        let mut vars = HashSet::new();
        for line in func_text.lines() {
            let trimmed = line.trim();
            // Match: var = malloc(...), var = (T*)malloc(...), var = calloc(...)
            // Also match: var = (T *)realloc(...)
            if let Some(var) = self.extract_alloc_target(trimmed) {
                vars.insert(var);
            }
        }
        vars
    }

    fn extract_alloc_target(&self, line: &str) -> Option<String> {
        // Look for pattern: IDENT = ... malloc/calloc/realloc(
        let eq_pos = line.find('=')?;
        let before_eq = line[..eq_pos].trim();
        let after_eq = line[eq_pos + 1..].trim();

        // Skip ==, !=, <=, >=
        if line.len() > eq_pos + 1 {
            let next = line.as_bytes().get(eq_pos + 1)?;
            if *next == b'=' {
                return None;
            }
        }
        if eq_pos > 0 && matches!(line.as_bytes()[eq_pos - 1], b'!' | b'<' | b'>') {
            return None;
        }

        // Check if RHS contains an allocation call
        if !after_eq.contains("malloc(")
            && !after_eq.contains("calloc(")
            && !after_eq.contains("realloc(")
        {
            return None;
        }

        // Extract the variable name (last token before =)
        let var_name = before_eq.split_whitespace().last()?.trim_start_matches('*');

        if var_name.chars().all(|c| c.is_alphanumeric() || c == '_')
            && !var_name.is_empty()
            && var_name.chars().next()?.is_alphabetic()
        {
            Some(var_name.to_string())
        } else {
            None
        }
    }

    fn is_allocation_of(&self, line: &str, var: &str) -> bool {
        // var = ...malloc/calloc/realloc(
        if let Some(eq_pos) = line.find('=') {
            if eq_pos > 0 && line.as_bytes()[eq_pos - 1] != b'!' {
                let before = line[..eq_pos].trim();
                let after = line[eq_pos + 1..].trim();
                let last_token = before.split_whitespace().last().unwrap_or("");
                let last_token = last_token.trim_start_matches('*');
                if last_token == var
                    && (after.contains("malloc(")
                        || after.contains("calloc(")
                        || after.contains("realloc("))
                {
                    return true;
                }
            }
        }
        false
    }

    fn is_pointer_modification(&self, line: &str, var: &str) -> bool {
        // var++ or var--
        if line.contains(&format!("{}++", var)) || line.contains(&format!("{}--", var)) {
            return true;
        }
        // ++var or --var
        if line.contains(&format!("++{}", var)) || line.contains(&format!("--{}", var)) {
            return true;
        }
        // var += or var -=
        if line.contains(&format!("{} +=", var)) || line.contains(&format!("{} -=", var)) {
            return true;
        }
        // for(...; ...; var++) pattern
        if line.starts_with("for")
            && (line.contains(&format!("{}++", var)) || line.contains(&format!("{}--", var)))
        {
            return true;
        }
        false
    }

    fn is_free_of(&self, line: &str, var: &str) -> bool {
        line.contains(&format!("free({})", var)) || line.contains(&format!("free( {} )", var))
    }

    fn is_reassignment_of(&self, line: &str, var: &str) -> bool {
        // var = something (but not var == or var +=/-=)
        let pattern = format!("{} = ", var);
        if let Some(pos) = line.find(&pattern) {
            let after = &line[pos + pattern.len()..];
            // Not var = var + N (that's still modification, not reset)
            if !after.trim_start().starts_with(var) {
                return true;
            }
        }
        false
    }
}