Commit Diff


commit - 078e971d4f71f224136916ca245ed368072f3c05
commit + 0d26ce361e0d50e331fac03e817a72a4d1edcfe7
blob - 27eab4c76697959df3eabaa6437bcc6ccec0fe44
blob + c8e773f37bd9412ed0428d83db0624e1f358ba84
--- lib/diff3.c
+++ lib/diff3.c
@@ -85,6 +85,10 @@
 #include "got_lib_diff.h"
 #include "worklist.h"
 
+#ifndef nitems
+#define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
+#endif
+
 /* flags shared between merge(1) and rcsmerge(1) */
 #define MERGE_EFLAG     (1<<16)
 #define MERGE_OFLAG     (1<<17)
@@ -266,6 +270,7 @@ merge_diff3(BUF **buf, char **av, int flags)
 	size_t dlen, plen;
 	struct wklhead temp_files;
 	struct diff3_state *d3s;
+	int i;
 
 	*buf = NULL;
 
@@ -391,6 +396,11 @@ out:
 
 	worklist_clean(&temp_files, worklist_unlink);
 
+	for (i = 0; i < nitems(d3s->fp); i++) {
+		if (d3s->fp[i])
+			fclose(d3s->fp[i]);
+	}
+	free(d3s);
 	if (err == NULL)
 		*buf = diffb;
 	return err;
@@ -420,7 +430,6 @@ diff3_internal(char *dp13, char *dp23, char *path1, ch
 	if ((n = readin(dp23, &d3s->d23, d3s)) < 0)
 		return got_error_from_errno();
 
-	/* XXX LEAK: at present we never close these files! */
 	if ((d3s->fp[0] = fopen(path1, "r")) == NULL)
 		return got_error_from_errno();
 	if ((d3s->fp[1] = fopen(path2, "r")) == NULL)