Commit a78522637bcc7d3f4120a37fff0673e1bec74430

Stefan Sperling 2017-11-30T15:37:45

handle fdopen() failure in opentemp()

diff --git a/lib/diff.c b/lib/diff.c
index cc11a64..fdc0c13 100644
--- a/lib/diff.c
+++ b/lib/diff.c
@@ -35,6 +35,7 @@ opentemp(void)
 {
 	char name[PATH_MAX];
 	int fd;
+	FILE *f;
 
 	if (strlcpy(name, "/tmp/got.XXXXXXXX", sizeof(name)) >= sizeof(name))
 		return NULL;
@@ -44,7 +45,13 @@ opentemp(void)
 		return NULL;
 
 	unlink(name);
-	return (fdopen(fd, "w+"));
+	f = fdopen(fd, "w+");
+	if (f == NULL) {
+		close(fd);
+		return NULL;
+	}
+
+	return f;
 }
 
 const struct got_error *