Repository navigation
Bug Detection
bceverly edited this page Nov 3, 2025
·
1 revision
This page documents the 162 Bug Detection rules that find reliability issues - actual defects that will cause runtime errors, incorrect results, or program crashes.
See also: Security Vulnerabilities | Code Smells | Rule Catalog
(* BUG *)
Calculate[x_, y_] := x / y (* Crashes when y = 0 *)
(* FIX *)
CalculateSafe[x_, y_] := If[y == 0, $Failed, x / y](* BUG *)
Process[] := While[True, DoSomething[]] (* Never exits *)
(* FIX *)
Process[] := Module[{iter = 0, maxIter = 1000},
While[iter < maxIter && !Done[], iter++; DoSomething[]]
](* BUG *)
Sum[i, {i, n}] (* If n undefined, error *)
(* FIX *)
If[!NumericQ[n], Return[$Failed]]; Sum[i, {i, n}](* BUG *)
GetElement[list_, i_] := list[[i]] (* Crashes if i > Length[list] *)
(* FIX *)
GetElement[list_, i_] := If[1 <= i <= Length[list], list[[i]], $Failed](* BUG *)
Process[data_] := Module[{result},
result = ComputeValue[data]; (* May return $Failed *)
result + 1 (* Fails *)
]
(* FIX *)
Process[data_] := Module[{result},
result = ComputeValue[data];
If[FailureQ[result], Return[$Failed]];
result + 1
](* BUG *)
ReadData[] := Module[{stream, data},
stream = OpenRead["data.txt"];
data = ReadList[stream];
(* Forgot to close! *)
data
]
(* FIX *)
ReadData[] := Module[{stream, data},
stream = OpenRead["data.txt"];
data = ReadList[stream];
Close[stream];
data
]
(* BEST *)
ReadData[] := Import["data.txt", "Table"](* BUG *)
Add[x_, y_] := x + y
Add["hello", 5] (* String + Number error *)
(* FIX *)
Add[x_?NumericQ, y_?NumericQ] := x + y
Add[___] := $Failed###8. Undefined Symbols
(* BUG *)
Calculate[] := unknownFunction[data] (* unknownFunction undefined *)
(* FIX *)
If[!ValueQ[unknownFunction], Return[$Failed]];
Calculate[] := unknownFunction[data](* BUG *)
f[x__] := x (* Matches 1+ args, returns Sequence *)
(* FIX *)
f[x__] := {x} (* Wrap in List *)(* BUG *)
Factorial[n_] := n * Factorial[n - 1] (* Infinite recursion! *)
(* FIX *)
Factorial[0] = 1;
Factorial[n_?Positive] := n * Factorial[n - 1]- Uninitialized variable use
- Use before definition
- Variable shadowing bugs
- Undefined symbol references
- Missing Module/Block scoping
- $Failed propagation
- Missing[] handling
- Null checks
- Failed function calls
- Error propagation
- File handles not closed
- Stream leaks
- Database connections
- Memory leaks
- Temporary file cleanup
- Uninitialized reads
- Dead stores
- Use-after-free equivalent
- Double evaluation
- Side effects in pure functions
- Unreachable code
- Dead branches
- Missing return statements
- Infinite loops
- Recursion depth
- Type mismatches
- Pattern type errors
- List/Association confusion
- String/Symbol confusion
- Numeric precision
- Index out of bounds
- Empty list operations
- Part[] errors
- List length mismatches
- Ragged arrays
- Wrong argument count
- Incorrect argument types
- Missing required parameters
- Apply/Map errors
- Hold attribute violations
- Ambiguous patterns
- Overlapping definitions
- Pattern test failures
- Blank vs BlankSequence
- Optional parameter bugs
- Circular dependencies
- Undefined functions
- Missing package imports
- Context resolution
Analyzes syntax tree structure to find:
- Missing base cases
- Unreachable code
- Pattern errors
Tracks values through code to find:
- Uninitialized reads
- Null propagation
- Resource leaks
Builds execution graph to find:
- Infinite loops
- Dead code
- Missing returns
Infers types to find:
- Type mismatches
- Invalid operations
- Precision loss
- Review all CRITICAL and HIGH bugs first
- Test fixes thoroughly - bugs indicate edge cases
- Add defensive checks - validate inputs
- Use Quick Fixes when available
- Add tests to prevent regression
See Rule Catalog for all 162 bug rules.