Run static analysis on OCaml C stubs in the CI #6338
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Similar to the static analyzer introduced in xha, but for OCaml C stubs.
This uses my https://ocaml.org/p/dune-compiledb/latest to product a
compile_commands.json
for the C stubs out of thedune
rules (which can also be useful if you want to useclangd
and get some editor integration about compiler warnings). This requires installing enough of the build dependencies to be able to rundune
successfully (perhaps in the future that restriction can be removed). Caching is used though, and we only need to install the build deps whendune
files change, otherwise we can reuse a cachedcompile_commands.json
.Static analyzers, like CodeChecker support reading
compile_commands.json
and invoking static analyzers with the appropriate flags to preprocess, and analyze the C source code. We useclang
,clang-tidy
andcppcheck
as the default analyzers, although more analyzers could be added in the future (CodeChecker supports convertinggcc -fanalyzer
output for example. GCC also supports emiting SARIF format directly, but github cannot parse it, because it doesn't implement the full SARIF spec).At the end we convert the results back to the standard SARIF format that Github also supports for its code scanning results, which will make it automatically add comments on PRs that introduce new bugs, without necessarily gating on them.
I fixed some of the most obvious warnings, and suppressed some minor ones that we cannot fix (where the warning is caused by a Xen or OCaml header). More warnings can be skipped by adding to
.codechecker.json
if needed.So far it seems to have found a file descriptor leak in
unixpwd.c
on an error path, but I haven't gone through all the reports in detail yet.