mailsplit: fix FILE* leak in split_maildir

If we encounter an error while splitting a maildir, we exit the function early, leaking the open filehandle. This isn't a big deal, since we exit the program soon after, but it's easy enough to be careful. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Sep 24, 2015 at 17:03 UTC d270d7b7a2d631c3d11315f20bb0cf15e438dafa
1 file changed +4 -1
builtin/mailsplit.c
+4 -1
@@ -150,6 +150,7 @@ static int split_maildir(const char *maildir, const char *dir,
150 {
151 char file[PATH_MAX];
152 char name[PATH_MAX];
153 + FILE *f = NULL;
154 int ret = -1;
155 int i;
156 struct string_list list = STRING_LIST_INIT_DUP;
@@ -160,7 +161,6 @@ static int split_maildir(const char *maildir, const char *dir,
161 goto out;
162
163 for (i = 0; i < list.nr; i++) {
163 - FILE *f;
164 snprintf(file, sizeof(file), "%s/%s", maildir, list.items[i].string);
165 f = fopen(file, "r");
166 if (!f) {
@@ -177,10 +177,13 @@ static int split_maildir(const char *maildir, const char *dir,
177 split_one(f, name, 1);
178
179 fclose(f);
180 + f = NULL;
181 }
182
183 ret = skip;
184 out:
185 + if (f)
186 + fclose(f);
187 string_list_clear(&list, 1);
188 return ret;
189 }