do not leak open file in diff3.c's readin() error paths
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
diff --git a/lib/diff3.c b/lib/diff3.c
index 5532125..cf1c35d 100644
--- a/lib/diff3.c
+++ b/lib/diff3.c
@@ -587,12 +587,12 @@ readin(size_t *n, char *name, struct diff **dd, struct diff3_state *d3s)
return got_error_from_errno2("fopen", name);
err = getchange(&p, f, d3s);
if (err)
- return err;
+ goto done;
for (i = 0; p; i++) {
if (i >= d3s->szchanges - 1) {
err = increase(d3s);
if (err)
- return err;
+ goto done;
}
a = b = number(&p);
if (*p == ',') {
@@ -618,18 +618,18 @@ readin(size_t *n, char *name, struct diff **dd, struct diff3_state *d3s)
err = getchange(&p, f, d3s);
if (err)
- return err;
+ goto done;
}
if (i) {
(*dd)[i].old.from = (*dd)[i - 1].old.to;
(*dd)[i].new.from = (*dd)[i - 1].new.to;
}
-
- if (fclose(f) != 0)
+done:
+ if (fclose(f) != 0 && err == NULL)
err = got_error_from_errno("fclose");
-
- *n = i;
+ if (err == NULL)
+ *n = i;
return err;
}