Skip to content

Commit 480a0e3

Browse files
avargitster
authored andcommitted
merge-file: refactor for subsequent memory leak fix
Refactor the code in builtin/merge-file.c to: * Use the initializer to zero out "mmfs", and use modern C syntax for the rest. * Refactor the the inner loop to use a variable and "if/else if" pattern followed by "return". This will make a change to change it to a "goto cleanup" pattern smaller. Signed-off-by: Ævar Arnfjörð Bjarmason <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent d90dafb commit 480a0e3

File tree

1 file changed

+14
-12
lines changed

1 file changed

+14
-12
lines changed

builtin/merge-file.c

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@ static int label_cb(const struct option *opt, const char *arg, int unset)
2525

2626
int cmd_merge_file(int argc, const char **argv, const char *prefix)
2727
{
28-
const char *names[3] = { NULL, NULL, NULL };
29-
mmfile_t mmfs[3];
30-
mmbuffer_t result = {NULL, 0};
31-
xmparam_t xmp = {{0}};
28+
const char *names[3] = { 0 };
29+
mmfile_t mmfs[3] = { 0 };
30+
mmbuffer_t result = { 0 };
31+
xmparam_t xmp = { 0 };
3232
int ret = 0, i = 0, to_stdout = 0;
3333
int quiet = 0;
3434
struct option options[] = {
@@ -71,21 +71,23 @@ int cmd_merge_file(int argc, const char **argv, const char *prefix)
7171

7272
for (i = 0; i < 3; i++) {
7373
char *fname;
74-
int ret;
74+
mmfile_t *mmf = mmfs + i;
7575

7676
if (!names[i])
7777
names[i] = argv[i];
7878

7979
fname = prefix_filename(prefix, argv[i]);
80-
ret = read_mmfile(mmfs + i, fname);
80+
81+
if (read_mmfile(mmf, fname))
82+
ret = -1;
83+
else if (mmf->size > MAX_XDIFF_SIZE ||
84+
buffer_is_binary(mmf->ptr, mmf->size))
85+
ret = error("Cannot merge binary files: %s",
86+
argv[i]);
87+
8188
free(fname);
8289
if (ret)
83-
return -1;
84-
85-
if (mmfs[i].size > MAX_XDIFF_SIZE ||
86-
buffer_is_binary(mmfs[i].ptr, mmfs[i].size))
87-
return error("Cannot merge binary files: %s",
88-
argv[i]);
90+
return ret;
8991
}
9092

9193
xmp.ancestor = names[1];

0 commit comments

Comments
 (0)