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
459
460
461
462
463
//! FLP34-C: Ensure that floating-point conversions are within range of the new type
//!
//! This rule detects unchecked floating-point type conversions that can result in
//! undefined behavior when the value is outside the range of the target type.
//!
//! VIOLATIONS:
//! - i_a = f_a;                  // Float to int without range checking
//! - f_a = (float)d_a;           // Double to float cast without range checking
//! - f_b = (float)big_d;         // Long double to float cast without range checking
//!
//! COMPLIANT:
//! - if (isnan(f_a) || check_range) { /* handle */ } i_a = f_a;
//! - if (isnan(d_a) || isgreater(fabs(d_a), FLT_MAX)) { /* handle */ } f_a = (float)d_a;

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 Flp34C;

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

    fn description(&self) -> &'static str {
        "Ensure that floating-point conversions are within range of the new type"
    }

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

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

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

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

impl Flp34C {
    fn check_recursive(&self, node: &Node, source: &str, violations: &mut Vec<RuleViolation>) {
        // When we enter a function definition, collect variable types and check within it
        if node.kind() == "function_definition" {
            let type_map = self.collect_variable_types(node, source);
            self.check_function_body(node, source, &type_map, violations);
            return; // Don't recurse further; check_function_body handles children
        }

        // For cast expressions at file scope (unlikely but possible), still check
        if node.kind() == "cast_expression" {
            if let Some(violation) = self.check_cast_expression(node, source) {
                violations.push(violation);
            }
        }

        // Recurse into children
        for i in 0..node.child_count() {
            if let Some(child) = node.child(i) {
                self.check_recursive(&child, source, violations);
            }
        }
    }

    /// Check all nodes within a function body, using the collected type map for assignments
    fn check_function_body(
        &self,
        node: &Node,
        source: &str,
        type_map: &HashMap<String, String>,
        violations: &mut Vec<RuleViolation>,
    ) {
        if node.kind() == "cast_expression" {
            if let Some(violation) = self.check_cast_expression(node, source) {
                violations.push(violation);
            }
        }

        if node.kind() == "assignment_expression" {
            if let Some(violation) = self.check_assignment_conversion(node, source, type_map) {
                violations.push(violation);
            }
        }

        for i in 0..node.child_count() {
            if let Some(child) = node.child(i) {
                self.check_function_body(&child, source, type_map, violations);
            }
        }
    }

    /// Collect variable types from function parameters and local declarations.
    /// Returns a map from variable name to its base type string.
    fn collect_variable_types<'a>(
        &self,
        func_node: &Node<'a>,
        source: &'a str,
    ) -> HashMap<String, String> {
        let mut type_map = HashMap::new();

        // Collect from function parameters
        if let Some(declarator) = func_node.child_by_field_name("declarator") {
            self.collect_params_from_declarator(&declarator, source, &mut type_map);
        }

        // Collect from local declarations in the function body
        if let Some(body) = func_node.child_by_field_name("body") {
            self.collect_local_declarations(&body, source, &mut type_map);
        }

        type_map
    }

    /// Extract parameter types from a function declarator
    fn collect_params_from_declarator(
        &self,
        node: &Node,
        source: &str,
        type_map: &mut HashMap<String, String>,
    ) {
        if node.kind() == "function_declarator" {
            if let Some(params) = node.child_by_field_name("parameters") {
                for i in 0..params.child_count() {
                    if let Some(param) = params.child(i) {
                        if param.kind() == "parameter_declaration" {
                            self.extract_type_and_name(&param, source, type_map);
                        }
                    }
                }
            }
        }
        // Recurse to find nested function_declarator (e.g. pointer declarators)
        for i in 0..node.child_count() {
            if let Some(child) = node.child(i) {
                self.collect_params_from_declarator(&child, source, type_map);
            }
        }
    }

    /// Collect local variable declarations from a compound_statement
    fn collect_local_declarations(
        &self,
        node: &Node,
        source: &str,
        type_map: &mut HashMap<String, String>,
    ) {
        if node.kind() == "declaration" {
            self.extract_type_and_name(node, source, type_map);
        }

        for i in 0..node.child_count() {
            if let Some(child) = node.child(i) {
                self.collect_local_declarations(&child, source, type_map);
            }
        }
    }

    /// Extract the type specifier text and variable name from a declaration or parameter_declaration
    fn extract_type_and_name(
        &self,
        node: &Node,
        source: &str,
        type_map: &mut HashMap<String, String>,
    ) {
        let mut type_text = String::new();

        // Collect the type from children
        for i in 0..node.child_count() {
            if let Some(child) = node.child(i) {
                match child.kind() {
                    "primitive_type" | "sized_type_specifier" | "type_identifier" => {
                        type_text = ast_utils::get_node_text(&child, source).to_string();
                    }
                    _ => {}
                }
            }
        }

        if type_text.is_empty() {
            return;
        }

        // Extract variable names from declarators
        if let Some(declarator) = node.child_by_field_name("declarator") {
            if let Some(name) = self.extract_identifier_name(&declarator, source) {
                type_map.insert(name, type_text.clone());
            }
        }

        // Also handle init_declarator lists (e.g. `int a, b;`)
        for i in 0..node.child_count() {
            if let Some(child) = node.child(i) {
                if child.kind() == "init_declarator" {
                    if let Some(decl) = child.child_by_field_name("declarator") {
                        if let Some(name) = self.extract_identifier_name(&decl, source) {
                            type_map.insert(name, type_text.clone());
                        }
                    }
                }
            }
        }
    }

    /// Extract the identifier name from a declarator, unwrapping pointer/array declarators
    fn extract_identifier_name(&self, node: &Node, source: &str) -> Option<String> {
        match node.kind() {
            "identifier" => Some(ast_utils::get_node_text(node, source).to_string()),
            "pointer_declarator" | "array_declarator" | "parenthesized_declarator" => {
                if let Some(inner) = node.child_by_field_name("declarator") {
                    self.extract_identifier_name(&inner, source)
                } else {
                    None
                }
            }
            _ => {
                // Search children for identifier
                for i in 0..node.child_count() {
                    if let Some(child) = node.child(i) {
                        if child.kind() == "identifier" {
                            return Some(ast_utils::get_node_text(&child, source).to_string());
                        }
                    }
                }
                None
            }
        }
    }

    /// Check if a cast expression converts floating-point types unsafely
    fn check_cast_expression(&self, cast_node: &Node, source: &str) -> Option<RuleViolation> {
        // Get the target type
        let type_node = cast_node.child_by_field_name("type")?;
        let target_type = ast_utils::get_node_text(&type_node, source);

        // Get the value being cast
        let _value_node = cast_node.child_by_field_name("value")?;

        // Check if this is a narrowing floating-point conversion
        if !self.is_narrowing_fp_conversion(&target_type) {
            return None;
        }

        // Check if there's range checking before this cast
        if self.has_range_checking(cast_node, source) {
            return None;
        }

        let start_point = cast_node.start_position();

        Some(RuleViolation {
            rule_id: "FLP34-C".to_string(),
            severity: Severity::Medium,
            message: format!(
                "Floating-point conversion to '{}' without range checking",
                target_type
            ),
            file_path: String::new(),
            line: start_point.row + 1,
            column: start_point.column + 1,
            suggestion: Some(
                "Check for isnan(), compare with FLT_MAX/FLT_MIN or DBL_MAX/DBL_MIN before conversion".to_string()
            ),
            ..Default::default()
        })
    }

    /// Check if an assignment involves unchecked floating-point conversion using type information
    fn check_assignment_conversion(
        &self,
        assignment_node: &Node,
        source: &str,
        type_map: &HashMap<String, String>,
    ) -> Option<RuleViolation> {
        let left = assignment_node.child_by_field_name("left")?;
        let right = assignment_node.child_by_field_name("right")?;

        // Get variable names
        let left_name = ast_utils::get_node_text(&left, source);
        let right_name = ast_utils::get_node_text(&right, source);

        // Only check simple identifier-to-identifier assignments
        // Skip if right side contains function calls, casts, or complex expressions
        if right.kind() != "identifier" {
            return None;
        }

        // Look up types for both sides
        let left_type = type_map.get(left_name)?;
        let right_type = type_map.get(right_name)?;

        // Check if this is a dangerous floating-point conversion
        if !self.is_dangerous_assignment(left_type, right_type) {
            return None;
        }

        // Check if there's range checking before this assignment
        if self.has_range_checking(assignment_node, source) {
            return None;
        }

        let start_point = assignment_node.start_position();

        Some(RuleViolation {
            rule_id: "FLP34-C".to_string(),
            severity: Severity::Medium,
            message: format!(
                "Floating-point conversion from '{}' to '{}' without range checking",
                right_type, left_type
            ),
            file_path: String::new(),
            line: start_point.row + 1,
            column: start_point.column + 1,
            suggestion: Some(
                "Check for isnan(), verify value is within target type's range before conversion"
                    .to_string(),
            ),
            ..Default::default()
        })
    }

    /// Check if assigning right_type to left_type is a dangerous float conversion
    fn is_dangerous_assignment(&self, left_type: &str, right_type: &str) -> bool {
        let left_fp = Self::fp_rank(left_type);
        let right_fp = Self::fp_rank(right_type);
        let left_int = Self::is_integer_type(left_type);

        // Float/double/long double assigned to integer type
        if right_fp > 0 && left_int {
            return true;
        }

        // Narrowing floating-point: higher rank to lower rank
        // double -> float, long double -> float, long double -> double
        if right_fp > left_fp && left_fp > 0 {
            return true;
        }

        false
    }

    /// Return floating-point "rank": 0 = not FP, 1 = float, 2 = double, 3 = long double
    fn fp_rank(type_text: &str) -> u8 {
        let t = type_text.trim();
        if t == "long double" {
            3
        } else if t == "double" {
            2
        } else if t == "float" {
            1
        } else {
            0
        }
    }

    /// Check if a type is an integer type
    fn is_integer_type(type_text: &str) -> bool {
        let t = type_text.trim();
        matches!(
            t,
            "int"
                | "short"
                | "long"
                | "long long"
                | "char"
                | "signed char"
                | "unsigned char"
                | "unsigned int"
                | "unsigned short"
                | "unsigned long"
                | "unsigned long long"
                | "signed"
                | "unsigned"
                | "int8_t"
                | "int16_t"
                | "int32_t"
                | "int64_t"
                | "uint8_t"
                | "uint16_t"
                | "uint32_t"
                | "uint64_t"
                | "size_t"
                | "ssize_t"
                | "ptrdiff_t"
                | "intptr_t"
                | "uintptr_t"
        )
    }

    /// Check if target type is a narrowing floating-point conversion
    fn is_narrowing_fp_conversion(&self, target_type: &str) -> bool {
        // Narrowing conversions: long double -> double/float, double -> float
        target_type.contains("float") && !target_type.contains("long")
            || target_type.contains("double") && !target_type.contains("long")
    }

    /// Check if there's range checking around the conversion
    fn has_range_checking(&self, node: &Node, source: &str) -> bool {
        // Find the containing function body
        let function_body = self.get_containing_function_body(node);
        let body = match function_body {
            Some(b) => b,
            None => return false,
        };

        let body_text = ast_utils::get_node_text(&body, source);

        // Look for range checking patterns
        if body_text.contains("isnan") {
            return true;
        }

        if body_text.contains("isgreater") || body_text.contains("isless") {
            return true;
        }

        if body_text.contains("FLT_MAX") || body_text.contains("FLT_MIN") {
            return true;
        }

        if body_text.contains("DBL_MAX") || body_text.contains("DBL_MIN") {
            return true;
        }

        if body_text.contains("INT_MAX") || body_text.contains("INT_MIN") {
            return true;
        }

        if body_text.contains("log2f") || body_text.contains("fabsf") || body_text.contains("fabs")
        {
            return true;
        }

        false
    }

    /// Get the containing function body
    fn get_containing_function_body<'a>(&self, node: &Node<'a>) -> Option<Node<'a>> {
        let mut current = node.parent();

        while let Some(n) = current {
            if n.kind() == "compound_statement" {
                if let Some(parent) = n.parent() {
                    if parent.kind() == "function_definition" {
                        return Some(n);
                    }
                }
            }
            current = n.parent();
        }

        None
    }
}