Guide · Static analysis

Static code analysis: finding defects without running the program

Static analysis examines source code without running it. It spots bugs, coding rule violations and security flaws as the code is written, while a defect is still cheap to fix. Here is what it detects, what it does not do, what the standards say about it, and how to go about it when you have several tools and several teams.

Definition

What is static code analysis?

A test runs the program and observes what it does. Static analysis reads the code instead: it rebuilds its structure, follows the possible values of variables and the execution paths, then reports likely defects and departures from coding rules. It needs neither the target hardware nor test cases. It can therefore run on every change, in the developer's environment as well as in the continuous integration pipeline.

The compiler already performs a form of static analysis: its compiler warnings are the first level of verification, and the most underrated one. Dedicated tools go much further.

Defects

What it detects

CategoryExamples
Run-time errorsNull pointer dereference, division by zero, array or integer overflow, variable read before it is initialized.
Logic and resource defectsMemory leaks, unclosed files, dead code, conditions that are always true, poorly protected concurrent access.
Coding rulesMISRA C and MISRA C++, AUTOSAR C++14, CERT C, CERT Java (SEI CERT Oracle Coding Standard for Java), the Philips C# coding standard maintained by TIOBE, in-house company rules.
SecurityInjections, unchecked external data, dangerous functions, weaknesses listed in the CWE.
MaintainabilityCyclomatic complexity, duplication, excessive dependencies between modules.
Compiler warningsThe ones the compiler already issues, but that nobody follows up on.
Tools

The families of tools

Rule checkers

They check compliance with a coding standard: MISRA, CERT, naming conventions. They are the fastest and the easiest to adopt.

Bug finders

They follow data flow and control flow to find likely run-time errors. They are very useful, but they do not find everything and they raise false alarms.

Abstract interpretation

Tools based on it, such as Coverity, prove the absence of certain classes of run-time errors. They are more demanding to deploy, and valuable for critical software.

Security tools (SAST)

They focus on vulnerabilities, for example Checkmarx or Fortify.

Maintainability metrics

They assess the complexity, duplication and structure of the code.

No tool covers everything. Most teams combine several, often depending on languages and projects, and that is where the trouble starts: scattered results, different formats, no big picture.

Limits

What static analysis does not do

It does not find everything

No tool can decide every property of a program. There will always be unreported defects and unjustified alarms. The point is to tune them, not to ignore them.

It does not check that the software does what it should

It finds defects in the code; only tests verify that the code meets its requirements. The two complement each other, and test coverage measures what the tests actually exercised.

It is useless if nobody handles its results

A list of ten thousand alarms produced once improves nothing. You need a starting point, priorities, and one simple rule: new code adds no defects.

Standards

What the safety standards say

StandardWhat it asks for
ISO 26262-6 (automotive)Recommends static code analysis to verify software units, whatever the ASIL, and requires coding guidelines, for example MISRA C.
IEC 61508-3 (industrial)Rates static analysis as "recommended" at SIL 1 and "highly recommended" from SIL 2 to SIL 4.
EN 50128 / EN 50716 (railway)Devotes a table of techniques to it among the software verification methods.
DO-178C (avionics)Requires reviews and analyses of the source code: compliance with coding standards, accuracy, consistency. A tool whose output replaces a verification activity must be qualified under DO-330.
IEC 62304 (medical)Requires software unit verification without imposing a technique; static analysis is a common way to meet it.

Outside embedded software, no standard mandates static analysis. But auditors, customers and regulators increasingly ask for evidence of code quality, especially for code delivered by subcontractors.

Choosing

How to choose a static analysis tool

  1. Languages and compilers: including embedded compilers and their extensions.
  2. Target rules: MISRA, CERT, AUTOSAR, CWE, security rules, in-house rules.
  3. Relevance: the false alarm rate, on your code rather than on a demo.
  4. Integration: in the developer's environment, the continuous integration pipeline and code review.
  5. Deployment: on-premise if your code must not leave the building.
  6. Tool qualification, if your standard requires it.
  7. Handling the results: baseline, priorities, justified deviations, tracking over time.
TiCS

One more analyzer, or the big picture?

Most of the time, the problem is not a lack of tools, but how many there are. Each project has its analyzers, each tool its format, and nobody can tell whether code quality is improving.

TiCS does not replace your analyzers: it orchestrates them. It takes in the results of more than a hundred tools, in 25 languages, including:

  • in C and C++: Coverity, Klocwork, CodeSonar, Astrée, PC-Lint, Cppcheck and Parasoft C++test;
  • in Java: SpotBugs, PMD, CheckStyle and Parasoft Jtest;
  • in C#: Roslyn analyzers, ReSharper and Parasoft dotTEST;
  • Checkmarx for security, Pylint for Python;
  • your compiler warnings.

It maps them to the ISO/IEC 25010 quality model and sums them up in one indicator, the TQI, readable from developers to management. Quality gates stop new code from adding defects. Trends show whether quality is improving, project by project and across the whole portfolio. TiCS is installed on-premise when your context requires it.

Languages and tools supported by TiCS →
TiCS or SonarQube? →

All your tools, one platform1 min 49 s · silent animation

Key takeaways

  • Static analysis finds defects in the code without running it, when they are cheapest to fix.
  • It covers run-time errors, coding rules, security, maintainability and compiler warnings.
  • It does not replace testing: it complements it, and coverage measures what the tests exercised.
  • Safety standards recommend or require it in one form or another.
  • With several tools and several teams, what matters becomes the big picture and tracking over time.
FAQ

Frequently asked questions

What is the difference between static and dynamic analysis?

Static analysis reads the code without running it. Dynamic analysis observes the program while it runs: tests, coverage measurement, run-time memory error detection. The two complement each other.

Does static analysis replace code review?

No. It takes care of the mechanical checks, such as coding rules and common errors, so that reviews can focus on design and logic.

Do all alarms in legacy code have to be fixed?

Rarely. You set a baseline, deal with serious defects first, and forbid adding new ones in new code. The debt then shrinks as the code is changed.

Can code written with AI or by a subcontractor be analyzed?

Yes, in the same way. It is even one of the best ways to check its quality without reviewing everything.

How long does it take to get started?

A first assessment takes a few days. The TiCS proof of concept runs on your code, with your tools.

Measure your code quality with your own tools

A free TiCS proof of concept, installed and configured by a Precilog consultant, on a few applications of your choice.

Request a proof of concept →

TiCS  ·  Languages and integrations  ·  TiCS or SonarQube?  ·  Testwell CTC++  ·  Standards  ·  Learn