From: Jeff King Date: Thu, 24 Jul 2025 00:03:08 +0000 (-0400) Subject: test-delta: close output descriptor after use X-Git-Tag: v2.51.0-rc0~17^2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=0f1b33815b553dd457f8e38e3768b73cf9227082;p=thirdparty%2Fgit.git test-delta: close output descriptor after use After we write to the output file, the program exits. This naturally closes the descriptor. But we should do an explicit close for two reasons: 1. It's possible to hit an error on close(), which we should detect and report via our exit code. 2. Leaking descriptors is a bad practice in general. Even if it isn't meaningful here, it sets a bad example. It is tempting to write: if (write_in_full(fd, ...) < 0 || close(fd) < 0) die_errno(...); But that pattern contains a subtle problem that has resulted in descriptor leaks before. If write_in_full() fails, we'll short-circuit and never call close(), leaking the descriptor. That's not a problem here, since our error path dies instead of returning up the stack. But since we're trying to set a good example, let's write it out as two separate conditions. As a bonus, that lets us produce a slightly more specific error message. Signed-off-by: Jeff King Signed-off-by: Junio C Hamano --- diff --git a/t/helper/test-delta.c b/t/helper/test-delta.c index 7945793078..52ea00c937 100644 --- a/t/helper/test-delta.c +++ b/t/helper/test-delta.c @@ -45,6 +45,8 @@ int cmd__delta(int argc, const char **argv) fd = xopen(argv[4], O_WRONLY|O_CREAT|O_TRUNC, 0666); if (write_in_full(fd, out_buf, out_size) < 0) die_errno("write(%s)", argv[4]); + if (close(fd) < 0) + die_errno("close(%s)", argv[4]); strbuf_release(&from); strbuf_release(&data);