Catalogue / cpp-static-audit
cpp-static-audit
Older skill: source not recordedSignedAudits C and C++ code with clang-tidy, cppcheck, and scan-build. Builds a compilation database, picks check families, writes a project .clang-tidy file, suppresses false positives, and adds analysis steps to CI. Use when the user asks which analyzer checks to enable, reports noisy static-analysis wa…--- name: cpp-static-audit description: > Audits C and C++ code with clang-tidy, cppcheck, and scan-build. Builds a compilation database, picks check families, writes a project .clang-tidy file, suppresses false positives, and adds analysis steps to CI. Use when the user asks which analyzer checks to enable, reports noisy static-analysis warnings, wants to harden C or C++ code, or needs to triage clang-tidy, cppcheck, or scan-build output. Not for runtime sanitizers, compiler warning flags, or languages other than C and C++. ---
C/C++ Static Audit
Scope
You hunt for defects in C and C++ source before the program ever runs. Three tools complement each other: clang-tidy (hundreds of named checks, many with automatic fixes), cppcheck (fast, works without build integration), and scan-build (simulates execution paths and catches bugs that span functions). This skill does not cover runtime sanitizers, compiler warning flags, or build-system configuration beyond producing the compilation database.
1. Produce the compilation database
clang-tidy needs the exact compile flags for each translation unit, which is what compile_commands.json provides.
- CMake project: configure with the export flag on; the database appears inside the build directory. Either symlink it to the repository root or point tools at the build directory with
-p. - Make or any custom build: record the build with an interception wrapper, or generate the database with
compiledb.
cmake -S . -B build -DCMAKE_EXPORT_COMPILE_COMMANDS=ON ln -s build/compile_commands.json . # Makefile-based alternative: bear -- make
If database generation fails on an exotic build system, fall back to cppcheck, which parses sources directly, and say so to the user.
2. First contact: one file, then the whole tree
Do not start with a full-project scan on an unfamiliar codebase — the output will be unmanageable. Prove the setup on a single translation unit first:
clang-tidy src/net/icmp_socket.c -- -std=c17 -Iinclude -D_GNU_SOURCE
Once that works, scale up with the parallel driver:
run-clang-tidy -p build -j8
To answer "what does check X say about this file", scope the run:
clang-tidy -checks='bugprone-*,performance-*' src/crypto/padding.cpp
3. Match check families to the goal
Ask the user (or infer from the request) which of these outcomes they want, then enable accordingly:
| Family | Catches | Typical finding | |---|---|---| | bugprone-* | Likely logic errors | sizeof applied to a pointer instead of the buffer, discarded error returns, identical if/else bodies | | clang-analyzer-* | Path-sensitive defects | Division by zero reachable on one branch, uninitialized reads, mismatched new/delete | | modernize-* | Pre-C++11 idioms | Macro NULL, missing override on virtuals, index loops a range-for replaces | | performance-* | Avoidable cost | Heavy copies a const reference would avoid, stream flushes inside hot loops | | cppcoreguidelines-* | Core Guidelines violations | goto, raw owning pointers, magic numbers | | cert-*, hicpp-* | Security and coding standards | Unchecked numeric conversions, insecure string APIs | | readability-* | Naming and clarity | Inconsistent identifier case, redundant casts |
Reasoning behind the default order: bugprone and clang-analyzer give the highest signal of real defects, so they come first. modernize and performance produce large stylistic diffs, so run them as a separate, planned pass. readability belongs last and should rarely block a build.
4. Pin the project's .clang-tidy file
Ad-hoc -checks flags get lost. Persist the decision at the repository root:
Checks: >
bugprone-*,
clang-analyzer-core.*,
clang-analyzer-unix.*,
clang-analyzer-security.*,
modernize-use-nullptr,
modernize-use-override,
performance-*,
-bugprone-easily-swappable-parameters,
-modernize-use-trailing-return-type
WarningsAsErrors: 'bugprone-*,clang-analyzer-core.*'
HeaderFilterRegex: '^(src|lib)/.*'
CheckOptions:
- key: readability-identifier-naming.ParameterCase
value: lower_caseExplain each line when you write one:
Checksis an allow-list; entries prefixed with-carve exceptions back out. The two disables above exist becauseeasily-swappable-parametersflags nearly every stable C API by design, and trailing return types are a style choice.WarningsAsErrorsshould stay narrow — only defect-class families, never style families.HeaderFilterRegexkeeps diagnostics limited to first-party code so vendored headers do not flood the output.
5. Triage findings and silence what is noise
Work through the report in this order of preference:
- The finding is real — fix the code.
- The finding is a false positive on a specific line — suppress that check on that line, naming the check so the suppression is auditable later.
- The same suppression repeats everywhere — disable the check project-wide in
.clang-tidyand leave a comment on why.
Line and block suppression mechanisms:
// NOLINTNEXTLINE(cppcoreguidelines-avoid-magic-numbers) static const uint16_t MSS_DEFAULT = 1460; // Named single-line suppression: ssize_t n = read_tunnel(tunnel); // NOLINT(bugprone-narrowing-conversions) // Region suppression: // NOLINTBEGIN(cert-err34-c) legacy_atoi(s); // NOLINTEND(cert-err34-c)
Prefer a named check over a bare comment-only suppression; unnamed suppressions hide whatever check fires there and rot silently.
6. Run cppcheck for a second opinion
cppcheck often catches things clang-tidy misses and vice versa:
cppcheck --enable=warning,performance,portability \
--suppress=missingIncludeSystem \
--error-exitcode=1 \
src/
# Reuse the compilation database when present:
cppcheck --project=build/compile_commands.json
# Machine-readable report for CI tooling:
cppcheck --xml --xml-version=2 src/ 2> cppcheck.xml--enable= levels and what they cover:
| Level | Covers | |---|---| | warning | Undefined behavior, suspicious logic | | style | Redundant or dead constructs | | performance | Inefficient patterns | | portability | Constructs that break across platforms or compilers | | information | Notes about the analysis configuration itself | | all | Everything above |
Two flags worth knowing: --inline-suppr makes cppcheck honor // cppcheck-suppress <checkId> comments in source, and --error-exitcode turns findings into a nonzero exit so CI can react. The missingIncludeSystem suppression skips complaints about system headers you do not control.
7. Go deeper with scan-build
When a bug is suspected but neither tool finds it, wrap the build in the path-sensitive analyzer:
scan-build make scan-build cmake --build build/ # Browse the generated HTML report: scan-view /tmp/scan-build-*
Extra checkers can be switched on individually:
scan-build -enable-checker security.insecureAPI.getpw \
-enable-checker alpha.core.CastSize \
makeThis is the tool for use-after-free that crosses function boundaries, dead stores caused by tangled logic, and null dereferences that only one interleaving of branches reaches. It is slow, so run it on demand or nightly rather than on every commit.
8. Wire the tools into CI
A minimal job for a GitHub Actions workflow:
- name: clang-tidy
run: |
cmake -S . -B build -DCMAKE_EXPORT_COMPILE_COMMANDS=ON
run-clang-tidy -p build -j8 -warnings-as-errors 'bugprone-*,clang-analyzer-core.*'
- name: cppcheck
run: cppcheck --enable=warning,performance --suppress=missingIncludeSystem --error-exitcode=1 src/Failing the build only on defect-class families is deliberate: style families flood a first run on an established repository. For a brownfield codebase, start the job as non-blocking until the warning backlog is burned down, then tighten the gate.
Confirmation points and hard limits
- Never launch fix mode across the whole repository on your own initiative. Auto-fix rewrites source files wholesale; before any bulk fix run, tell the user which checks will apply fixes, confirm the working tree is clean so every change lands in a reviewable diff, and get an explicit go-ahead.
- Do not commit or push CI workflow changes without showing the user the exact file first — CI configuration is shared infrastructure and its failure mode affects everyone on the branch.
- Never silence a finding by deleting code, and never disable a check just to make a report or pipeline green; every suppression must name the check it mutes and the reason.
- Verify a finding against the real code path before calling it a bug — "fixing" a false positive can inject a genuine defect.
- Do not edit vendored or third-party code to satisfy an analyzer; exclude it with header filtering instead.
| File | Size | SHA-256 |
|---|---|---|
| SKILL.md | 8.9 kB | e19ad464062d |
| examples.json | 2.1 kB | 3c0c9753419d |
Published verbatim from the pinned commit. Nothing is rewritten.
| Version | Pinned commit | Signed | |
|---|---|---|---|
| 1.0.0 Current | Not recorded | 6 Oct 2026 |
Locking a skill to one version needs Pro.