# [PATCH] test-mktemp: plug memory and descriptor leaks

1 messages from 2025-11-18 to 2025-11-18. Participants: Jeff King.
Thread: https://gitlist.dev/t/64505

## Jeff King, 2025-11-18 12:21

Subject: [PATCH] test-mktemp: plug memory and descriptor leaks
Message-ID: <20251118122124.GA1117960@coredump.intra.peff.net>
URL: https://gitlist.dev/e/20251118122124.GA1117960%40coredump.intra.peff.net

```
We test xmkstemp() in our helper by just calling:

  xmkstemp(xstrdup(argv[1]));

This leaks both the copied string as well as the descriptor returned by
the function. In practice this isn't a big deal, since we immediately
exit the program, but:

  1. LSan will complain about the memory leak. The only reason we did
     not notice this in our leak-checking builds is that both of the
     callers in the test suite (both in t0070) pass a broken template
     (and expect failure). So the function calls die() before we can
     actually leak.

     But it's an accident waiting to happen if anybody adds a call which
     succeeds.

  2. Coverity complains about the descriptor leak. There's a long list
     of uninteresting or false positives in Coverity's results, but
     since we're here we might as well fix it, too.

I didn't bother adding a new test that triggers the leak. It's not even
in real production code, but just in the test-helper itself.

Signed-off-by: Jeff King <peff@peff.net>
---
I think the rewrite of xmkstemp() triggered Coverity to consider this a
"new" problem, even though it has been there for years. So not urgent,
but this is mostly just trying not to waste the brain cycles I spent
analyzing. :)

 t/helper/test-mktemp.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)

diff --git a/t/helper/test-mktemp.c b/t/helper/test-mktemp.c
index 2290688940..da195640a9 100644
--- a/t/helper/test-mktemp.c
+++ b/t/helper/test-mktemp.c
@@ -6,10 +6,16 @@
 
 int cmd__mktemp(int argc, const char **argv)
 {
+	char *template;
+	int fd;
+
 	if (argc != 2)
 		usage("Expected 1 parameter defining the temporary file template");
+	template = xstrdup(argv[1]);
 
-	xmkstemp(xstrdup(argv[1]));
+	fd = xmkstemp(template);
 
+	close(fd);
+	free(template);
 	return 0;
 }
-- 
2.52.0.292.gf04cec7acc

```
