-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfailure_scenarios.py
More file actions
1388 lines (1175 loc) · 63.2 KB
/
failure_scenarios.py
File metadata and controls
1388 lines (1175 loc) · 63.2 KB
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
#!/usr/bin/env python3
"""
Logic Bug Pattern Detector
This module defines and detects common logical bug patterns in Java source code,
which can be targeted by the test generation process. It includes pattern detectors
for various categories of logical bugs based on academic research and practical
experience with real bugs from defect repositories.
"""
import re
import os
import json
import logging
from collections import defaultdict
logger = logging.getLogger("logic_bug_patterns")
class FS_Detector:
"""
Detects common logical bug patterns in Java source code.
These patterns represent categories of logic bugs that are common
in real-world code and can be specifically targeted by tests.
"""
# 在 FS_Detector.__init__ 方法中更新检测器列表
def __init__(self, source_code, class_name, package_name, f_model=None):
"""
Initialize with source code to analyze
Parameters:
source_code (str): Java source code
class_name (str): Class name
package_name (str): Package name
f_model (object, optional): Logic model for enhanced detection
"""
self.source_code = source_code
self.class_name = class_name
self.package_name = package_name
self.lines = source_code.split('\n')
self.f_model = f_model
# Pattern detection results
self.patterns = []
# Register all pattern detectors
self.detectors = [
self._detect_operator_precedence_bugs,
self._detect_off_by_one_bugs,
self._detect_boundary_condition_bugs,
self._detect_null_handling_bugs,
self._detect_string_comparison_bugs,
self._detect_boolean_bugs,
self._detect_resource_leaks,
self._detect_state_corruption_bugs,
self._detect_integer_overflow_bugs,
self._detect_copy_paste_bugs,
self._detect_floating_point_comparison,
self._detect_exception_handling_bugs,
self._detect_complex_loop_conditions,
self._detect_resource_management_defects,
self._detect_data_operation_bugs,
self._detect_concurrency_issues,
self._detect_error_propagation_issues,
self._detect_improper_validation,
self._detect_security_vulnerabilities,
self._detect_string_index_bounds_bugs, # 添加新的字符串索引边界检测器
self._detect_array_index_bounds_bugs,
]
#logic bug patterns
# self.detectors = [
# self._detect_operator_precedence_bugs,
# self._detect_off_by_one_bugs,
# self._detect_boundary_condition_bugs,
# self._detect_string_comparison_bugs,
# self._detect_boolean_bugs,
# self._detect_integer_overflow_bugs,
# self._detect_copy_paste_bugs,
# self._detect_floating_point_comparison,
# self._detect_complex_loop_conditions,
# self._detect_data_operation_bugs,
# self._detect_string_index_bounds_bugs,
# self._detect_array_index_bounds_bugs,
# ]
def detect_patterns(self):
"""
Detect all registered logic bug patterns in the source code
Returns:
list: Detected patterns
"""
for detector in self.detectors:
try:
detector()
except Exception as e:
logger.error(f"Error in pattern detector {detector.__name__}: {str(e)}")
# Sort patterns by risk level (high to low)
self.patterns.sort(key=lambda p: {"high": 3, "medium": 2, "low": 1}.get(p["risk_level"], 0), reverse=True)
return self.patterns
def _detect_operator_precedence_bugs(self):
"""Detect potential operator precedence bugs"""
# Look for complex expressions with mixed operators without parentheses
mixed_operators_pattern = r'([^()]*?[&|<>=!^]+[^()]*?[&|<>=!^]+[^()]*?)'
matches = re.finditer(mixed_operators_pattern, self.source_code)
for match in matches:
expr = match.group(1).strip()
line_num = self._get_line_number(match.start())
# Skip if inside comments or strings
if self._is_in_comment_or_string(match.start()):
continue
# Check if expression has mixed AND/OR without parentheses
if ('&&' in expr and '||' in expr) or ('&' in expr and '|' in expr):
# Check if there are no parentheses grouping operators
if not re.search(r'\([^()]*?(?:&&|\|\|)[^()]*?\)', expr):
self.patterns.append({
"type": "operator_precedence",
"location": line_num,
"code": expr,
"risk_level": "high",
"description": "Mixed logical operators (AND/OR) without clarifying parentheses"
})
# Check for bitwise/logical operator confusion
if ('&' in expr and '&&' not in expr) or ('|' in expr and '||' not in expr):
# If looks like it might be intended as logical
if re.search(r'if\s*\([^)]*[&|][^)]*\)', expr):
self.patterns.append({
"type": "bitwise_logical_confusion",
"location": line_num,
"code": expr,
"risk_level": "high",
"description": "Possible confusion between bitwise (&, |) and logical (&&, ||) operators"
})
def _detect_off_by_one_bugs(self):
"""Detect potential off-by-one bugs"""
# Look for array access with hardcoded indices
array_access_pattern = r'(\w+)\s*\[\s*([^][]+)\s*\]'
matches = re.finditer(array_access_pattern, self.source_code)
for match in matches:
array_name = match.group(1)
index_expr = match.group(2)
line_num = self._get_line_number(match.start())
# Check if the index is a constant or involves length
if re.search(r'^\d+$', index_expr):
# Fixed index - check nearby for length comparisons
context = self._get_context(line_num, 5)
if f"{array_name}.length" in context:
self.patterns.append({
"type": "off_by_one",
"location": line_num,
"code": f"{array_name}[{index_expr}]",
"risk_level": "medium",
"description": f"Hardcoded array index ({index_expr}) near length check"
})
# Look for .length - 1 or .length comparisons
elif ".length - 1" in index_expr or ".length" in index_expr:
self.patterns.append({
"type": "off_by_one",
"location": line_num,
"code": f"{array_name}[{index_expr}]",
"risk_level": "medium",
"description": "Array access using length expression, potential off-by-one"
})
# Look for loop boundary conditions
loop_patterns = [
r'for\s*\([^;]*;\s*(\w+)\s*([<>=!]+)\s*([^;]+);',
r'while\s*\(\s*(\w+)\s*([<>=!]+)\s*([^)]+)\)'
]
for pattern in loop_patterns:
matches = re.finditer(pattern, self.source_code)
for match in matches:
var_name = match.group(1)
operator = match.group(2)
boundary = match.group(3).strip()
line_num = self._get_line_number(match.start())
# Check for common off-by-one patterns
if (".length" in boundary or ".size()" in boundary) and operator in ["<=", ">="]:
self.patterns.append({
"type": "off_by_one",
"location": line_num,
"code": f"{var_name} {operator} {boundary}",
"risk_level": "high",
"description": f"Potential off-by-one in loop condition using {operator} with length/size"
})
elif re.search(r'\d+', boundary) and operator in ["<=", ">="]:
context = self._get_context(line_num, 5)
array_or_list_check = re.search(r'(\w+)\.(?:length|size)', context)
if array_or_list_check:
self.patterns.append({
"type": "off_by_one",
"location": line_num,
"code": f"{var_name} {operator} {boundary}",
"risk_level": "medium",
"description": "Loop using <= or >= with constant boundary near array/list access"
})
def _detect_boundary_condition_bugs(self):
"""Detect potential boundary condition bugs"""
# Look for boundary checks
boundary_check_pattern = r'if\s*\(\s*([^)]+?)\s*([<>=!]+)\s*([^)]+?)\s*\)'
matches = re.finditer(boundary_check_pattern, self.source_code)
for match in matches:
left = match.group(1).strip()
operator = match.group(2)
right = match.group(3).strip()
line_num = self._get_line_number(match.start())
# Skip if in comment
if self._is_in_comment_or_string(match.start()):
continue
# Find checks against 0 or 1
if right in ["0", "1"] or left in ["0", "1"]:
# Check if there's array access nearby
context = self._get_context(line_num, 3)
if "[" in context and "]" in context:
self.patterns.append({
"type": "boundary_condition",
"location": line_num,
"code": f"{left} {operator} {right}",
"risk_level": "high",
"description": f"Boundary check against {right} near array access"
})
# Check for String length or Collection size boundary checks
if ".length()" in left or ".size()" in left or ".length" in left:
if operator in ["==", "<=", ">="]:
# Check if comparing against 0
if right == "0":
self.patterns.append({
"type": "boundary_condition",
"location": line_num,
"code": f"{left} {operator} {right}",
"risk_level": "medium",
"description": "Empty check using length/size"
})
else:
self.patterns.append({
"type": "boundary_condition",
"location": line_num,
"code": f"{left} {operator} {right}",
"risk_level": "medium",
"description": f"Size/length comparison using {operator}"
})
def _detect_null_handling_bugs(self):
"""Detect potential null handling bugs"""
# Look for missing null checks before method calls
method_call_pattern = r'(\w+)\.(\w+)\('
matches = re.finditer(method_call_pattern, self.source_code)
null_checked_vars = set()
# First, find all null checks
null_check_pattern = r'if\s*\(\s*(\w+)\s*(?:==|!=)\s*null\s*\)'
null_checks = re.finditer(null_check_pattern, self.source_code)
for check in null_checks:
null_checked_vars.add(check.group(1))
# Then find method calls on objects that might be null
for match in matches:
obj_name = match.group(1)
method_name = match.group(2)
line_num = self._get_line_number(match.start())
# Skip primitive types and common guaranteed non-null objects
if obj_name in ["this", "super", "String", "Integer", "Boolean", "Double", "Math"]:
continue
# Skip if we've seen a null check for this variable
if obj_name in null_checked_vars:
continue
# Check if near parameter usage (parameters often need null checks)
if self._is_likely_parameter(obj_name):
self.patterns.append({
"type": "null_handling",
"location": line_num,
"code": f"{obj_name}.{method_name}(...)",
"risk_level": "high",
"description": f"Method call on potential parameter {obj_name} without null check"
})
# Look for nested null-safe access patterns
nested_access_pattern = r'(\w+)\.(\w+)\.(\w+)'
matches = re.finditer(nested_access_pattern, self.source_code)
for match in matches:
obj_name = match.group(1)
line_num = self._get_line_number(match.start())
# Skip if not nested property/method access
if not self._is_property_access(match.group(2)):
continue
# Check if there's no null check
if obj_name not in null_checked_vars:
self.patterns.append({
"type": "null_handling",
"location": line_num,
"code": match.group(0),
"risk_level": "medium",
"description": "Nested property access without null checking intermediate results"
})
def _detect_string_comparison_bugs(self):
"""Detect potential string comparison bugs"""
# Look for string comparison using == instead of equals()
str_comparison_pattern = r'(\w+)\s*(==|!=)\s*(["\w]+)'
matches = re.finditer(str_comparison_pattern, self.source_code)
for match in matches:
left = match.group(1)
operator = match.group(2)
right = match.group(3)
line_num = self._get_line_number(match.start())
# Skip if in comment or if comparing null
if self._is_in_comment_or_string(match.start()) or right == "null" or left == "null":
continue
# Check if comparing string literals or variables of string type
context = self._get_context(line_num, 5)
if ('"' in right or
"String" in context and (right.isalpha() or left.isalpha())):
self.patterns.append({
"type": "string_comparison",
"location": line_num,
"code": f"{left} {operator} {right}",
"risk_level": "high",
"description": f"Possible string comparison using {operator} instead of .equals()"
})
def _detect_boolean_bugs(self):
"""Detect potential boolean logic bugs"""
# Look for complex boolean expressions
bool_expr_pattern = r'(?:if|while)\s*\(\s*([^{};()]+?(?:&&|\|\|)[^{};()]+?)\s*\)'
matches = re.finditer(bool_expr_pattern, self.source_code)
for match in matches:
expr = match.group(1).strip()
line_num = self._get_line_number(match.start())
# Check for potential negation issues (double negation, etc.)
if expr.count('!') > 1:
self.patterns.append({
"type": "boolean_bug",
"location": line_num,
"code": expr,
"risk_level": "medium",
"description": "Multiple negations in boolean expression, possible logic error"
})
# Check for DeMorgan's law violations - common logical mistake
demorgan_pattern = r'!\s*\(\s*([^()]+?)\s*(?:&&|\|\|)\s*([^()]+?)\s*\)'
if re.search(demorgan_pattern, expr):
self.patterns.append({
"type": "boolean_bug",
"location": line_num,
"code": expr,
"risk_level": "high",
"description": "Negated AND/OR expression, potential DeMorgan's Law error"
})
# Check for redundant conditions
parts = re.split(r'&&|\|\|', expr)
unique_parts = set(p.strip() for p in parts)
if len(parts) != len(unique_parts):
self.patterns.append({
"type": "boolean_bug",
"location": line_num,
"code": expr,
"risk_level": "medium",
"description": "Redundant conditions in boolean expression"
})
# Check for potential tautologies or contradictions
if ('true' in expr and '||' in expr) or ('false' in expr and '&&' in expr):
self.patterns.append({
"type": "boolean_bug",
"location": line_num,
"code": expr,
"risk_level": "medium",
"description": "Potential tautology or contradiction in boolean expression"
})
def _detect_resource_leaks(self):
"""Detect potential resource leak bugs"""
# Look for resource allocations without try-with-resources
resource_pattern = r'new\s+(FileInputStream|FileOutputStream|BufferedReader|Scanner|Connection)[\s\(]'
matches = re.finditer(resource_pattern, self.source_code)
for match in matches:
resource_type = match.group(1)
line_num = self._get_line_number(match.start())
context = self._get_context(line_num, 10)
# Check if it's in a try-with-resources
if "try (" in context and ")" in context.split("try (")[1].split("{")[0]:
continue
# Check if there's a close method call
if ".close()" not in context:
self.patterns.append({
"type": "resource_leak",
"location": line_num,
"code": f"new {resource_type}(...)",
"risk_level": "high",
"description": f"Resource allocation without proper closing or try-with-resources"
})
def _detect_state_corruption_bugs(self):
"""Detect potential state corruption bugs"""
# Look for collections being modified during iteration
iterator_pattern = r'for\s*\(\s*(?:\w+\s+)?(\w+)\s*:\s*(\w+)\s*\)'
matches = re.finditer(iterator_pattern, self.source_code)
for match in matches:
loop_var = match.group(1)
collection = match.group(2)
line_num = self._get_line_number(match.start())
# Find the loop body
loop_start = self.source_code.find("{", match.end())
if loop_start == -1:
continue
# Find the corresponding closing brace
depth = 1
loop_end = loop_start + 1
while depth > 0 and loop_end < len(self.source_code):
if self.source_code[loop_end] == '{':
depth += 1
elif self.source_code[loop_end] == '}':
depth -= 1
loop_end += 1
loop_body = self.source_code[loop_start:loop_end]
# Check if collection is modified in the loop body
if f"{collection}.add" in loop_body or f"{collection}.remove" in loop_body:
self.patterns.append({
"type": "state_corruption",
"location": line_num,
"code": f"for ({loop_var} : {collection})",
"risk_level": "high",
"description": f"Collection {collection} modified during iteration, possible ConcurrentModificationException"
})
def _detect_integer_overflow_bugs(self):
"""Detect potential integer overflow bugs"""
# Look for arithmetic operations on integers near MAX_VALUE
max_value_pattern = r'(Integer\.MAX_VALUE|Long\.MAX_VALUE)'
matches = re.finditer(max_value_pattern, self.source_code)
for match in matches:
line_num = self._get_line_number(match.start())
context = self._get_context(line_num, 3)
# Check if there's arithmetic near MAX_VALUE
if re.search(r'[+\-*/]', context):
self.patterns.append({
"type": "integer_overflow",
"location": line_num,
"code": context.split('\n')[0],
"risk_level": "high",
"description": f"Arithmetic operation near {match.group(1)}, possible overflow"
})
# Look for unchecked array allocation with large sizes
large_array_pattern = r'new\s+\w+\[([^]]+)\]'
matches = re.finditer(large_array_pattern, self.source_code)
for match in matches:
size_expr = match.group(1)
line_num = self._get_line_number(match.start())
# Check if the size involves arithmetic or large constants
if re.search(r'[+\-*/]', size_expr) or re.search(r'\d{6,}', size_expr):
self.patterns.append({
"type": "integer_overflow",
"location": line_num,
"code": f"new ...[{size_expr}]",
"risk_level": "medium",
"description": "Array allocation with complex size expression, possible overflow"
})
def _detect_copy_paste_bugs(self):
"""Detect potential copy-paste bugs"""
# Look for similar consecutive lines with small differences
lines = self.source_code.split('\n')
for i in range(len(lines) - 1):
if len(lines[i].strip()) < 10: # Skip short lines
continue
current = lines[i].strip()
next_line = lines[i+1].strip()
# If lines are similar but not identical
if current != next_line and self._similarity(current, next_line) > 0.8:
# Find the differences
diff_indices = [j for j in range(min(len(current), len(next_line))) if current[j] != next_line[j]]
# If there are only a few differences
if 0 < len(diff_indices) <= 5:
# Check if only variable names changed
current_diff = ''.join(current[j] for j in diff_indices if j < len(current))
next_diff = ''.join(next_line[j] for j in diff_indices if j < len(next_line))
if current_diff.isalnum() and next_diff.isalnum():
self.patterns.append({
"type": "copy_paste",
"location": i + 1, # 1-based line number
"code": f"{current}\n{next_line}",
"risk_level": "medium",
"description": "Similar consecutive lines with small differences, potential copy-paste error"
})
def _detect_floating_point_comparison(self):
"""Detect exact floating point comparisons"""
# Look for exact equality comparison with floats/doubles
float_comparison_pattern = r'([^=!><]|^)(==|!=)\s*(\d+\.\d+)'
matches = re.finditer(float_comparison_pattern, self.source_code)
for match in matches:
operator = match.group(2)
float_value = match.group(3)
line_num = self._get_line_number(match.start())
# Skip if in comment or string
if self._is_in_comment_or_string(match.start()):
continue
context = self._get_context(line_num, 3)
if "float" in context or "double" in context or "Float" in context or "Double" in context:
self.patterns.append({
"type": "floating_point_comparison",
"location": line_num,
"code": f"... {operator} {float_value}",
"risk_level": "high",
"description": f"Exact comparison of floating point values using {operator}"
})
# Also look for variable comparisons in floating point context
var_comparison_pattern = r'(\w+)\s+(==|!=)\s+(\w+)'
matches = re.finditer(var_comparison_pattern, self.source_code)
for match in matches:
var1 = match.group(1)
operator = match.group(2)
var2 = match.group(3)
line_num = self._get_line_number(match.start())
# Check if in a float/double context
context = self._get_context(line_num, 5)
if ("float" in context or "double" in context or "Float" in context or "Double" in context) and \
not "int " in context and not "Integer" in context:
self.patterns.append({
"type": "floating_point_comparison",
"location": line_num,
"code": f"{var1} {operator} {var2}",
"risk_level": "high",
"description": f"Potential exact comparison of floating point variables using {operator}"
})
def _detect_exception_handling_bugs(self):
"""Detect potential exception handling bugs"""
# Look for empty catch blocks
empty_catch_pattern = r'catch\s*\([^)]+\)\s*\{\s*\}'
matches = re.finditer(empty_catch_pattern, self.source_code)
for match in matches:
line_num = self._get_line_number(match.start())
self.patterns.append({
"type": "exception_handling",
"location": line_num,
"code": match.group(0),
"risk_level": "medium",
"description": "Empty catch block, silently swallowing exception"
})
# Look for catch blocks that only have comments
comment_catch_pattern = r'catch\s*\([^)]+\)\s*\{\s*(?://[^\n]*|/\*[^*]*\*/)\s*\}'
matches = re.finditer(comment_catch_pattern, self.source_code)
for match in matches:
line_num = self._get_line_number(match.start())
self.patterns.append({
"type": "exception_handling",
"location": line_num,
"code": match.group(0),
"risk_level": "low",
"description": "Catch block with only comments, effectively swallowing exception"
})
# Look for catch Exception (too generic)
generic_catch_pattern = r'catch\s*\(\s*(?:Exception|Throwable|RuntimeException)\s+'
matches = re.finditer(generic_catch_pattern, self.source_code)
for match in matches:
line_num = self._get_line_number(match.start())
self.patterns.append({
"type": "exception_handling",
"location": line_num,
"code": match.group(0) + "...",
"risk_level": "medium",
"description": "Catching generic Exception/Throwable, may mask important errors"
})
# Look for throw from finally block
throw_finally_pattern = r'finally\s*\{[^}]*throw\s+'
matches = re.finditer(throw_finally_pattern, self.source_code)
for match in matches:
line_num = self._get_line_number(match.start())
self.patterns.append({
"type": "exception_handling",
"location": line_num,
"code": "finally { ... throw ...",
"risk_level": "high",
"description": "Throwing exception from finally block, may mask original exception"
})
def _detect_complex_loop_conditions(self):
"""Detect overly complex loop conditions"""
# Look for while loops with complex conditions
while_pattern = r'while\s*\(\s*([^{};()]+?(?:&&|\|\|)[^{};()]+?)\s*\)'
matches = re.finditer(while_pattern, self.source_code)
for match in matches:
condition = match.group(1)
line_num = self._get_line_number(match.start())
# Count logical operators
op_count = condition.count("&&") + condition.count("||")
if op_count >= 2:
self.patterns.append({
"type": "complex_loop_condition",
"location": line_num,
"code": f"while ({condition})",
"risk_level": "medium",
"description": f"Complex loop condition with {op_count} logical operators"
})
# Look for loops that update their own condition variables
for_pattern = r'for\s*\(\s*(?:\w+\s+)?(\w+)[^;]*;\s*\1[^;]*;\s*\1\s*([+\-*/%]=|\+\+|--)'
matches = re.finditer(for_pattern, self.source_code)
for match in matches:
var_name = match.group(1)
update_op = match.group(2)
line_num = self._get_line_number(match.start())
# Check if the loop also updates the variable inside the body
# (this can lead to unexpected termination or infinite loops)
loop_start = self.source_code.find("{", match.end())
if loop_start == -1:
continue
# Find the corresponding closing brace
depth = 1
loop_end = loop_start + 1
while depth > 0 and loop_end < len(self.source_code):
if self.source_code[loop_end] == '{':
depth += 1
elif self.source_code[loop_end] == '}':
depth -= 1
loop_end += 1
loop_body = self.source_code[loop_start:loop_end]
# Regex to find var updates like var++, var--, var += etc.
body_updates = re.findall(f"\\b{var_name}\\s*([+\\-*/%]=|\\+\\+|--)", loop_body)
if body_updates:
self.patterns.append({
"type": "complex_loop_condition",
"location": line_num,
"code": f"for (...{var_name}...;...{var_name}...;...{var_name}{update_op}...)",
"risk_level": "high",
"description": f"Loop variable {var_name} updated both in loop control and loop body, potential logic error"
})
def _detect_resource_management_defects(self):
"""Detect potential resource management defects such as leaks or improper cleanup"""
# Look for resource acquisition without proper release
resource_patterns = [
# File handling resources
(r'new FileInputStream\([^)]+\)', r'\.close\(\)'),
(r'new FileOutputStream\([^)]+\)', r'\.close\(\)'),
(r'new FileReader\([^)]+\)', r'\.close\(\)'),
(r'new FileWriter\([^)]+\)', r'\.close\(\)'),
# Database connections
(r'getConnection\([^)]+\)', r'\.close\(\)'),
(r'createStatement\(\)', r'\.close\(\)'),
(r'prepareStatement\([^)]+\)', r'\.close\(\)'),
# Locks and other resources
(r'\.lock\(\)', r'\.unlock\(\)'),
(r'\.acquire\(\)', r'\.release\(\)')
]
for resource_pattern, release_pattern in resource_patterns:
resource_matches = re.finditer(resource_pattern, self.source_code)
for match in resource_matches:
line_num = self._get_line_number(match.start())
# Skip if inside comments or strings
if self._is_in_comment_or_string(match.start()):
continue
# Check if the resource is properly released
context_after = self.source_code[match.end():match.end()+500]
# Check if there's a try-with-resources structure (Java 7+)
try_with_resources = re.search(r'try\s*\(\s*[^)]*' + re.escape(match.group(0)), context_after)
# If not in try-with-resources, check for explicit close/release
if not try_with_resources:
has_release = re.search(release_pattern, context_after)
# If no release found, report as potential resource leak
if not has_release:
self.patterns.append({
"type": "resource_management",
"subtype": "resource_leak",
"location": line_num,
"code": match.group(0),
"risk_level": "high",
"description": f"Resource acquired but not properly released: {match.group(0)}"
})
# Check for closed resources that might be used afterward
close_matches = re.finditer(r'(\w+)\.close\(\)', self.source_code)
for match in close_matches:
resource_var = match.group(1)
line_num = self._get_line_number(match.start())
# Skip if inside comments or strings
if self._is_in_comment_or_string(match.start()):
continue
# Check if resource is used after being closed
context_after = self.source_code[match.end():match.end()+500]
usage_after_close = re.search(r'\b' + re.escape(resource_var) + r'\.\w+', context_after)
if usage_after_close:
self.patterns.append({
"type": "resource_management",
"subtype": "use_after_close",
"location": line_num,
"code": f"{resource_var}.close()",
"risk_level": "high",
"description": f"Resource {resource_var} might be used after being closed"
})
def _detect_data_operation_bugs(self):
"""Detect potential data operation issues like improper conversions or manipulations"""
# Check for risky type conversions
risky_conversions = [
# Integer truncation
(r'(\w+)\s*=\s*\(int\)\s*(\w+)', "integer_truncation"),
# Double to float loss of precision
(r'(\w+)\s*=\s*\(float\)\s*(\w+)', "precision_loss"),
# Long to int conversions
(r'(\w+)\s*=\s*\(int\)\s*(\w+)\.(\w+)(?:\(\))?', "long_to_int_conversion")
]
for pattern, issue_type in risky_conversions:
matches = re.finditer(pattern, self.source_code)
for match in matches:
line_num = self._get_line_number(match.start())
# Skip if inside comments or strings
if self._is_in_comment_or_string(match.start()):
continue
self.patterns.append({
"type": "data_operation",
"subtype": issue_type,
"location": line_num,
"code": match.group(0),
"risk_level": "medium",
"description": f"Potentially risky type conversion: {match.group(0)}"
})
# Check for integer division issues
int_division_matches = re.finditer(r'(\b\d+)\s*/\s*(\b\d+)', self.source_code)
for match in int_division_matches:
line_num = self._get_line_number(match.start())
# Skip if inside comments or strings
if self._is_in_comment_or_string(match.start()):
continue
# Check if this looks like integer division that should be floating point
context = self._get_context(line_num, 2)
if "double" in context or "float" in context:
self.patterns.append({
"type": "data_operation",
"subtype": "integer_division",
"location": line_num,
"code": match.group(0),
"risk_level": "medium",
"description": "Integer division in floating-point context may cause precision loss"
})
# Check for signed/unsigned comparison issues
signed_unsigned_matches = re.finditer(r'([a-zA-Z0-9_.]+)\.length\s*([<>=!]+)\s*(-\d+)', self.source_code)
for match in signed_unsigned_matches:
if match.group(3).startswith('-'): # Negative comparison with length which is always >= 0
line_num = self._get_line_number(match.start())
self.patterns.append({
"type": "data_operation",
"subtype": "signed_unsigned_comparison",
"location": line_num,
"code": match.group(0),
"risk_level": "medium",
"description": "Comparison of .length (always >= 0) with negative value"
})
def _detect_concurrency_issues(self):
"""Detect potential concurrency issues such as race conditions or deadlocks"""
# Check for shared state without synchronization
shared_fields_pattern = r'(private|protected|public)(?:\s+static)?\s+(?!final)\s*(\w+)(?:<[^>]+>)?\s+(\w+)\s*[=;]'
shared_fields = re.finditer(shared_fields_pattern, self.source_code)
synchronized_fields = set()
synchronized_blocks = re.finditer(r'synchronized\s*\(\s*(\w+|\bthis\b)\s*\)', self.source_code)
# Collect synchronized fields
for match in synchronized_blocks:
synchronized_fields.add(match.group(1))
for match in shared_fields:
# Skip if field is primitive or immutable
field_type = match.group(2)
if field_type in ['int', 'boolean', 'char', 'byte', 'short', 'long', 'float', 'double', 'String']:
continue
field_name = match.group(3)
line_num = self._get_line_number(match.start())
# Skip if inside comments or strings
if self._is_in_comment_or_string(match.start()):
continue
# Check if field is accessed in a method without synchronization
if 'synchronized' not in match.group(0) and field_name not in synchronized_fields:
# Check for multiple threads accessing this field
thread_references = re.search(r'Thread|Runnable|Callable|ExecutorService', self.source_code)
if thread_references:
self.patterns.append({
"type": "concurrency",
"subtype": "unsynchronized_shared_state",
"location": line_num,
"code": match.group(0),
"risk_level": "high",
"description": f"Potentially shared mutable field '{field_name}' without proper synchronization"
})
# Check for possible deadlocks (nested synchronized blocks)
method_pattern = r'(?:public|protected|private)\s+[\w<>[\],\s]+\s+(\w+)\s*\([^)]*\)\s*\{([^}]+)'
method_matches = re.finditer(method_pattern, self.source_code)
for method_match in method_matches:
method_body = method_match.group(2)
method_name = method_match.group(1)
# Find synchronized blocks
sync_blocks = re.finditer(r'synchronized\s*\(\s*(\w+|\bthis\b)\s*\)', method_body)
synced_objects = [m.group(1) for m in sync_blocks]
# Check for nested synchronized blocks
if len(synced_objects) > 1:
line_num = self._get_line_number(method_match.start())
self.patterns.append({
"type": "concurrency",
"subtype": "potential_deadlock",
"location": line_num,
"code": f"Method {method_name} with multiple synchronized blocks",
"risk_level": "high",
"description": f"Multiple synchronized blocks in method '{method_name}' may lead to deadlocks"
})
def _detect_error_propagation_issues(self):
"""Detect potential issues with error handling and propagation"""
# Check for empty catch blocks
empty_catch_pattern = r'catch\s*\([^)]+\)\s*\{\s*\}'
empty_catches = re.finditer(empty_catch_pattern, self.source_code)
for match in empty_catches:
line_num = self._get_line_number(match.start())
# Skip if inside comments or strings
if self._is_in_comment_or_string(match.start()):
continue
self.patterns.append({
"type": "exception_handling",
"subtype": "empty_catch",
"location": line_num,
"code": match.group(0),
"risk_level": "high",
"description": "Empty catch block swallows exception without handling"
})
# Check for exception swallowing (catch Throwable)
catch_throwable_pattern = r'catch\s*\(\s*(Throwable|Exception)\s+\w+\s*\)'
catch_throwables = re.finditer(catch_throwable_pattern, self.source_code)
for match in catch_throwables:
line_num = self._get_line_number(match.start())
# Skip if inside comments or strings
if self._is_in_comment_or_string(match.start()):
continue
# Get context to see how it's handled
context_after = self.source_code[match.end():match.end()+300]
proper_handling = re.search(r'(?:throw|log|report|printStackTrace)', context_after)
if not proper_handling:
self.patterns.append({
"type": "exception_handling",
"subtype": "swallowed_exception",
"location": line_num,
"code": match.group(0),
"risk_level": "high",
"description": f"Catching {match.group(1)} without proper handling may swallow important exceptions"
})
def _detect_string_index_bounds_bugs(self):
"""Detect potential string index out of bounds bugs"""
# Look for string index access and substring operations
string_access_pattern = r'(\w+)\.charAt\(\s*([^)]+)\s*\)'
substring_pattern = r'(\w+)\.substring\(\s*([^,)]+)(?:\s*,\s*([^)]+))?\s*\)'
# Process charAt matches
matches = re.finditer(string_access_pattern, self.source_code)
for match in matches:
string_var = match.group(1)
index_expr = match.group(2)
line_num = self._get_line_number(match.start())
# Skip if in comment or string
if self._is_in_comment_or_string(match.start()):
continue
# Check for risky index expressions
if index_expr.isdigit() or "-" in index_expr or "+" in index_expr:
# Fixed or calculated index - check for length checks
context = self._get_context(line_num, 5)
if f"{string_var}.length()" not in context:
self.patterns.append({
"type": "string_index_bounds",
"location": line_num,
"code": f"{string_var}.charAt({index_expr})",
"risk_level": "high",
"description": f"String charAt() without proper length check, potential StringIndexOutOfBoundsException"
})
# Process substring matches
matches = re.finditer(substring_pattern, self.source_code)
for match in matches:
string_var = match.group(1)
start_idx = match.group(2)
end_idx = match.group(3) # Could be None for single-arg substring
line_num = self._get_line_number(match.start())
# Skip if in comment or string
if self._is_in_comment_or_string(match.start()):
continue
# Check for risky substring operations
if (start_idx.isdigit() or "-" in start_idx or "+" in start_idx or