From 8c34ab4f4efd791087668d8700a314e3752e01ad Mon Sep 17 00:00:00 2001 From: Fabio Scotto di Santolo Date: Wed, 26 Aug 2026 16:19:50 +0200 Subject: [PATCH] Add a make valgrind target Runs the existing suite with every fsnap invocation under memcheck, rather than duplicating a list of commands in the Makefile: tests/run.sh already funnels each call through fsnap(), so a command prefix is all it takes. VALGRIND is empty for a plain `make test`. -q keeps stderr clean, which matters because several tests compare it, and --error-exitcode=99 turns a finding into a failing test instead of a note nobody reads. The per-invocation timeout goes to 300s, since memcheck costs an order of magnitude: the whole suite takes about 25 seconds against a fraction of a second without it. Checked the same way as the suite itself. Dropping the free() of the getline buffer in list keeps `make test` green at 91 passed while `make valgrind` fails, which is the whole point of having it. Replacing the calloc in RecordObjectNew with malloc is caught too, and named precisely: free() on an uninitialised value at types.c:420. Baseline green either side, and the missing-valgrind guard exits 2 with a clear message. Co-Authored-By: Claude Opus 5 Claude-Session: https://claude.ai/code/session_018KyCHPAxWzqa3vHKdXJkgx --- Makefile | 13 ++++++++++++- README.md | 5 +++++ tests/run.sh | 15 ++++++++++++--- 3 files changed, 29 insertions(+), 4 deletions(-) diff --git a/Makefile b/Makefile index 9677169..47180f2 100644 --- a/Makefile +++ b/Makefile @@ -45,7 +45,7 @@ TESTS := tests/run.sh OBJECTS := $(SOURCES:%.c=$(BUILD_DIR)/%.o) DEPENDENCIES := $(OBJECTS:.o=.d) -.PHONY: all clean run test install uninstall +.PHONY: all clean run test valgrind install uninstall all: $(TARGET) @@ -63,6 +63,17 @@ run: $(TARGET) test: $(TARGET) @FSNAP='$(CURDIR)/$(TARGET)' sh $(TESTS) +# The same suite with every invocation under memcheck. -q keeps stderr clean, +# which matters because several tests compare it; a distinct exit code turns any +# finding into a failed test rather than a note nobody reads. +MEMCHECK := valgrind -q --error-exitcode=99 --leak-check=full \ + --errors-for-leak-kinds=all --track-origins=yes + +valgrind: $(TARGET) + @command -v valgrind >/dev/null 2>&1 || { \ + echo 'valgrind is not installed' >&2; exit 2; } + @FSNAP='$(CURDIR)/$(TARGET)' VALGRIND='$(MEMCHECK)' sh $(TESTS) + install: $(TARGET) $(INSTALL) -d $(DESTDIR)$(BINDIR) $(INSTALL) -m 755 $(TARGET) $(DESTDIR)$(BINDIR)/$(NAME) diff --git a/README.md b/README.md index a0e7730..003e5d7 100644 --- a/README.md +++ b/README.md @@ -48,6 +48,11 @@ parser cases from §14 and §15 and the `diff` classifications. It is POSIX sh with no dependencies, and skips rather than fails what the environment cannot provide. +`make valgrind` runs that same suite with every invocation under memcheck, +which takes about 25 seconds against a fraction of a second for `make test`. It +reports a leak or an invalid access as a failed test rather than as a note in +the output, and exits 2 if valgrind is not installed. + `make install` copies the binary to `$(BINDIR)`, which defaults to `/usr/local/bin`; `make uninstall` removes it again. Both honour `DESTDIR` for staged installs: diff --git a/tests/run.sh b/tests/run.sh index e472ff0..92c9f20 100755 --- a/tests/run.sh +++ b/tests/run.sh @@ -23,19 +23,28 @@ if [ ! -x "$FSNAP" ]; then exit 2 fi +# Set by `make valgrind` to a memcheck command line. Running the whole suite +# under a checker is then just a matter of prefixing every invocation, which is +# what fsnap() below is for. It stays empty for a plain `make test`. +VALGRIND=${VALGRIND:-} + # A bug in the walker can make create block forever rather than fail -- opening # a FIFO waits for a writer that never comes. Cap every invocation so that turns # into a failing test instead of a hung suite. timeout(1) is not in POSIX, so # fall back to running unguarded where it is missing. if command -v timeout >/dev/null 2>&1; then - TIMEOUT="timeout 30" + if [ -n "$VALGRIND" ]; then + TIMEOUT="timeout 300" # memcheck costs an order of magnitude + else + TIMEOUT="timeout 30" + fi else TIMEOUT="" fi fsnap() { - # shellcheck disable=SC2086 # deliberate split: TIMEOUT is a command prefix - $TIMEOUT "$FSNAP" "$@" + # shellcheck disable=SC2086 # deliberate split: both are command prefixes + $TIMEOUT $VALGRIND "$FSNAP" "$@" } testno=0