Commit 202329ae1b4393a9093c61915de94a5a9a51555a

Stefan Sperling 2019-08-11T20:11:56

fix some more occurrences of potential errno clobbering

diff --git a/lib/buf.c b/lib/buf.c
index 32d933f..eb235b0 100644
--- a/lib/buf.c
+++ b/lib/buf.c
@@ -294,10 +294,11 @@ buf_write(BUF *b, const char *path, mode_t mode)
 	int fd;
  open:
 	if ((fd = open(path, O_WRONLY|O_CREAT|O_TRUNC, mode)) == -1) {
+		err = got_error_from_errno2("open", path);
 		if (errno == EACCES && unlink(path) != -1)
 			goto open;
 		else
-			return got_error_from_errno2("open", path);
+			return err;
 	}
 
 	if (buf_write_fd(b, fd) == -1) {
diff --git a/lib/commit_graph.c b/lib/commit_graph.c
index 05aa9d3..9b907af 100644
--- a/lib/commit_graph.c
+++ b/lib/commit_graph.c
@@ -404,8 +404,9 @@ got_commit_graph_open(struct got_commit_graph **graph,
 
 	*graph = alloc_graph(path);
 	if (*graph == NULL) {
+		err = got_error_from_errno("alloc_graph");
 		got_object_commit_close(commit);
-		return got_error_from_errno("alloc_graph");
+		return err;
 	}
 
 	if (first_parent_traversal)
diff --git a/lib/path.c b/lib/path.c
index a8c3780..8ae1c96 100644
--- a/lib/path.c
+++ b/lib/path.c
@@ -401,6 +401,7 @@ got_path_strip_trailing_slashes(char *path)
 const struct got_error *
 got_path_find_prog(char **filename, const char *prog)
 {
+	const struct got_error *err = NULL;
 	char *p;
 	int len;
 	struct stat sbuf;
@@ -436,8 +437,9 @@ got_path_find_prog(char **filename, const char *prog)
 			p[--len] = '\0';	/* strip trailing '/' */
 
 		if (asprintf(filename, "%s/%s", p, prog) == -1) {
+			err = got_error_from_errno("asprintf");
 			free(path);
-			return got_error_from_errno("asprintf");
+			return err;
 		}
 		if ((stat(*filename, &sbuf) == 0) && S_ISREG(sbuf.st_mode) &&
 		    access(*filename, X_OK) == 0) {