Run Valgrind Memcheck in CI as a Merge Gate
Run Valgrind Memcheck on your unit tests in CI with exit codes, suppressions and XML output so a new leak or invalid access fails the build before it merges.

A memory bug found in review costs minutes, one found in a test run costs an hour, and one found in production costs a weekend. Valgrind Memcheck can sit in your continuous integration pipeline and turn the second kind into the first: every pull request runs the unit tests under Memcheck, and a new leak or invalid access fails the check. This post builds that gate for a C project with Valgrind 3.24 on Linux 6.12, covering the flags that make the exit code meaningful, the test selection that keeps run time sane, the suppression file, and the way to keep the gate from becoming something the team learns to override. Reading the reports is covered in the earlier posts in this series, and the cornerstone guide on Valgrind has the tool overview.
What the gate must do
A gate is only useful if it is reliable. It has to fail when something new is wrong, pass when nothing is, and give the author enough in the log to fix it without a second run. That leads to four requirements:
A deterministic exit code. Memcheck exits with the program's own status by default, so a leak does not fail the build unless you ask.
No unrelated noise. Known library issues must be suppressed, or every run fails and people stop reading.
A usable log. The full report must be kept as a build artifact, since the terminal output scrolls away.
A bounded run time. Memcheck is many times slower than native code, so the gate runs only what is worth the cost.
The flags
These options give a gate with a clear contract:
valgrind \
--error-exitcode=99 \
--leak-check=full \
--errors-for-leak-kinds=definite,possible \
--track-origins=yes \
--num-callers=30 \
--suppressions=ci/valgrind.supp \
--log-file=valgrind-%p.log \
./build/tests/unit_testsWhat each does:
| Flag | Purpose in CI |
|---|---|
--error-exitcode=99 | If Memcheck reports any error, the process exits with 99, a value that your test program never uses, so a failure is distinguishable |
--leak-check=full | Report each leak with its allocation stack |
--errors-for-leak-kinds=definite,possible | Count definite and possible leaks as errors; ignore "still reachable" |
--track-origins=yes | Show where an uninitialised value came from, which saves a rerun when a test fails |
--num-callers=30 | Longer stacks help in deep call chains such as test frameworks |
--suppressions=... | Hide known issues from third-party libraries |
--log-file=valgrind-%p.log | Write each process's report to a file named by pid, which you upload as an artifact |
The exit-code rule needs one detail. With --error-exitcode, the program's own exit status is replaced only when Valgrind found errors; otherwise the program's status passes through, so a failing test still fails the job. So one command covers both "tests failed" and "memory errors". If you must tell them apart in the log, choose a code that tests never return and report it in the job output.
The Valgrind manual lists the options in full, including --error-limit and --max-stackframe, which you rarely need.
Build for the check
Valgrind needs symbols and works best on lightly optimized code. A separate build for the gate avoids touching the release build:
cmake -S . -B build-vg \
-DCMAKE_BUILD_TYPE=Debug \
-DCMAKE_C_FLAGS="-g -O1 -fno-omit-frame-pointer"
cmake --build build-vg --parallel-O1 keeps the stacks close to the source while avoiding the extreme slowness of -O0 on long loops. Do not combine Valgrind with AddressSanitizer in one binary; they conflict because both replace the allocator. Run the sanitizer build as a separate job. The post comparing Valgrind and AddressSanitizer says which finds what.
A CI job
A GitHub Actions job that does the whole thing, with the log kept as an artifact, looks like this:
name: memcheck
on:
pull_request:
jobs:
valgrind:
runs-on: ubuntu-24.04
steps:
- uses: actions/checkout@v4
- name: Install tools
run: sudo apt-get update && sudo apt-get install -y valgrind cmake gcc
- name: Build
run: |
cmake -S . -B build-vg -DCMAKE_BUILD_TYPE=Debug -DCMAKE_C_FLAGS="-g -O1"
cmake --build build-vg --parallel
- name: Memcheck
run: |
valgrind --error-exitcode=99 --leak-check=full \
--errors-for-leak-kinds=definite,possible \
--track-origins=yes --suppressions=ci/valgrind.supp \
--log-file=valgrind-%p.log \
./build-vg/tests/unit_tests
- name: Keep the logs
if: always()
uses: actions/upload-artifact@v4
with:
name: valgrind-logs
path: valgrind-*.logThe if: always() on the last step uploads the log even when Memcheck failed, which is exactly when you need it. Ubuntu 24.04's package carries Valgrind 3.22, so if you need the 3.24 behavior described here, build Valgrind from the release tarball in an earlier step and cache it. Other CI systems follow the same four steps: build, run with the exit code, keep the logs, fail on non-zero.
Make the run time bounded
Memcheck makes code run many times slower and the run time grows with how much code the tests exercise. Three decisions keep the gate fast:
Run unit tests, not integration tests. Unit tests cover the code you change, with small inputs. A test that processes a 500 MB file belongs in a nightly job.
Shard. If the suite takes too long, split it by test binary and run the shards in parallel jobs. Each Valgrind process is independent.
Use a timeout. Give the job a
timeout-minuteslimit, so a hang under Valgrind, which is slower to detect, does not block the queue for hours.
For multi-threaded tests, add --fair-sched=yes, which makes Valgrind's thread scheduler fair and prevents some tests from stalling when one thread spins waiting for another.
Keep the gate honest
A gate that fails for noise gets ignored, and one that is easy to bypass is worth little. Four practices keep it reliable.
Start green. Before enabling the gate, run it on the main branch, fix or suppress what it finds, and only then require it on pull requests. A gate that is red on day one is turned off on day two.
Review suppression changes. The file
ci/valgrind.suppis code. A pull request that adds an entry needs a reviewer who reads it, because a broad entry hides future bugs. The post on suppression files explains how to keep entries narrow.Never raise the exit code threshold to pass. If a test becomes flaky under Valgrind, find out why. Often it is an uninitialised value or a race that real users hit rarely.
Pin the Valgrind version. A new release may report new things in the same code. Upgrade deliberately, in its own pull request, and fix what appears.
What the gate does not cover
Memcheck sees only code the tests execute. A leak on an error path that no test triggers will not be found, which is the reason the earlier post recommends invalid-input tests. It does not find data races, since that is Helgrind's or ThreadSanitizer's job, and it does not catch logic errors. Treat it as one gate among several: it is excellent at the memory bugs of C and C++, and it says nothing about the rest of correctness.


