Commit 08578a35f60be8657db97b705f27a55ab61850c8

Stefan Sperling 2021-01-22T11:05:05

make close(2) failure checks consistent; check 'close() == -1' everywhere ok millert, naddy

diff --git a/lib/buf.c b/lib/buf.c
index 3986b23..b26da2a 100644
--- a/lib/buf.c
+++ b/lib/buf.c
@@ -303,7 +303,7 @@ buf_write(BUF *b, const char *path, mode_t mode)
 	if (fchmod(fd, mode) < 0)
 		err = got_error_from_errno2("fchmod", path);
 
-	if (close(fd) != 0 && err == NULL)
+	if (close(fd) == -1 && err == NULL)
 		err = got_error_from_errno2("close", path);
 
 	return err;
@@ -328,7 +328,7 @@ buf_write_stmp(BUF *b, char *template)
 		(void)unlink(template);
 	}
 
-	if (close(fd) != 0 && err == NULL)
+	if (close(fd) == -1 && err == NULL)
 		err = got_error_from_errno("close");
 
 	return err;
diff --git a/lib/fetch.c b/lib/fetch.c
index 134a933..bc5756e 100644
--- a/lib/fetch.c
+++ b/lib/fetch.c
@@ -557,7 +557,7 @@ got_fetch_pack(struct got_object_id **pack_hash, struct got_pathlist_head *refs,
 		    GOT_PATH_PROG_FETCH_PACK, tmppackpath);
 	}
 
-	if (close(imsg_fetchfds[1]) != 0) {
+	if (close(imsg_fetchfds[1]) == -1) {
 		err = got_error_from_errno("close");
 		goto done;
 	}
@@ -732,7 +732,7 @@ got_fetch_pack(struct got_object_id **pack_hash, struct got_pathlist_head *refs,
 	} else if (idxpid == 0)
 		got_privsep_exec_child(imsg_idxfds,
 		    GOT_PATH_PROG_INDEX_PACK, tmppackpath);
-	if (close(imsg_idxfds[1]) != 0) {
+	if (close(imsg_idxfds[1]) == -1) {
 		err = got_error_from_errno("close");
 		goto done;
 	}
diff --git a/lib/lockfile.c b/lib/lockfile.c
index bc4cf86..71a5d73 100644
--- a/lib/lockfile.c
+++ b/lib/lockfile.c
@@ -82,7 +82,7 @@ got_lockfile_unlock(struct got_lockfile *lf)
 
 	if (lf->path && lf->fd != -1 && unlink(lf->path) != 0)
 		err = got_error_from_errno("unlink");
-	if (lf->fd != -1 && close(lf->fd) != 0 && err == NULL)
+	if (lf->fd != -1 && close(lf->fd) == -1 && err == NULL)
 		err = got_error_from_errno("close");
 	free(lf->path);
 	free(lf->locked_path);
diff --git a/lib/object.c b/lib/object.c
index 7fe404a..b85e765 100644
--- a/lib/object.c
+++ b/lib/object.c
@@ -237,7 +237,7 @@ start_pack_privsep_child(struct got_pack *pack, struct got_packidx *packidx)
 		/* not reached */
 	}
 
-	if (close(imsg_fds[1]) != 0)
+	if (close(imsg_fds[1]) == -1)
 		return got_error_from_errno("close");
 	pack->privsep_child->imsg_fd = imsg_fds[0];
 	pack->privsep_child->pid = pid;
@@ -362,7 +362,7 @@ read_object_header_privsep(struct got_object **obj, struct got_repository *repo,
 		/* not reached */
 	}
 
-	if (close(imsg_fds[1]) != 0) {
+	if (close(imsg_fds[1]) == -1) {
 		err = got_error_from_errno("close");
 		free(ibuf);
 		return err;
@@ -542,7 +542,7 @@ read_commit_privsep(struct got_commit_object **commit, int obj_fd,
 		/* not reached */
 	}
 
-	if (close(imsg_fds[1]) != 0) {
+	if (close(imsg_fds[1]) == -1) {
 		err = got_error_from_errno("close");
 		free(ibuf);
 		return err;
@@ -731,7 +731,7 @@ read_tree_privsep(struct got_tree_object **tree, int obj_fd,
 		/* not reached */
 	}
 
-	if (close(imsg_fds[1]) != 0) {
+	if (close(imsg_fds[1]) == -1) {
 		err = got_error_from_errno("close");
 		free(ibuf);
 		return err;
@@ -1087,7 +1087,7 @@ read_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
 		/* not reached */
 	}
 
-	if (close(imsg_fds[1]) != 0) {
+	if (close(imsg_fds[1]) == -1) {
 		err = got_error_from_errno("close");
 		free(ibuf);
 		return err;
@@ -1163,7 +1163,7 @@ open_blob(struct got_blob_object **blob, struct got_repository *repo,
 	}
 
 	if (outbuf) {
-		if (close(outfd) != 0 && err == NULL)
+		if (close(outfd) == -1 && err == NULL)
 			err = got_error_from_errno("close");
 		outfd = -1;
 		(*blob)->f = fmemopen(outbuf, size, "rb");
@@ -1444,7 +1444,7 @@ read_tag_privsep(struct got_tag_object **tag, int obj_fd,
 		/* not reached */
 	}
 
-	if (close(imsg_fds[1]) != 0) {
+	if (close(imsg_fds[1]) == -1) {
 		err = got_error_from_errno("close");
 		free(ibuf);
 		return err;
diff --git a/lib/object_create.c b/lib/object_create.c
index 69a6e96..5b84439 100644
--- a/lib/object_create.c
+++ b/lib/object_create.c
@@ -203,7 +203,7 @@ got_object_blob_file_create(struct got_object_id **id, FILE **blobfile,
 	rewind(*blobfile);
 done:
 	free(header);
-	if (fd != -1 && close(fd) != 0 && err == NULL)
+	if (fd != -1 && close(fd) == -1 && err == NULL)
 		err = got_error_from_errno("close");
 	if (err) {
 		free(*id);
diff --git a/lib/pack.c b/lib/pack.c
index b46583a..712b0f5 100644
--- a/lib/pack.c
+++ b/lib/pack.c
@@ -408,7 +408,7 @@ got_packidx_close(struct got_packidx *packidx)
 		free(packidx->hdr.large_offsets);
 		free(packidx->hdr.trailer);
 	}
-	if (close(packidx->fd) != 0 && err == NULL)
+	if (close(packidx->fd) == -1 && err == NULL)
 		err = got_error_from_errno("close");
 	free(packidx);
 
@@ -525,7 +525,7 @@ got_pack_stop_privsep_child(struct got_pack *pack)
 	if (err)
 		return err;
 	err = got_privsep_wait_for_child(pack->privsep_child->pid);
-	if (close(pack->privsep_child->imsg_fd) != 0 && err == NULL)
+	if (close(pack->privsep_child->imsg_fd) == -1 && err == NULL)
 		err = got_error_from_errno("close");
 	free(pack->privsep_child);
 	pack->privsep_child = NULL;
@@ -540,7 +540,7 @@ got_pack_close(struct got_pack *pack)
 	err = got_pack_stop_privsep_child(pack);
 	if (pack->map && munmap(pack->map, pack->filesize) == -1 && !err)
 		err = got_error_from_errno("munmap");
-	if (pack->fd != -1 && close(pack->fd) != 0 && err == NULL)
+	if (pack->fd != -1 && close(pack->fd) == -1 && err == NULL)
 		err = got_error_from_errno("close");
 	pack->fd = -1;
 	free(pack->path_packfile);
diff --git a/lib/privsep.c b/lib/privsep.c
index 4fb6f24..2a8fcc8 100644
--- a/lib/privsep.c
+++ b/lib/privsep.c
@@ -2307,7 +2307,7 @@ got_privsep_unveil_exec_helpers(void)
 void
 got_privsep_exec_child(int imsg_fds[2], const char *path, const char *repo_path)
 {
-	if (close(imsg_fds[0]) != 0) {
+	if (close(imsg_fds[0]) == -1) {
 		fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
 		_exit(1);
 	}
diff --git a/lib/repository.c b/lib/repository.c
index 4a6e13f..1118dce 100644
--- a/lib/repository.c
+++ b/lib/repository.c
@@ -740,7 +740,7 @@ got_repo_close(struct got_repository *repo)
 		    repo->privsep_children[i].pid);
 		if (child_err && err == NULL)
 			err = child_err;
-		if (close(repo->privsep_children[i].imsg_fd) != 0 &&
+		if (close(repo->privsep_children[i].imsg_fd) == -1 &&
 		    err == NULL)
 			err = got_error_from_errno("close");
 	}
diff --git a/lib/worktree.c b/lib/worktree.c
index 9874757..2d9ccf0 100644
--- a/lib/worktree.c
+++ b/lib/worktree.c
@@ -504,7 +504,7 @@ got_worktree_close(struct got_worktree *worktree)
 	free(worktree->base_commit_id);
 	free(worktree->head_ref_name);
 	if (worktree->lockfd != -1) {
-		if (close(worktree->lockfd) != 0)
+		if (close(worktree->lockfd) == -1)
 			err = got_error_from_errno2("close",
 			    got_worktree_get_root_path(worktree));
 	}
@@ -914,7 +914,7 @@ done:
 	if (symlinkf && fclose(symlinkf) == EOF && err == NULL)
 		err = got_error_from_errno2("fclose", symlink_path);
 	free(symlink_path);
-	if (merged_fd != -1 && close(merged_fd) != 0 && err == NULL)
+	if (merged_fd != -1 && close(merged_fd) == -1 && err == NULL)
 		err = got_error_from_errno("close");
 	if (f_orig && fclose(f_orig) == EOF && err == NULL)
 		err = got_error_from_errno("fclose");
@@ -1581,7 +1581,7 @@ install_blob(struct got_worktree *worktree, const char *ondisk_path,
 	}
 
 done:
-	if (fd != -1 && close(fd) != 0 && err == NULL)
+	if (fd != -1 && close(fd) == -1 && err == NULL)
 		err = got_error_from_errno("close");
 	if (tmppath != NULL && unlink(tmppath) == -1 && err == NULL)
 		err = got_error_from_errno2("unlink", tmppath);
@@ -3623,7 +3623,7 @@ worktree_status(struct got_worktree *worktree, const char *path,
 	}
 done:
 	free_ignores(&arg.ignores);
-	if (fd != -1 && close(fd) != 0 && err == NULL)
+	if (fd != -1 && close(fd) == -1 && err == NULL)
 		err = got_error_from_errno("close");
 	free(ondisk_path);
 	return err;
diff --git a/libexec/got-read-blob/got-read-blob.c b/libexec/got-read-blob/got-read-blob.c
index e2a99e9..092a8f9 100644
--- a/libexec/got-read-blob/got-read-blob.c
+++ b/libexec/got-read-blob/got-read-blob.c
@@ -167,11 +167,11 @@ done:
 			if (fclose(f) == EOF && err == NULL)
 				err = got_error_from_errno("fclose");
 		} else if (imsg.fd != -1) {
-			if (close(imsg.fd) != 0 && err == NULL)
+			if (close(imsg.fd) == -1 && err == NULL)
 				err = got_error_from_errno("close");
 		}
 		if (imsg_outfd.fd != -1) {
-			if (close(imsg_outfd.fd) != 0 && err == NULL)
+			if (close(imsg_outfd.fd) == -1 && err == NULL)
 				err = got_error_from_errno("close");
 		}
 
@@ -190,7 +190,7 @@ done:
 			got_privsep_send_error(&ibuf, err);
 		}
 	}
-	if (close(GOT_IMSG_FD_CHILD) != 0 && err == NULL)
+	if (close(GOT_IMSG_FD_CHILD) == -1 && err == NULL)
 		err = got_error_from_errno("close");
 	return err ? 1 : 0;
 }
diff --git a/libexec/got-read-commit/got-read-commit.c b/libexec/got-read-commit/got-read-commit.c
index c87a051..41be1ea 100644
--- a/libexec/got-read-commit/got-read-commit.c
+++ b/libexec/got-read-commit/got-read-commit.c
@@ -151,7 +151,7 @@ done:
 			if (fclose(f) == EOF && err == NULL)
 				err = got_error_from_errno("fclose");
 		} else if (imsg.fd != -1) {
-			if (close(imsg.fd) != 0 && err == NULL)
+			if (close(imsg.fd) == -1 && err == NULL)
 				err = got_error_from_errno("close");
 		}
 		imsg_free(&imsg);
@@ -166,7 +166,7 @@ done:
 			got_privsep_send_error(&ibuf, err);
 		}
 	}
-	if (close(GOT_IMSG_FD_CHILD) != 0 && err == NULL)
+	if (close(GOT_IMSG_FD_CHILD) == -1 && err == NULL)
 		err = got_error_from_errno("close");
 	return err ? 1 : 0;
 }
diff --git a/libexec/got-read-gitconfig/got-read-gitconfig.c b/libexec/got-read-gitconfig/got-read-gitconfig.c
index b0457c7..eadef71 100644
--- a/libexec/got-read-gitconfig/got-read-gitconfig.c
+++ b/libexec/got-read-gitconfig/got-read-gitconfig.c
@@ -406,7 +406,7 @@ main(int argc, char *argv[])
 			got_privsep_send_error(&ibuf, err);
 		}
 	}
-	if (close(GOT_IMSG_FD_CHILD) != 0 && err == NULL)
+	if (close(GOT_IMSG_FD_CHILD) == -1 && err == NULL)
 		err = got_error_from_errno("close");
 	return err ? 1 : 0;
 }
diff --git a/libexec/got-read-gotconfig/got-read-gotconfig.c b/libexec/got-read-gotconfig/got-read-gotconfig.c
index 2fe537d..682d443 100644
--- a/libexec/got-read-gotconfig/got-read-gotconfig.c
+++ b/libexec/got-read-gotconfig/got-read-gotconfig.c
@@ -392,7 +392,7 @@ main(int argc, char *argv[])
 			got_privsep_send_error(&ibuf, err);
 		}
 	}
-	if (close(GOT_IMSG_FD_CHILD) != 0 && err == NULL)
+	if (close(GOT_IMSG_FD_CHILD) == -1 && err == NULL)
 		err = got_error_from_errno("close");
 	return err ? 1 : 0;
 }
diff --git a/libexec/got-read-object/got-read-object.c b/libexec/got-read-object/got-read-object.c
index 5e19013..96f238d 100644
--- a/libexec/got-read-object/got-read-object.c
+++ b/libexec/got-read-object/got-read-object.c
@@ -111,7 +111,7 @@ main(int argc, char *argv[])
 
 		err = got_privsep_send_obj(&ibuf, obj);
 done:
-		if (close(imsg.fd) != 0 && err == NULL)
+		if (close(imsg.fd) == -1 && err == NULL)
 			err = got_error_from_errno("close");
 		imsg_free(&imsg);
 		if (obj)
@@ -127,7 +127,7 @@ done:
 			got_privsep_send_error(&ibuf, err);
 		}
 	}
-	if (close(GOT_IMSG_FD_CHILD) != 0 && err == NULL)
+	if (close(GOT_IMSG_FD_CHILD) == -1 && err == NULL)
 		err = got_error_from_errno("close");
 	return err ? 1 : 0;
 }
diff --git a/libexec/got-read-pack/got-read-pack.c b/libexec/got-read-pack/got-read-pack.c
index f753b89..dd85687 100644
--- a/libexec/got-read-pack/got-read-pack.c
+++ b/libexec/got-read-pack/got-read-pack.c
@@ -1009,7 +1009,7 @@ main(int argc, char *argv[])
 			break;
 		}
 
-		if (imsg.fd != -1 && close(imsg.fd) != 0 && err == NULL)
+		if (imsg.fd != -1 && close(imsg.fd) == -1 && err == NULL)
 			err = got_error_from_errno("close");
 		imsg_free(&imsg);
 		if (err)
@@ -1028,7 +1028,7 @@ main(int argc, char *argv[])
 			got_privsep_send_error(&ibuf, err);
 		}
 	}
-	if (close(GOT_IMSG_FD_CHILD) != 0 && err == NULL)
+	if (close(GOT_IMSG_FD_CHILD) == -1 && err == NULL)
 		err = got_error_from_errno("close");
 	return err ? 1 : 0;
 }
diff --git a/libexec/got-read-tag/got-read-tag.c b/libexec/got-read-tag/got-read-tag.c
index 69d50c0..2ee8df0 100644
--- a/libexec/got-read-tag/got-read-tag.c
+++ b/libexec/got-read-tag/got-read-tag.c
@@ -143,7 +143,7 @@ done:
 			if (fclose(f) == EOF && err == NULL)
 				err = got_error_from_errno("fclose");
 		} else if (imsg.fd != -1) {
-			if (close(imsg.fd) != 0 && err == NULL)
+			if (close(imsg.fd) == -1 && err == NULL)
 				err = got_error_from_errno("close");
 		}
 		imsg_free(&imsg);
@@ -158,7 +158,7 @@ done:
 			got_privsep_send_error(&ibuf, err);
 		}
 	}
-	if (close(GOT_IMSG_FD_CHILD) != 0 && err == NULL)
+	if (close(GOT_IMSG_FD_CHILD) == -1 && err == NULL)
 		err = got_error_from_errno("close");
 	return err ? 1 : 0;
 }
diff --git a/libexec/got-read-tree/got-read-tree.c b/libexec/got-read-tree/got-read-tree.c
index a2f17b2..03b252f 100644
--- a/libexec/got-read-tree/got-read-tree.c
+++ b/libexec/got-read-tree/got-read-tree.c
@@ -149,7 +149,7 @@ done:
 			if (fclose(f) == EOF && err == NULL)
 				err = got_error_from_errno("fclose");
 		} else if (imsg.fd != -1) {
-			if (close(imsg.fd) != 0 && err == NULL)
+			if (close(imsg.fd) == -1 && err == NULL)
 				err = got_error_from_errno("close");
 		}
 		imsg_free(&imsg);
@@ -164,7 +164,7 @@ done:
 			got_privsep_send_error(&ibuf, err);
 		}
 	}
-	if (close(GOT_IMSG_FD_CHILD) != 0 && err == NULL)
+	if (close(GOT_IMSG_FD_CHILD) == -1 && err == NULL)
 		err = got_error_from_errno("close");
 	return err ? 1 : 0;
 }