Ground Embedded KO Subscribe

Notes / Firmware Design & Quality

Static analysis: finding bugs before running

Turn compiler warnings all the way up, and find defects without executing the code using cppcheck and clang-tidy.

GROUND EMBEDDED · NOTEFirmware Design & Quality 05Static analysis 1volatile uint32_t *ODR 2 = (uint32_t*)0x40020014; 3*ODR |= (1u << 5); 4while (1) { 5 if (flag) toggle(); 6}
Compiler warningsStatic analysisUnit testsBoard testsThe field
The later a bug is found, the more it costs. The further left you catch it, the better.

Stage 1: compiler warnings

-Wall -Wextra -Wshadow -Wconversion -Wdouble-promotion -Werror
  • -Wconversion: implicit narrowing conversions (int → uint8_t)
  • -Wdouble-promotion: float calculations accidentally becoming double (very slow on an M4, whose FPU is single precision only)
  • -Werror: turns warnings into errors so they cannot pile up

Keeping zero warnings is by itself the cheapest form of quality control.

Stage 2: static analysis tools

Tool Characteristics
cppcheck Free; null pointers, array bounds, uninitialised variables
clang-tidy Free; many rules, automatic fixes
PC-lint / Polyspace / QAC Commercial; MISRA, automotive certification projects
RoadmapSee where this note sits in Firmware Design & Quality→

What they catch

  • Use of an uninitialised variable
  • An index beyond array bounds
  • Paths where a null pointer may be dereferenced
  • Use after free, memory leaks
  • Unreachable code, conditions that are always true

Running it

Do not try to fix every warning at once. Set a baseline, keep new code at zero, and check automatically in CI. Suppress false positives with a comment giving the reason.

FREE PDFGet the 3 free ebooksIf you want to keep reading this topic as one book: BLDC motor control, serial communication and sensors at book length. PDFs are in Korean.Get them free →