- Introduced by
- David Engler and William Pugh
- Year
- 2001
- Type
- automated analysis
- Family
- Code quality inspection
What does it do?
Static code analysis examines source code without executing it to detect potential bugs, security vulnerabilities, code smells, and style violations.
Engler et al. (2001) demonstrate that correctness rules can be inferred from the code itself and cross-checked for contradictions, and Hovemeyer and Pugh (2004) describe automatic detectors for bug patterns in Java programs. Teams commonly run such tools in continuous integration to catch defects before code review or deployment.
The intuition behind the method
Many bugs follow recognisable patterns, such as null pointer dereferences, uninitialised variables, resource leaks, and unsafe use of library calls. Rather than waiting for testing or deployment to expose these, static analysis tools scan code for such patterns without running it. The rules come from two sources: rules written by people for known bug classes, and rules inferred from the code, for example by noticing that most calls to one function are paired with a call to another. A deviation from the inferred pattern is then flagged for review.
History
Engler et al. (2001) showed that correctness rules can be inferred from source code by treating facts implied by code as beliefs and cross-checking them for contradictions; their template checkers found hundreds of bugs in real systems such as Linux and OpenBSD. Hovemeyer and Pugh (2004) reported that automatic bug pattern detectors found serious bugs in several widely used Java applications and libraries, and that even simple detectors were effective in practice.
When to use it
Use static analysis as a routine check in continuous integration, running on each commit or on changed files, before human code review. It suits defects that have a recognisable code pattern, such as null dereferences, resource leaks, and injection-type security issues. Do not rely on it for logic errors, concurrency problems, or performance issues, which it generally does not model, and do not treat it as a substitute for testing. Adopt it only if the team will invest in tuning rules, because an untuned ruleset tends to produce warnings that developers learn to ignore.
Limitations and considerations
- Many findings are false positives. A conservative analysis of possible program states flags paths that cannot occur at run time, while a permissive analysis misses real bugs.
- Pattern detectors and rule templates see code idioms, not intent, so logic errors that look syntactically normal fall outside their reach.
- Belief-based checking ranks deviations by how often a pattern holds elsewhere in the code. A deliberate deviation that happens to be the only one will be ranked as a probable error.
- Coverage depends on language and framework support; checkers must be written or configured for each language's semantics and libraries.
Source conditions do not replace independent scientific review or validation by an analysis engine.
Strengths
- Works on source code without executing it, so a suspicious pattern can be flagged on a code path that tests never exercise.
- Pattern-based detectors are cheap to write: Hovemeyer and Pugh (2004) report that the effort to implement a bug pattern detector tends to be low, and that even simple detectors found bugs in real applications.
- Engler et al. (2001) infer rules from the code rather than requiring a complete specification written in advance, and report that their template checkers found hundreds of bugs in real systems such as Linux and OpenBSD.
- Taint tracking, which follows untrusted data through the program's data dependency graph, is a natural fit for injection-type security checks because it reasons about data flow without running the program.
Applications
- Catching SQL injection vulnerabilities in legacy web applications before deployment by scanning all database query construction sites for untrusted data flows
- Preventing resource leaks in embedded systems by detecting file handles and network sockets opened without corresponding close() calls
- Enforcing architectural constraints in large codebases by detecting unauthorized cross-layer dependencies between application modules
- Security hardening of medical device software by identifying unsafe use of cryptographic APIs and hardcoded secrets. The findings are candidate issues for security review; static analysis does not by itself establish that a device meets its requirements.
How it works
How it works
Obtain source code in version control or as direct submissions. Determine target language(s) and analysis scope (entire codebase or changed files only).
Parse source code into abstract syntax trees (ASTs) or intermediate representations (IRs). Build control flow graphs and data dependency graphs for program structure.
Apply pattern matching and semantic rules to detect violations: taint analysis (tracking untrusted data flows), null-pointer checks (flagging dereferences of pointers that may be null, since a dereference implies that the pointer is non-null), and resource tracking (ensuring files and connections are properly closed). Uninitialised-variable checks are a separate rule and are configured separately.
Rank findings by severity and confidence. Beliefs the code must satisfy, such as a pointer being non-null at the point it is dereferenced, are flagged directly when contradicted. Beliefs the programmer may hold, such as a call to one function normally being paired with another, are ranked statistically: the more often the pattern holds elsewhere in the code, the more likely a single deviation is a real error. Engler et al. (2001) give the example of a pairing that holds in 999 of 1000 cases, where the lone deviation is probably an error. Filter false positives using heuristics. Aggregate results by issue type and severity level. Rankings are heuristic and must be checked by a developer.
Present findings to developers: one finding per code location, with explanation and remediation suggestions. Read a ranked finding as a candidate for review: a high rank means the deviation is unusual relative to the code's own patterns, not that it has been confirmed as a bug. Integrate findings into code review and quality gates.
Common pitfalls
- Ignoring high false positive rates. When developers see floods of warnings, they stop reading them and genuine issues are missed.
- Treating every finding as a bug requiring an immediate fix. Some findings reflect deliberate design trade-offs or false positives and need a documented decision.
- Using static analysis as the only quality gate. It detects certain bug classes well but does not model logic, concurrency, or performance problems.
- Running a generic ruleset without tuning. Untuned rules generate noise; the checks have to be configured for the project's own libraries and conventions.
Frequently asked questions
How do I deal with high false positive rates in static analysis tools?
Tune the tool configuration to your codebase: disable irrelevant rules, adjust sensitivity thresholds, and mark intentional violations in the configuration. Suppress warnings only for specific instances with documented justification, not for entire files. Running more than one tool can help, since agreement between tools may raise confidence in a finding, though this is a heuristic and not a guarantee. Invest time calibrating tools to an acceptable level of noise.
Should static analysis replace human code review?
No. Static analysis and human review cover different kinds of problems. Static analysis is well suited to pattern-based bugs such as null pointers, resource leaks, and injection; human reviewers are better placed to judge logic, architecture, and readability. A practical arrangement is to run static analysis automatically on every change and have reviewers focus on logic, design, and complex interactions.
What kinds of bugs can static analysis reliably detect?
Static analysis works best on bugs with a recognisable code pattern, such as null pointer dereferences, buffer overflows, resource leaks, use of undefined variables, format string vulnerabilities, and SQL injection detected through taint analysis. It is weaker on logic errors, concurrency bugs such as race conditions, performance issues, API misuse when no explicit specification exists, and cryptographic weaknesses, which depend on context.
Can I use static analysis for security code review?
Yes, as a first pass. SAST tools (static application security testing) are designed for vulnerability detection. They are usually combined with dynamic testing, penetration testing, and expert human review, because static analysis alone misses context-dependent vulnerabilities and sophisticated attacks.
Related methods
Sources
Chess, B., & West, J. (2007). Secure Programming with Static Analysis. Addison-Wesley Professional.
Open publication referenceEngler, D., Chen, D. Y., Hallem, S., Chou, A., & Chelf, B. (2001). Bugs as deviant behavior: A general approach to inferring errors in systems code. In Proceedings of the 18th ACM Symposium on Operating Systems Principles (pp. 57–72).
Open DOI referenceHovemeyer, D., & Pugh, W. (2004). Finding bugs is easy. ACM SIGSOFT Software Engineering Notes, 29(6), 1–8.
Open DOI reference