Skip to content

Commit 0f1b338

Browse files
peffgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 760dd80 commit 0f1b338

File tree

1 file changed

+2
-0
lines changed

1 file changed

+2
-0
lines changed

t/helper/test-delta.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ int cmd__delta(int argc, const char **argv)
4545
fd = xopen(argv[4], O_WRONLY|O_CREAT|O_TRUNC, 0666);
4646
if (write_in_full(fd, out_buf, out_size) < 0)
4747
die_errno("write(%s)", argv[4]);
48+
if (close(fd) < 0)
49+
die_errno("close(%s)", argv[4]);
4850

4951
strbuf_release(&from);
5052
strbuf_release(&data);

0 commit comments

Comments
 (0)