Static analysis examines a program without running it, to find bugs, enforce rules or prove properties. Type checkers, linters and compiler warnings are all static analysers. The core trade-off never goes away: a perfect analysis of a nontrivial property is impossible, so tools either miss some bugs or report some that are not real.
Key concepts
- Control-flow graph: the program as blocks of code joined by the jumps between them.
- Data-flow analysis: facts, such as which variables may be null, propagated along the graph until nothing changes.
- Abstract interpretation: run the program on approximate values to get a sound over-approximation.
- Soundness and completeness: a sound tool misses no real bug; a complete one reports no false alarm; you cannot have both.
- Pattern and style checking: linters match known bad shapes of code cheaply.
- Type systems: a lightweight, built-in static analysis.
Vocabulary
- False positive: a warning about code that is actually fine.
- False negative: a real bug the tool did not report.
- Taint analysis: tracking where untrusted data can flow.
- Lattice: the ordered set of abstract facts that an analysis climbs.
What to study, in order
- Turn on every warning of your compiler and linter, and read each one to decide if it is real.
- Build a control-flow graph for a small function by hand.
- Implement a simple data-flow analysis, such as unused variables, over a toy language.
- Learn the idea of abstract interpretation with an interval domain.
- Run an open source analyser on a project of yours and triage the results.