sqc 0.4.84

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
use super::super::{CertRule, RuleViolation};
use crate::manifest::{RuleCategory, Severity};
use crate::utility::cert_c::ast_utils;
use std::collections::HashMap;
use tree_sitter::Node;

pub struct Dcl01C;

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

    fn description(&self) -> &'static str {
        "Do not reuse variable names in subscopes"
    }

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

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

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

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

        // Collect all variable declarations at this level and check for shadowing
        check_scope_for_shadowing(node, source, &mut violations, self.rule_id());

        violations
    }
}

type Scope = HashMap<String, (usize, usize)>;

/// Check scopes for variable name shadowing, starting at `root`.
///
/// Uses an explicit work stack instead of recursion: an else-if chain nests
/// an `if_statement` as the `alternative` of the previous one, so a
/// generated/obfuscated chain thousands deep would blow the native call
/// stack if each link were a recursive call (the same hostap-style shape
/// that motivated task 153).
///
/// Each stack entry pairs a node with the scope of variable names visible
/// from its enclosing scopes (name -> declaration location); this mirrors
/// the `outer_vars` parameter the recursive version threaded through calls.
fn check_scope_for_shadowing(
    root: &Node,
    source: &str,
    violations: &mut Vec<RuleViolation>,
    rule_id: &str,
) {
    let mut stack: Vec<(Node, Scope)> = vec![(*root, Scope::new())];

    while let Some((node, outer_vars)) = stack.pop() {
        // Build a new scope with variables declared at this level
        let mut current_scope = outer_vars.clone();

        match node.kind() {
            "translation_unit" => check_translation_unit_shadowing(&node, source, &mut stack),
            "function_definition" => check_function_definition_shadowing(
                &node,
                source,
                &outer_vars,
                &mut current_scope,
                violations,
                rule_id,
                &mut stack,
            ),
            "compound_statement" => check_compound_statement_shadowing(
                &node,
                source,
                &outer_vars,
                &mut current_scope,
                violations,
                rule_id,
                &mut stack,
            ),
            "for_statement" => check_for_statement_shadowing(
                &node,
                source,
                &outer_vars,
                &current_scope,
                violations,
                rule_id,
                &mut stack,
            ),
            // While/do-while loop - check body
            "while_statement" | "do_statement" => {
                if let Some(body) = node.child_by_field_name("body") {
                    stack.push((body, current_scope));
                }
            }
            "if_statement" => check_if_statement_shadowing(&node, &current_scope, &mut stack),
            // Switch statement - check body
            "switch_statement" => {
                if let Some(body) = node.child_by_field_name("body") {
                    stack.push((body, current_scope));
                }
            }
            _ => {
                // For other nodes, queue children for processing
                for i in 0..node.child_count() {
                    if let Some(child) = node.child(i) {
                        stack.push((child, current_scope.clone()));
                    }
                }
            }
        }
    }
}

/// `translation_unit` case of [`check_scope_for_shadowing`]: file scope
/// collects all global declarations, then queues each function body against
/// that global scope.
fn check_translation_unit_shadowing<'a>(
    node: &Node<'a>,
    source: &str,
    stack: &mut Vec<(Node<'a>, Scope)>,
) {
    let mut global_vars = HashMap::new();
    collect_declarations_in_node(node, source, &mut global_vars);

    for i in 0..node.child_count() {
        if let Some(child) = node.child(i) {
            if child.kind() == "function_definition" {
                stack.push((child, global_vars.clone()));
            }
        }
    }
}

/// `function_definition` case of [`check_scope_for_shadowing`]: function
/// scope collects parameters (flagging any that shadow an outer variable),
/// then queues the function body.
#[allow(clippy::too_many_arguments)]
fn check_function_definition_shadowing<'a>(
    node: &Node<'a>,
    source: &str,
    outer_vars: &HashMap<String, (usize, usize)>,
    current_scope: &mut HashMap<String, (usize, usize)>,
    violations: &mut Vec<RuleViolation>,
    rule_id: &str,
    stack: &mut Vec<(Node<'a>, Scope)>,
) {
    let params = extract_function_parameters(node, source);
    for (param_name, line, col) in params {
        if let Some((outer_line, outer_col)) = outer_vars.get(&param_name) {
            violations.push(RuleViolation {
                rule_id: rule_id.to_string(),
                severity: Severity::Low,
                message: format!(
                    "Function parameter '{}' shadows variable from outer scope (line {}:{})",
                    param_name, outer_line, outer_col
                ),
                file_path: String::new(),
                line,
                column: col,
                suggestion: Some(format!(
                    "Rename parameter '{}' to avoid shadowing outer scope variable",
                    param_name
                )),
                ..Default::default()
            });
        }
        current_scope.insert(param_name, (line, col));
    }

    if let Some(body) = find_compound_statement(node) {
        stack.push((body, current_scope.clone()));
    }
}

/// `compound_statement` case of [`check_scope_for_shadowing`]: block scope
/// collects declarations in this block (flagging any that shadow an outer
/// variable), then queues nested scopes.
#[allow(clippy::too_many_arguments)]
fn check_compound_statement_shadowing<'a>(
    node: &Node<'a>,
    source: &str,
    outer_vars: &HashMap<String, (usize, usize)>,
    current_scope: &mut HashMap<String, (usize, usize)>,
    violations: &mut Vec<RuleViolation>,
    rule_id: &str,
    stack: &mut Vec<(Node<'a>, Scope)>,
) {
    let mut block_vars = HashMap::new();

    for i in 0..node.child_count() {
        let Some(child) = node.child(i) else { continue };
        if child.kind() != "declaration" {
            continue;
        }
        let decls = extract_declarations(&child, source);
        for (var_name, line, col) in decls {
            // Check if this shadows an outer variable
            if let Some((outer_line, outer_col)) = outer_vars.get(&var_name) {
                violations.push(RuleViolation {
                    rule_id: rule_id.to_string(),
                    severity: Severity::Low,
                    message: format!(
                        "Variable '{}' shadows variable from outer scope (line {}:{})",
                        var_name, outer_line, outer_col
                    ),
                    file_path: String::new(),
                    line,
                    column: col,
                    suggestion: Some(format!("Rename variable '{}' to avoid shadowing", var_name)),
                    ..Default::default()
                });
            }
            block_vars.insert(var_name, (line, col));
        }
    }

    // Merge block variables into current scope
    current_scope.extend(block_vars);

    // Queue nested scopes, in reverse so they're popped in original
    // left-to-right document order.
    for i in (0..node.child_count()).rev() {
        let Some(child) = node.child(i) else { continue };
        if matches!(
            child.kind(),
            "compound_statement"
                | "for_statement"
                | "while_statement"
                | "do_statement"
                | "if_statement"
                | "switch_statement"
        ) {
            stack.push((child, current_scope.clone()));
        }
    }
}

/// `for_statement` case of [`check_scope_for_shadowing`]: checks the
/// initializer for a loop-variable declaration that shadows an outer
/// variable, then queues the loop body.
#[allow(clippy::too_many_arguments)]
fn check_for_statement_shadowing<'a>(
    node: &Node<'a>,
    source: &str,
    outer_vars: &HashMap<String, (usize, usize)>,
    current_scope: &HashMap<String, (usize, usize)>,
    violations: &mut Vec<RuleViolation>,
    rule_id: &str,
    stack: &mut Vec<(Node<'a>, Scope)>,
) {
    let mut loop_scope = current_scope.clone();

    if let Some(init) = node.child_by_field_name("initializer") {
        if init.kind() == "declaration" {
            let decls = extract_declarations(&init, source);
            for (var_name, line, col) in decls {
                if let Some((outer_line, outer_col)) = outer_vars.get(&var_name) {
                    violations.push(RuleViolation {
                        rule_id: rule_id.to_string(),
                        severity: Severity::Low,
                        message: format!(
                            "Loop variable '{}' shadows variable from outer scope (line {}:{})",
                            var_name, outer_line, outer_col
                        ),
                        file_path: String::new(),
                        line,
                        column: col,
                        suggestion: Some(format!(
                            "Rename loop variable '{}' to avoid shadowing",
                            var_name
                        )),
                        ..Default::default()
                    });
                }
                loop_scope.insert(var_name, (line, col));
            }
        }
    }

    if let Some(body) = node.child_by_field_name("body") {
        stack.push((body, loop_scope));
    }
}

/// `if_statement` case of [`check_scope_for_shadowing`]: queues the
/// consequence and (if present) alternative branches. The alternative of an
/// `else if` is itself an `if_statement`, so a long else-if chain is
/// consumed one work-stack entry at a time rather than nesting a native
/// call per link.
fn check_if_statement_shadowing<'a>(
    node: &Node<'a>,
    current_scope: &HashMap<String, (usize, usize)>,
    stack: &mut Vec<(Node<'a>, Scope)>,
) {
    // Push alternative first so consequence (which appears first in source)
    // is popped and processed first.
    if let Some(alternative) = node.child_by_field_name("alternative") {
        stack.push((alternative, current_scope.clone()));
    }
    if let Some(consequence) = node.child_by_field_name("consequence") {
        stack.push((consequence, current_scope.clone()));
    }
}

/// Collect all variable declarations in a node (non-recursive)
fn collect_declarations_in_node(
    node: &Node,
    source: &str,
    vars: &mut HashMap<String, (usize, usize)>,
) {
    for i in 0..node.child_count() {
        if let Some(child) = node.child(i) {
            if child.kind() == "declaration" {
                let decls = extract_declarations(&child, source);
                for (var_name, line, col) in decls {
                    vars.insert(var_name, (line, col));
                }
            }
        }
    }
}

/// Extract variable names from a declaration node
///
/// Returns: Vec<(variable_name, line_number, column_number)>
fn extract_declarations(decl_node: &Node, source: &str) -> Vec<(String, usize, usize)> {
    let mut declarations = Vec::new();

    // Look for declarators in the declaration
    for i in 0..decl_node.child_count() {
        if let Some(child) = decl_node.child(i) {
            match child.kind() {
                "init_declarator" => {
                    if let Some(declarator) = child.child_by_field_name("declarator") {
                        let var_name =
                            ast_utils::get_identifier_from_declarator(&declarator, source);
                        if !var_name.is_empty() {
                            let pos = declarator.start_position();
                            declarations.push((var_name, pos.row + 1, pos.column + 1));
                        }
                    }
                }
                "pointer_declarator" | "array_declarator" | "identifier" => {
                    let var_name = ast_utils::get_identifier_from_declarator(&child, source);
                    if !var_name.is_empty() {
                        let pos = child.start_position();
                        declarations.push((var_name, pos.row + 1, pos.column + 1));
                    }
                }
                _ => {}
            }
        }
    }

    declarations
}

/// Extract function parameters with their positions
///
/// Returns: Vec<(parameter_name, line_number, column_number)>
fn extract_function_parameters(func_node: &Node, source: &str) -> Vec<(String, usize, usize)> {
    let mut parameters = Vec::new();

    // Find the function_declarator
    for i in 0..func_node.child_count() {
        if let Some(child) = func_node.child(i) {
            if child.kind() == "function_declarator" {
                // Find parameter_list
                for j in 0..child.child_count() {
                    if let Some(param_list) = child.child(j) {
                        if param_list.kind() == "parameter_list" {
                            // Extract each parameter
                            for k in 0..param_list.child_count() {
                                if let Some(param) = param_list.child(k) {
                                    if param.kind() == "parameter_declaration" {
                                        // Find the declarator in the parameter
                                        for m in 0..param.child_count() {
                                            if let Some(declarator) = param.child(m) {
                                                if matches!(
                                                    declarator.kind(),
                                                    "identifier"
                                                        | "pointer_declarator"
                                                        | "array_declarator"
                                                        | "function_declarator"
                                                ) {
                                                    let param_name =
                                                        ast_utils::get_identifier_from_declarator(
                                                            &declarator,
                                                            source,
                                                        );
                                                    if !param_name.is_empty() {
                                                        let pos = declarator.start_position();
                                                        parameters.push((
                                                            param_name,
                                                            pos.row + 1,
                                                            pos.column + 1,
                                                        ));
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }

    parameters
}

/// Find the compound_statement (body) of a function
fn find_compound_statement<'a>(func_node: &Node<'a>) -> Option<Node<'a>> {
    for i in 0..func_node.child_count() {
        if let Some(child) = func_node.child(i) {
            if child.kind() == "compound_statement" {
                return Some(child);
            }
        }
    }
    None
}