ci: fail memcheck when valgrind finds a definite leak
The "Combo / all" job runs `ctest -T memcheck` to catch leaks before a
merge, but valgrind findings never failed it. MEMORYCHECK_COMMAND_OPTIONS
had no --error-exitcode, so a leaking test process still exits 0; ctest
then lists the defects after the run and exits 0 itself, and the workflow
step only sees ctest's exit status. The jwe_gcm leak fixed in v3.6.2
(#347) went through CI this way. The jwt_cli count is unreliable for a
second reason: ctest gives valgrind one --log-file with no %p, so every
child started under --trace-children truncates it.
Fail the test when valgrind finds an error, which works per process and
so does not depend on the shared log:
* Add --error-exitcode=1 --errors-for-leak-kinds=definite to
MEMORYCHECK_COMMAND_OPTIONS. valgrind now exits non-zero on a
definite leak (not the noisier "possible" kind), failing that test.
* Add tests/valgrind-bats.supp and point MEMORYCHECK_SUPPRESSIONS_FILE
at it. The bats CLI test runs bash, mktemp, jq and other /usr/bin
programs under --trace-children, and those leak definitely in the CI
image; without the suppression jwt_cli would fail before any case
runs. The suppression is scoped to obj:/usr/bin/* so it can only
match the system programs installed there. libjwt's library, tools
and test binaries live in the build tree, never under /usr/bin, so a
leak in libjwt's own code can never match it and is still reported.
The suppressions path is seeded as a cache entry because include(CTest)
declares MEMORYCHECK_SUPPRESSIONS_FILE with an empty default; a plain
variable is dropped when CTest.cmake creates the cache entry and the
field would configure out empty.
Verified in the CI image (issue #366): with these options v3.6.1 jwe_gcm
fails on each backend with "Early exit with return value 1" and ctest
exits non-zero, v3.6.2 passes all unit-test suites, and jwt_cli passes
only with the suppressions file. CMake wiring confirmed locally under
both JSON backends (the generated DartConfiguration.tcl carries the new
options and the absolute suppressions path); valgrind itself cannot run
on the local macOS host, so the memcheck run is left to CI.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Signed-off-by: Ben Collins <bcollins@libjwt.io>
diff --git a/CMakeLists.txt b/CMakeLists.txt
index b3bd623..5fb4b3d 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -17,7 +17,19 @@
HOMEPAGE_URL ${LIBJWT_HOMEPAGE_URL}
LANGUAGES C)
-set(MEMORYCHECK_COMMAND_OPTIONS "-q --tool=memcheck --leak-check=yes --num-callers=50 --trace-children=yes --leak-check=full --track-origins=yes --gen-suppressions=all")
+# --error-exitcode makes valgrind exit non-zero per process when it finds an
+# error, so a leaking test fails the memcheck step. Without it ctest only lists
+# defects after the run and still exits 0, so CI never caught them.
+# --errors-for-leak-kinds=definite limits that to definite leaks (not the noisier
+# "possible" kind). The suppressions file silences the definite leaks in the
+# system binaries (bash, mktemp, jq, ...) that the bats CLI test runs under
+# --trace-children; it is scoped to /usr/bin only and cannot mask a libjwt leak.
+set(MEMORYCHECK_COMMAND_OPTIONS "-q --tool=memcheck --leak-check=yes --num-callers=50 --trace-children=yes --leak-check=full --track-origins=yes --gen-suppressions=all --error-exitcode=1 --errors-for-leak-kinds=definite")
+# Seed the cache entry before include(CTest), which declares this same variable
+# with an empty default (no FORCE) and would otherwise win: a plain variable is
+# dropped when CTest.cmake creates the cache entry, leaving the field empty.
+set(MEMORYCHECK_SUPPRESSIONS_FILE "${CMAKE_SOURCE_DIR}/tests/valgrind-bats.supp"
+ CACHE FILEPATH "Valgrind leak suppressions for the memcheck job")
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
add_compile_options(-Wall -Werror -Wextra -Wunused)
diff --git a/tests/valgrind-bats.supp b/tests/valgrind-bats.supp
new file mode 100644
index 0000000..2608656
--- /dev/null
+++ b/tests/valgrind-bats.supp
@@ -0,0 +1,20 @@
+# Valgrind leak suppressions for the memcheck job (ctest -T memcheck).
+#
+# The bats CLI test (jwt_cli) runs the shell and helper tools with
+# --trace-children=yes, so valgrind also traces bash, mktemp, jq and friends.
+# Those system binaries report definite leaks of their own, which would fail the
+# test for reasons that have nothing to do with libjwt. Suppress definite leaks
+# whose stack passes through a /usr/bin binary.
+#
+# This is deliberately scoped to obj:/usr/bin/* so it can ONLY match the system
+# programs installed there. libjwt's library, tools and test binaries live in
+# the build tree, never under /usr/bin, so a leak in libjwt's own code can never
+# match this suppression and is still reported.
+
+{
+ leaks-in-system-binaries-run-by-bats
+ Memcheck:Leak
+ match-leak-kinds: definite
+ ...
+ obj:/usr/bin/*
+}