{"thread":{"id":"64505","subject":"[PATCH] test-mktemp: plug memory and descriptor leaks","startedAt":"2025-11-18T12:21:25Z","lastAt":"2025-11-18T12:21:25Z","messageCount":1,"participants":["Jeff King"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"530902","messageId":"20251118122124.GA1117960@coredump.intra.peff.net","threadId":"64505","inReplyTo":null,"subject":"[PATCH] test-mktemp: plug memory and descriptor leaks","fromName":"Jeff King","fromEmail":"peff@peff.net","sentAt":"2025-11-18T12:21:24Z","receivedAt":"2025-11-18T12:21:25Z","isPatch":true,"sender":{"key":"peff@peff.net","avatar":"https://avatars.githubusercontent.com/u/45925?v=4"},"body":"We test xmkstemp() in our helper by just calling:\n\n  xmkstemp(xstrdup(argv[1]));\n\nThis leaks both the copied string as well as the descriptor returned by\nthe function. In practice this isn't a big deal, since we immediately\nexit the program, but:\n\n  1. LSan will complain about the memory leak. The only reason we did\n     not notice this in our leak-checking builds is that both of the\n     callers in the test suite (both in t0070) pass a broken template\n     (and expect failure). So the function calls die() before we can\n     actually leak.\n\n     But it's an accident waiting to happen if anybody adds a call which\n     succeeds.\n\n  2. Coverity complains about the descriptor leak. There's a long list\n     of uninteresting or false positives in Coverity's results, but\n     since we're here we might as well fix it, too.\n\nI didn't bother adding a new test that triggers the leak. It's not even\nin real production code, but just in the test-helper itself.\n\nSigned-off-by: Jeff King <peff@peff.net>\n---\nI think the rewrite of xmkstemp() triggered Coverity to consider this a\n\"new\" problem, even though it has been there for years. So not urgent,\nbut this is mostly just trying not to waste the brain cycles I spent\nanalyzing. :)\n\n t/helper/test-mktemp.c | 8 +++++++-\n 1 file changed, 7 insertions(+), 1 deletion(-)\n\ndiff --git a/t/helper/test-mktemp.c b/t/helper/test-mktemp.c\nindex 2290688940..da195640a9 100644\n--- a/t/helper/test-mktemp.c\n+++ b/t/helper/test-mktemp.c\n@@ -6,10 +6,16 @@\n \n int cmd__mktemp(int argc, const char **argv)\n {\n+\tchar *template;\n+\tint fd;\n+\n \tif (argc != 2)\n \t\tusage(\"Expected 1 parameter defining the temporary file template\");\n+\ttemplate = xstrdup(argv[1]);\n \n-\txmkstemp(xstrdup(argv[1]));\n+\tfd = xmkstemp(template);\n \n+\tclose(fd);\n+\tfree(template);\n \treturn 0;\n }\n-- \n2.52.0.292.gf04cec7acc\n"}]}