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
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
//! EXP39-C: Do not access a variable through a pointer of an incompatible type
//!
//! This rule detects pointer casts to incompatible types, which can lead to undefined
//! behavior. Accessing an object through a pointer of an incompatible type violates
//! the strict aliasing rules and can cause unpredictable results.
//!
//! ## Examples:
//!
//! **Non-compliant:**
//! ```c
//! void f(void) {
//! float f = 0.0f;
//! int *ip = (int *)&f; // Incompatible type cast
//! (*ip)++; // Undefined behavior
//! }
//! ```
//!
//! **Non-compliant (array dimension mismatch):**
//! ```c
//! void func(void) {
//! int a[10][15];
//! int (*b)[10] = a; // Wrong second dimension
//! }
//! ```
//!
//! **Compliant:**
//! ```c
//! #include <math.h>
//! void f(void) {
//! float f = 0.0f;
//! f = nextafterf(f, FLT_MAX); // Use standard library functions
//! }
//! ```
//!
//! ## Detection Strategy:
//! - Find cast_expression nodes that cast pointers
//! - Extract source and target pointer types
//! - Check if types are incompatible (e.g., float* to int*, short* to int*)
//! - Flag violations for known incompatible type combinations
//! - Exceptions: char/unsigned char types are allowed (strict aliasing exception)
use super::super::{CertRule, RuleViolation};
use crate::manifest::{RuleCategory, Severity};
use crate::utility::cert_c::ast_utils::get_node_text;
use std::collections::{HashMap, HashSet};
use tree_sitter::Node;
pub struct Exp39C;
/// Collected variable type information
#[derive(Debug, Clone)]
struct VarTypeInfo {
base_type: String,
is_array: bool,
array_dimensions: Vec<String>, // e.g., ["ROWS", "COLS"] for int a[ROWS][COLS]
}
impl CertRule for Exp39C {
fn rule_id(&self) -> &'static str {
"EXP39-C"
}
fn description(&self) -> &'static str {
"Do not access a variable through a pointer of an incompatible type"
}
fn severity(&self) -> Severity {
Severity::Medium
}
fn category(&self) -> RuleCategory {
RuleCategory::Rule
}
fn cert_id(&self) -> &'static str {
"EXP39-C"
}
fn check(&self, node: &Node, source: &str) -> Vec<RuleViolation> {
let mut violations = Vec::new();
// First pass: collect variable type information and layout tracking
let mut var_types = HashMap::new();
let mut struct_field_ptrs = HashSet::new();
let mut union_vars = HashSet::new();
self.collect_var_types(
node,
source,
&mut var_types,
&mut struct_field_ptrs,
&mut union_vars,
);
// Second pass: check for violations and track union member accesses
let mut union_member_accesses: HashMap<String, Vec<(String, usize, usize, bool)>> =
HashMap::new();
self.check_node(
node,
source,
&mut violations,
&var_types,
&struct_field_ptrs,
&union_vars,
&mut union_member_accesses,
);
// Post-pass: check union type punning (CWE-188)
self.check_union_type_punning(&union_vars, &union_member_accesses, &mut violations);
violations
}
}
impl Exp39C {
/// Collect variable type information from declarations
fn collect_var_types(
&self,
node: &Node,
source: &str,
var_types: &mut HashMap<String, VarTypeInfo>,
struct_field_ptrs: &mut HashSet<String>,
union_vars: &mut HashSet<String>,
) {
if node.kind() == "declaration" {
self.process_declaration(node, source, var_types);
self.check_union_declaration(node, source, union_vars);
}
// Track struct field pointer assignments: ptr = &struct.field
if node.kind() == "assignment_expression" {
self.check_struct_field_ptr_assignment(node, source, struct_field_ptrs);
}
// Track struct field pointer initializations: type *ptr = &struct.field
if node.kind() == "init_declarator" {
self.check_struct_field_ptr_init(node, source, struct_field_ptrs);
}
// Recurse into children
for i in 0..node.child_count() {
if let Some(child) = node.child(i) {
self.collect_var_types(&child, source, var_types, struct_field_ptrs, union_vars);
}
}
}
/// Process a declaration to extract variable types
fn process_declaration(
&self,
node: &Node,
source: &str,
var_types: &mut HashMap<String, VarTypeInfo>,
) {
// Get the type specifier
let mut base_type = String::new();
for i in 0..node.child_count() {
if let Some(child) = node.child(i) {
match child.kind() {
"primitive_type" | "sized_type_specifier" | "type_identifier" => {
base_type = get_node_text(&child, source).trim().to_string();
break;
}
"struct_specifier" => {
// Handle struct declarations like "struct gadget *gp;"
base_type = get_node_text(&child, source).trim().to_string();
break;
}
_ => {}
}
}
}
if base_type.is_empty() {
return;
}
// Get the declarator(s) - handle pointer_declarator for struct pointers
for i in 0..node.child_count() {
if let Some(child) = node.child(i) {
match child.kind() {
"init_declarator" | "array_declarator" => {
self.extract_var_from_declarator(&child, source, &base_type, var_types);
}
"pointer_declarator" => {
// Handle "struct gadget *gp;"
if let Some(var_name) = self.find_var_name(&child, source) {
var_types.insert(
var_name,
VarTypeInfo {
base_type: base_type.clone(),
is_array: false,
array_dimensions: Vec::new(),
},
);
}
}
"identifier" => {
let var_name = get_node_text(&child, source).trim().to_string();
var_types.insert(
var_name,
VarTypeInfo {
base_type: base_type.clone(),
is_array: false,
array_dimensions: Vec::new(),
},
);
}
_ => {}
}
}
}
}
/// Extract variable name and type from a declarator
fn extract_var_from_declarator(
&self,
node: &Node,
source: &str,
base_type: &str,
var_types: &mut HashMap<String, VarTypeInfo>,
) {
let node_text = get_node_text(node, source);
let is_array = node.kind() == "array_declarator" || node_text.contains('[');
// Extract array dimensions
let array_dimensions = self.extract_array_dimensions(node, source);
// Find the identifier
if let Some(var_name) = self.find_var_name(node, source) {
var_types.insert(
var_name,
VarTypeInfo {
base_type: base_type.to_string(),
is_array,
array_dimensions,
},
);
}
}
/// Extract array dimensions from a declarator (e.g., [ROWS][COLS])
/// Returns dimensions in declaration order: for int a[ROWS][COLS], returns ["ROWS", "COLS"]
fn extract_array_dimensions(&self, node: &Node, source: &str) -> Vec<String> {
let mut dimensions = Vec::new();
self.collect_array_dimensions_recursive(node, source, &mut dimensions);
// Tree-sitter parses array declarators from outer to inner in the AST,
// but we want them in source order (first dimension first).
// Since we push as we recurse, the vector is in reverse source order.
// Reverse it to get proper source order: [ROWS][COLS] -> ["ROWS", "COLS"]
dimensions.reverse();
dimensions
}
/// Recursively collect array dimensions
fn collect_array_dimensions_recursive(
&self,
node: &Node,
source: &str,
dimensions: &mut Vec<String>,
) {
if node.kind() == "array_declarator" {
// Get the size expression (last child that's not '[' or ']')
if let Some(size_node) = node.child_by_field_name("size") {
let size_text = get_node_text(&size_node, source).trim().to_string();
dimensions.push(size_text);
}
// Recurse into nested array declarators
if let Some(declarator) = node.child_by_field_name("declarator") {
self.collect_array_dimensions_recursive(&declarator, source, dimensions);
}
}
// Check children for nested array declarators
for i in 0..node.child_count() {
if let Some(child) = node.child(i) {
if child.kind() == "array_declarator" {
self.collect_array_dimensions_recursive(&child, source, dimensions);
}
}
}
}
/// Find variable name in a declarator
fn find_var_name(&self, node: &Node, source: &str) -> Option<String> {
if node.kind() == "identifier" {
return Some(get_node_text(node, source).trim().to_string());
}
// Check named fields
if let Some(declarator) = node.child_by_field_name("declarator") {
return self.find_var_name(&declarator, source);
}
// Recurse into children
for i in 0..node.child_count() {
if let Some(child) = node.child(i) {
if let Some(name) = self.find_var_name(&child, source) {
return Some(name);
}
}
}
None
}
fn check_node(
&self,
node: &Node,
source: &str,
violations: &mut Vec<RuleViolation>,
var_types: &HashMap<String, VarTypeInfo>,
struct_field_ptrs: &HashSet<String>,
union_vars: &HashSet<String>,
union_member_accesses: &mut HashMap<String, Vec<(String, usize, usize, bool)>>,
) {
// Look for cast_expression nodes
if node.kind() == "cast_expression" {
self.check_cast_expression(node, source, violations, var_types);
self.check_struct_field_ptr_arithmetic(node, source, struct_field_ptrs, violations);
}
// Check for realloc with struct type casting
if node.kind() == "assignment_expression" {
self.check_realloc_cast(node, source, violations, var_types);
}
// Also check pointer_declarator assignments with incompatible array types
if node.kind() == "init_declarator" {
self.check_init_declarator(node, source, violations, var_types);
}
// Track union member accesses for CWE-188 type punning detection
if node.kind() == "field_expression" {
self.track_union_member_access(node, source, union_vars, union_member_accesses);
}
// Recursively check child nodes
for i in 0..node.child_count() {
if let Some(child) = node.child(i) {
self.check_node(
&child,
source,
violations,
var_types,
struct_field_ptrs,
union_vars,
union_member_accesses,
);
}
}
}
/// Check for realloc casts from one struct type to another
fn check_realloc_cast(
&self,
node: &Node,
source: &str,
violations: &mut Vec<RuleViolation>,
var_types: &HashMap<String, VarTypeInfo>,
) {
if let Some(right) = node.child_by_field_name("right") {
// Check if right side is a cast expression containing realloc
if right.kind() == "cast_expression" {
let right_text = get_node_text(&right, source);
if right_text.contains("realloc") {
// Get the cast target type
if let Some(type_node) = right.child_by_field_name("type") {
let target_type = get_node_text(&type_node, source).trim().to_string();
// Check if casting to a struct pointer
if target_type.contains("struct") && target_type.contains('*') {
// Get the variable being assigned to
if let Some(left) = node.child_by_field_name("left") {
let var_name = get_node_text(&left, source).trim().to_string();
// Check if there's a memset after this realloc in the same source
// If memset is used to reinitialize, it's compliant
let node_end = node.end_byte();
let source_after = &source[node_end..];
let has_memset = source_after
.contains(&format!("memset({}", var_name))
|| source_after.contains(&format!("memset( {}", var_name));
if !has_memset {
// Also check the realloc argument type
if let Some(value) = right.child_by_field_name("value") {
self.check_realloc_argument_type(
&value,
source,
&target_type,
var_types,
violations,
node,
);
}
}
}
}
}
}
}
}
}
/// Check if realloc is called with mismatched pointer type
fn check_realloc_argument_type(
&self,
node: &Node,
source: &str,
target_type: &str,
var_types: &HashMap<String, VarTypeInfo>,
violations: &mut Vec<RuleViolation>,
violation_node: &Node,
) {
if node.kind() == "call_expression" {
if let Some(func) = node.child_by_field_name("function") {
let func_name = get_node_text(&func, source).trim();
if func_name == "realloc" {
// Get first argument (the pointer being reallocated)
if let Some(args) = node.child_by_field_name("arguments") {
for i in 0..args.child_count() {
if let Some(arg) = args.child(i) {
if arg.kind() != "(" && arg.kind() != ")" && arg.kind() != "," {
let arg_name = get_node_text(&arg, source).trim().to_string();
if let Some(arg_type) = var_types.get(&arg_name) {
// Check if source and target struct types differ
if arg_type.base_type.contains("struct")
&& target_type.contains("struct")
{
let target_struct =
self.extract_struct_name(target_type);
let source_struct =
self.extract_struct_name(&arg_type.base_type);
if target_struct != source_struct {
self.report_struct_cast_violation(
violation_node,
source,
&target_struct,
&source_struct,
violations,
);
}
}
}
break; // Only check first argument
}
}
}
}
}
}
}
}
/// Extract struct name from a type string
fn extract_struct_name(&self, type_str: &str) -> String {
// Extract "widget" from "struct widget *" or "struct widget"
let cleaned = type_str
.replace('*', "")
.replace("struct", "")
.trim()
.to_string();
cleaned
}
/// Report violation for casting between different struct types
fn report_struct_cast_violation(
&self,
node: &Node,
source: &str,
target_struct: &str,
source_struct: &str,
violations: &mut Vec<RuleViolation>,
) {
let start_point = node.start_position();
let stmt_text = get_node_text(node, source).trim().to_string();
violations.push(RuleViolation {
rule_id: self.rule_id().to_string(),
severity: Severity::Medium,
message: format!(
"Accessing memory allocated as 'struct {}' through pointer to incompatible 'struct {}': {}",
source_struct,
target_struct,
if stmt_text.len() > 50 {
format!("{}...", &stmt_text[..50])
} else {
stmt_text
}
),
file_path: String::new(),
line: start_point.row + 1,
column: start_point.column + 1,
suggestion: Some(
"When using realloc to resize memory, ensure the data is properly reinitialized for the new type. Do not access struct members as if they contain valid data from a different struct type.".to_string()
),
..Default::default()
});
}
fn check_cast_expression(
&self,
node: &Node,
source: &str,
violations: &mut Vec<RuleViolation>,
var_types: &HashMap<String, VarTypeInfo>,
) {
// Get the type being cast to
if let Some(type_node) = node.child_by_field_name("type") {
let target_type = get_node_text(&type_node, source).trim().to_string();
// Get the value being cast
if let Some(value_node) = node.child_by_field_name("value") {
// Check if this is a pointer cast
if target_type.contains('*') {
// Extract base types
let target_base = self.extract_base_type(&target_type);
let source_base = self.infer_source_type(&value_node, source, var_types);
if let (Some(target), Some(source_type)) = (target_base, source_base) {
if self.are_incompatible_pointer_types(&target, &source_type) {
self.report_incompatible_cast_violation(
node,
source,
&target,
&source_type,
violations,
);
}
}
}
}
}
}
fn check_init_declarator(
&self,
node: &Node,
source: &str,
violations: &mut Vec<RuleViolation>,
var_types: &HashMap<String, VarTypeInfo>,
) {
// Check for array pointer type mismatches like: int (*b)[ROWS] = a;
if let Some(declarator) = node.child_by_field_name("declarator") {
if let Some(value) = node.child_by_field_name("value") {
let declarator_text = get_node_text(&declarator, source);
let value_text = get_node_text(&value, source).trim().to_string();
// Check for pointer to array declarations: (*var)[size]
if declarator_text.contains("(*") && declarator_text.contains('[') {
// Extract the dimension from the pointer-to-array declaration
// e.g., (*b)[ROWS] should extract "ROWS"
if let Some(ptr_dimension) =
self.extract_pointer_array_dimension(&declarator_text)
{
// Check if value is an array variable
if let Some(var_info) = var_types.get(&value_text) {
if var_info.is_array && !var_info.array_dimensions.is_empty() {
// For int a[ROWS][COLS], the second dimension should match
// int (*b)[COLS] = a; is valid (pointer to array of COLS ints)
// int (*b)[ROWS] = a; is invalid (wrong dimension)
// The pointer-to-array dimension should match the LAST dimension
// of the source array (the inner-most array dimension)
let last_dimension = var_info.array_dimensions.last();
if let Some(last_dim) = last_dimension {
if *last_dim != ptr_dimension {
self.report_array_dimension_violation(
node, source, violations,
);
}
}
}
}
}
}
}
}
}
/// Extract the dimension from a pointer-to-array declaration like "(*b)[ROWS]"
fn extract_pointer_array_dimension(&self, decl_text: &str) -> Option<String> {
// Find the last [...] in the declarator
if let Some(bracket_start) = decl_text.rfind('[') {
if let Some(bracket_end) = decl_text.rfind(']') {
if bracket_start < bracket_end {
let dimension = decl_text[bracket_start + 1..bracket_end].trim().to_string();
if !dimension.is_empty() {
return Some(dimension);
}
}
}
}
None
}
fn extract_base_type(&self, type_str: &str) -> Option<String> {
// Remove pointer asterisks, const, volatile, etc.
let cleaned = type_str
.replace("const", "")
.replace("volatile", "")
.replace("restrict", "")
.replace('*', "")
.replace(['(', ')'], "")
.trim()
.to_string();
if cleaned.is_empty() {
None
} else {
Some(cleaned)
}
}
fn infer_source_type(
&self,
node: &Node,
source: &str,
var_types: &HashMap<String, VarTypeInfo>,
) -> Option<String> {
// Try to infer the type from the expression
match node.kind() {
"pointer_expression" => {
// Address-of operator: &variable
if let Some(argument) = node.child_by_field_name("argument") {
let arg_text = get_node_text(&argument, source).trim().to_string();
// Check if we have type info for this variable
if let Some(var_info) = var_types.get(&arg_text) {
return Some(var_info.base_type.clone());
}
// This is simplified - in reality we'd need type information
// For now, check common patterns
if arg_text.contains("float") {
return Some("float".to_string());
} else if arg_text.contains("double") {
return Some("double".to_string());
}
// Try to infer from the variable name (heuristic)
return self.infer_type_from_name(&arg_text);
}
}
"identifier" => {
let name = get_node_text(node, source).trim().to_string();
// Check if we have type info for this variable
if let Some(var_info) = var_types.get(&name) {
return Some(var_info.base_type.clone());
}
return self.infer_type_from_name(&name);
}
"cast_expression" => {
// Nested cast - get the target type of the inner cast
if let Some(type_node) = node.child_by_field_name("type") {
let inner_type = get_node_text(&type_node, source).trim().to_string();
return self.extract_base_type(&inner_type);
}
}
_ => {}
}
None
}
fn infer_type_from_name(&self, name: &str) -> Option<String> {
// Heuristic: try to infer type from variable naming conventions
let lower_name = name.to_lowercase();
if lower_name.starts_with('f') || lower_name.contains("float") {
Some("float".to_string())
} else if lower_name.starts_with("db") || lower_name.contains("double") {
Some("double".to_string())
} else if lower_name.starts_with("ch") || lower_name.contains("char") {
Some("char".to_string())
} else if lower_name.starts_with("sh") || lower_name.contains("short") {
Some("short".to_string())
} else if lower_name.starts_with('l') && lower_name.contains("long") {
Some("long".to_string())
} else {
// Default to int for most cases
Some("int".to_string())
}
}
fn are_incompatible_pointer_types(&self, target_type: &str, source_type: &str) -> bool {
// Check if the two pointer types are incompatible
// Same types are compatible
if target_type == source_type {
return false;
}
// Character types can alias anything (exception in strict aliasing rules)
if target_type == "char" || target_type == "unsigned char" || target_type == "signed char" {
return false;
}
if source_type == "char" || source_type == "unsigned char" || source_type == "signed char" {
return false;
}
// void* is compatible with any pointer type
if target_type == "void" || source_type == "void" {
return false;
}
// Signed/unsigned variants of the same type are compatible
let target_normalized = target_type.replace("unsigned ", "").replace("signed ", "");
let source_normalized = source_type.replace("unsigned ", "").replace("signed ", "");
if target_normalized == source_normalized {
return false;
}
// Different integer types with different sizes are incompatible
// This catches short* -> int*, int* -> short*, etc.
let incompatible_pairs = [
("float", "int"),
("int", "float"),
("double", "int"),
("int", "double"),
("float", "short"),
("short", "float"),
("double", "long"),
("long", "double"),
("float", "long"),
("long", "float"),
("short", "int"),
("int", "short"),
("short", "long"),
("long", "short"),
];
for (type1, type2) in &incompatible_pairs {
if (target_type.contains(type1) && source_type.contains(type2))
|| (target_type.contains(type2) && source_type.contains(type1))
{
return true;
}
}
false
}
#[allow(dead_code)]
fn has_array_dimension_mismatch(&self, declarator: &str, _value: &str) -> bool {
// Simplified check for array dimension mismatches
// This would need more sophisticated parsing in a complete implementation
declarator.contains("(*") && declarator.contains("][")
}
fn report_incompatible_cast_violation(
&self,
node: &Node,
source: &str,
target_type: &str,
source_type: &str,
violations: &mut Vec<RuleViolation>,
) {
let start_point = node.start_position();
let cast_text = get_node_text(node, source).trim().to_string();
violations.push(RuleViolation {
rule_id: self.rule_id().to_string(),
severity: Severity::Medium,
message: format!(
"Do not access a '{}' object through a pointer of incompatible type '{}*': {}",
source_type,
target_type,
if cast_text.len() > 50 {
format!("{}...", &cast_text[..50])
} else {
cast_text
}
),
file_path: String::new(),
line: start_point.row + 1,
column: start_point.column + 1,
suggestion: Some(
"Avoid casting between incompatible pointer types. Use type-appropriate operations or unions for legitimate type punning. Consider using standard library functions instead of direct type manipulation.".to_string()
),
..Default::default()
});
}
fn report_array_dimension_violation(
&self,
node: &Node,
source: &str,
violations: &mut Vec<RuleViolation>,
) {
let start_point = node.start_position();
let decl_text = get_node_text(node, source).trim().to_string();
violations.push(RuleViolation {
rule_id: self.rule_id().to_string(),
severity: Severity::Medium,
message: format!(
"Array pointer dimension mismatch detected: {}",
if decl_text.len() > 60 {
format!("{}...", &decl_text[..60])
} else {
decl_text
}
),
file_path: String::new(),
line: start_point.row + 1,
column: start_point.column + 1,
suggestion: Some(
"Ensure array pointer dimensions match the array being assigned. Incompatible array types can lead to undefined behavior.".to_string()
),
..Default::default()
});
}
// ── CWE-188: Struct memory layout assumption detection ──────────────────
/// Check if a declaration is a union type and track the variable name
fn check_union_declaration(&self, node: &Node, source: &str, union_vars: &mut HashSet<String>) {
let mut has_union = false;
for i in 0..node.child_count() {
if let Some(child) = node.child(i) {
if child.kind() == "union_specifier" {
has_union = true;
break;
}
}
}
if !has_union {
return;
}
// Extract variable name from direct children (not from union body)
for i in 0..node.child_count() {
if let Some(child) = node.child(i) {
match child.kind() {
"identifier" => {
let name = get_node_text(&child, source).trim().to_string();
union_vars.insert(name);
}
"init_declarator" | "pointer_declarator" => {
if let Some(name) = self.find_var_name(&child, source) {
union_vars.insert(name);
}
}
_ => {}
}
}
}
}
/// Check if an assignment stores &struct.field into a variable
fn check_struct_field_ptr_assignment(
&self,
node: &Node,
source: &str,
struct_field_ptrs: &mut HashSet<String>,
) {
if let Some(left) = node.child_by_field_name("left") {
if left.kind() == "identifier" {
if let Some(right) = node.child_by_field_name("right") {
if self.is_address_of_struct_field(&right, source) {
let var_name = get_node_text(&left, source).trim().to_string();
struct_field_ptrs.insert(var_name);
}
}
}
}
}
/// Check if an init_declarator initializes with &struct.field
fn check_struct_field_ptr_init(
&self,
node: &Node,
source: &str,
struct_field_ptrs: &mut HashSet<String>,
) {
if let Some(value) = node.child_by_field_name("value") {
if self.is_address_of_struct_field(&value, source) {
if let Some(var_name) = self.find_var_name(node, source) {
struct_field_ptrs.insert(var_name);
}
}
}
}
/// Check if a node is &struct_var.field (address-of a struct field)
fn is_address_of_struct_field(&self, node: &Node, source: &str) -> bool {
if node.kind() != "pointer_expression" {
return false;
}
let text = get_node_text(node, source).trim();
if !text.starts_with('&') {
return false;
}
if let Some(arg) = node.child_by_field_name("argument") {
return arg.kind() == "field_expression";
}
false
}
/// Detect cast expressions involving struct field pointer arithmetic (CWE-188 modify_local)
/// Pattern: *(int*)(charPtr + sizeof(int)) where charPtr = &struct.field
fn check_struct_field_ptr_arithmetic(
&self,
cast_node: &Node,
source: &str,
struct_field_ptrs: &HashSet<String>,
violations: &mut Vec<RuleViolation>,
) {
if struct_field_ptrs.is_empty() {
return;
}
// Must be a pointer cast
if let Some(type_node) = cast_node.child_by_field_name("type") {
let target_type = get_node_text(&type_node, source);
if !target_type.contains('*') {
return;
}
} else {
return;
}
// Check if value involves pointer arithmetic with a struct field pointer
if let Some(value) = cast_node.child_by_field_name("value") {
if self.contains_struct_field_ptr_arithmetic(&value, source, struct_field_ptrs) {
let start = cast_node.start_position();
let text = get_node_text(cast_node, source).trim().to_string();
violations.push(RuleViolation {
rule_id: self.rule_id().to_string(),
severity: Severity::Medium,
message: format!(
"Pointer arithmetic on struct field address with type cast assumes specific memory layout: {}",
if text.len() > 60 { format!("{}...", &text[..60]) } else { text }
),
file_path: String::new(),
line: start.row + 1,
column: start.column + 1,
suggestion: Some(
"Access struct fields by name instead of using pointer arithmetic with assumed offsets. Struct layout may include padding.".to_string()
),
..Default::default()
});
}
}
}
/// Check if an expression contains pointer arithmetic with a struct field pointer
fn contains_struct_field_ptr_arithmetic(
&self,
node: &Node,
source: &str,
struct_field_ptrs: &HashSet<String>,
) -> bool {
match node.kind() {
"binary_expression" => {
// Check if either operand is a struct field pointer
for field in &["left", "right"] {
if let Some(operand) = node.child_by_field_name(field) {
if operand.kind() == "identifier" {
let name = get_node_text(&operand, source).trim().to_string();
if struct_field_ptrs.contains(&name) {
return true;
}
}
}
}
false
}
"parenthesized_expression" => {
for i in 0..node.child_count() {
if let Some(child) = node.child(i) {
if child.kind() != "(" && child.kind() != ")" {
return self.contains_struct_field_ptr_arithmetic(
&child,
source,
struct_field_ptrs,
);
}
}
}
false
}
_ => false,
}
}
/// Track union member accesses for type punning detection
fn track_union_member_access(
&self,
node: &Node,
source: &str,
union_vars: &HashSet<String>,
union_member_accesses: &mut HashMap<String, Vec<(String, usize, usize, bool)>>,
) {
if union_vars.is_empty() {
return;
}
// Only process outermost field_expression in a chain
// Skip if parent is also a field_expression and we're its argument
if let Some(parent) = node.parent() {
if parent.kind() == "field_expression" {
if let Some(parent_arg) = parent.child_by_field_name("argument") {
if parent_arg.id() == node.id() {
return;
}
}
}
}
// Check if this is a "deep" access (u.struct_member.field — 3+ levels)
// This indicates sub-field access into a struct union member, which is
// layout-dependent (byte-order, alignment)
let is_deep = node
.child_by_field_name("argument")
.map(|arg| arg.kind() == "field_expression")
.unwrap_or(false);
let (root, member) = self.get_field_access_root_and_member(node, source);
if let (Some(root_name), Some(member_name)) = (root, member) {
if union_vars.contains(&root_name) {
union_member_accesses.entry(root_name).or_default().push((
member_name,
node.start_position().row + 1,
node.start_position().column + 1,
is_deep,
));
}
}
}
/// Walk a field_expression chain to find root variable and first member name
/// For u.a → (u, a); for u.a.b.c → (u, a)
fn get_field_access_root_and_member(
&self,
node: &Node,
source: &str,
) -> (Option<String>, Option<String>) {
let mut current = *node;
loop {
if current.kind() != "field_expression" {
break;
}
if let Some(arg) = current.child_by_field_name("argument") {
if arg.kind() == "field_expression" {
current = arg;
} else {
// arg is the root (identifier)
let root = get_node_text(&arg, source).trim().to_string();
let member = current
.child_by_field_name("field")
.map(|f| get_node_text(&f, source).trim().to_string());
return (Some(root), member);
}
} else {
break;
}
}
(None, None)
}
/// Post-pass: flag union variables with layout-dependent type punning
/// Only flags when: (1) multiple different members accessed AND (2) at least one
/// access is "deep" (u.struct_member.field), indicating byte-order/alignment dependency
fn check_union_type_punning(
&self,
union_vars: &HashSet<String>,
union_member_accesses: &HashMap<String, Vec<(String, usize, usize, bool)>>,
violations: &mut Vec<RuleViolation>,
) {
for (var_name, accesses) in union_member_accesses {
if !union_vars.contains(var_name) {
continue;
}
let unique_members: HashSet<&str> =
accesses.iter().map(|(m, _, _, _)| m.as_str()).collect();
if unique_members.len() < 2 {
continue;
}
let has_deep = accesses.iter().any(|(_, _, _, deep)| *deep);
if !has_deep {
continue;
}
// Flag the deep accesses (sub-field access into struct union member)
let mut seen_members: HashSet<&str> = HashSet::new();
let mut first_member: Option<&str> = None;
for (member, line, col, is_deep) in accesses {
if first_member.is_none() {
first_member = Some(member);
seen_members.insert(member);
} else if let Some(first) = first_member {
if !seen_members.contains(member.as_str()) && *is_deep {
violations.push(RuleViolation {
rule_id: self.rule_id().to_string(),
severity: Severity::Medium,
message: format!(
"Accessing union '{}' member '{}' after writing to '{}' relies on data memory layout (byte-order, alignment, packing)",
var_name, member, first
),
file_path: String::new(),
line: *line,
column: *col,
suggestion: Some(
"Avoid relying on byte-order or alignment of union fields. Use type-appropriate bitwise operations instead of union type punning.".to_string()
),
..Default::default()
});
seen_members.insert(member);
}
}
}
}
}
}