Skip to content

Commit bc235a6

Browse files
peffgitster
authored andcommitted
test-delta: handle errors with die()
This is a short test helper that does all of its work in the main function. When we encounter an error, we try to clean up memory and descriptors and then jump to an error return, which exits the program. We can get the same effect by just calling die(), which means we do not have to bother with cleaning up. This simplifies the code, and also removes some inconsistencies where a few code paths forgot to clean up descriptors (though in practice it was not a big deal since we were exiting anyway). In addition to die() and die_errno(), we'll also use a few of our usual helpers like xopen() and usage() that make things more ergonomic. This does change the exit code in these cases from 1 to 128, but I don't think it matters (and arguably is better, as we'd already exit 128 for other errors like xmalloc() failure). Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 8cc1925 commit bc235a6

File tree

1 file changed

+18
-37
lines changed

1 file changed

+18
-37
lines changed

t/helper/test-delta.c

Lines changed: 18 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -21,39 +21,26 @@ int cmd__delta(int argc, const char **argv)
2121
struct stat st;
2222
void *from_buf = NULL, *data_buf = NULL, *out_buf = NULL;
2323
unsigned long from_size, data_size, out_size;
24-
int ret = 1;
2524

26-
if (argc != 5 || (strcmp(argv[1], "-d") && strcmp(argv[1], "-p"))) {
27-
fprintf(stderr, "usage: %s\n", usage_str);
28-
return 1;
29-
}
25+
if (argc != 5 || (strcmp(argv[1], "-d") && strcmp(argv[1], "-p")))
26+
usage(usage_str);
3027

31-
fd = open(argv[2], O_RDONLY);
32-
if (fd < 0 || fstat(fd, &st)) {
33-
perror(argv[2]);
34-
return 1;
35-
}
28+
fd = xopen(argv[2], O_RDONLY);
29+
if (fstat(fd, &st) < 0)
30+
die_errno("fstat(%s)", argv[2]);
3631
from_size = st.st_size;
3732
from_buf = xmalloc(from_size);
38-
if (read_in_full(fd, from_buf, from_size) < 0) {
39-
perror(argv[2]);
40-
close(fd);
41-
goto cleanup;
42-
}
33+
if (read_in_full(fd, from_buf, from_size) < 0)
34+
die_errno("read(%s)", argv[2]);
4335
close(fd);
4436

45-
fd = open(argv[3], O_RDONLY);
46-
if (fd < 0 || fstat(fd, &st)) {
47-
perror(argv[3]);
48-
goto cleanup;
49-
}
37+
fd = xopen(argv[3], O_RDONLY);
38+
if (fstat(fd, &st) < 0)
39+
die_errno("fstat(%s)", argv[3]);
5040
data_size = st.st_size;
5141
data_buf = xmalloc(data_size);
52-
if (read_in_full(fd, data_buf, data_size) < 0) {
53-
perror(argv[3]);
54-
close(fd);
55-
goto cleanup;
56-
}
42+
if (read_in_full(fd, data_buf, data_size) < 0)
43+
die_errno("read(%s)", argv[3]);
5744
close(fd);
5845

5946
if (argv[1][1] == 'd')
@@ -64,22 +51,16 @@ int cmd__delta(int argc, const char **argv)
6451
out_buf = patch_delta(from_buf, from_size,
6552
data_buf, data_size,
6653
&out_size);
67-
if (!out_buf) {
68-
fprintf(stderr, "delta operation failed (returned NULL)\n");
69-
goto cleanup;
70-
}
54+
if (!out_buf)
55+
die("delta operation failed (returned NULL)");
7156

72-
fd = open (argv[4], O_WRONLY|O_CREAT|O_TRUNC, 0666);
73-
if (fd < 0 || write_in_full(fd, out_buf, out_size) < 0) {
74-
perror(argv[4]);
75-
goto cleanup;
76-
}
57+
fd = xopen(argv[4], O_WRONLY|O_CREAT|O_TRUNC, 0666);
58+
if (write_in_full(fd, out_buf, out_size) < 0)
59+
die_errno("write(%s)", argv[4]);
7760

78-
ret = 0;
79-
cleanup:
8061
free(from_buf);
8162
free(data_buf);
8263
free(out_buf);
8364

84-
return ret;
65+
return 0;
8566
}

0 commit comments

Comments
 (0)