commit 3a6ce05aff1497b3b590a07e1dccad289ad84d0f from: Stefan Sperling date: Mon Feb 11 11:32:53 2019 UTC check for errors from close(2) commit - bcd28dfd6db4114ad7d859722b35b1ccf2c613ac commit + 3a6ce05aff1497b3b590a07e1dccad289ad84d0f blob - 3961b6d7873af2a2c046fb30c1497f452d1136f5 blob + fe7760b8b5c088e55c141d45fe73ad788eef4e63 --- include/got_worktree.h +++ include/got_worktree.h @@ -47,7 +47,7 @@ const struct got_error *got_worktree_init(const char * const struct got_error *got_worktree_open(struct got_worktree **, const char *); /* Dispose of an open work tree. */ -void got_worktree_close(struct got_worktree *); +const struct got_error *got_worktree_close(struct got_worktree *); /* * Get the path to the root directory of a worktree. blob - 2d48ff9b000b6328e27870447788445deb6e52d8 blob + 6fa9823d42b1356e726625f9d938f1f74ec1b0c9 --- lib/buf.c +++ lib/buf.c @@ -309,7 +309,8 @@ buf_write(BUF *b, const char *path, mode_t mode) if (fchmod(fd, mode) < 0) err = got_error_from_errno(); - (void)close(fd); + if (close(fd) != 0 && err == NULL) + err = got_error_from_errno(); return err; } @@ -335,7 +336,9 @@ buf_write_stmp(BUF *b, char *template, struct wklhead (void)unlink(template); } - (void)close(fd); + if (close(fd) != 0 && err == NULL) + err = got_error_from_errno(); + return err; } blob - 3e9aaf42345eb80afae9e7e148bfe4a90cb8bcda blob + 64f1a99230c0b4fde28e46444c1d01f7f3270f9d --- lib/object.c +++ lib/object.c @@ -181,7 +181,11 @@ get_packfile_path(char **path_packfile, struct got_pac static void exec_privsep_child(int imsg_fds[2], const char *path, const char *repo_path) { - close(imsg_fds[0]); + if (close(imsg_fds[0]) != 0) { + fprintf(stderr, "%s: %s\n", getprogname(), + strerror(errno)); + _exit(1); + } if (dup2(imsg_fds[1], GOT_IMSG_FD_CHILD) == -1) { fprintf(stderr, "%s: %s\n", getprogname(), @@ -260,7 +264,8 @@ start_pack_privsep_child(struct got_pack *pack, struct /* not reached */ } - close(imsg_fds[1]); + if (close(imsg_fds[1]) != 0) + return got_error_from_errno(); pack->privsep_child->imsg_fd = imsg_fds[0]; pack->privsep_child->pid = pid; imsg_init(ibuf, imsg_fds[0]); @@ -379,7 +384,8 @@ read_object_header_privsep(struct got_object **obj, st /* not reached */ } - close(imsg_fds[1]); + if (close(imsg_fds[1]) != 0) + return got_error_from_errno(); repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd = imsg_fds[0]; repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].pid = pid; @@ -434,8 +440,8 @@ got_object_open(struct got_object **obj, struct got_re err = got_repo_cache_object(repo, id, *obj); done: free(path); - if (fd != -1) - close(fd); + if (fd != -1 && close(fd) != 0 && errno != EBADF && err == NULL) + err = got_error_from_errno(); return err; } @@ -545,7 +551,8 @@ read_commit_privsep(struct got_commit_object **commit, /* not reached */ } - close(imsg_fds[1]); + if (close(imsg_fds[1]) != 0) + return got_error_from_errno(); repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd = imsg_fds[0]; repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].pid = pid; @@ -598,7 +605,8 @@ open_commit(struct got_commit_object **commit, if (err) return err; err = read_commit_privsep(commit, fd, repo); - close(fd); + if (close(fd) != 0 && errno != EBADF && err == NULL) + err = got_error_from_errno(); } if (err == NULL) { @@ -723,8 +731,8 @@ read_tree_privsep(struct got_tree_object **tree, int o /* not reached */ } - close(imsg_fds[1]); - + if (close(imsg_fds[1]) != 0) + return got_error_from_errno(); repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd = imsg_fds[0]; repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].pid = pid; @@ -777,7 +785,8 @@ open_tree(struct got_tree_object **tree, struct got_re if (err) return err; err = read_tree_privsep(tree, fd, repo); - close(fd); + if (close(fd) != 0 && errno != EBADF && err == NULL) + err = got_error_from_errno(); } if (err == NULL) { @@ -949,7 +958,8 @@ read_blob_privsep(uint8_t **outbuf, size_t *size, size /* not reached */ } - close(imsg_fds[1]); + if (close(imsg_fds[1]) != 0) + return got_error_from_errno(); repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd = imsg_fds[0]; repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].pid = pid; @@ -1011,7 +1021,8 @@ open_blob(struct got_blob_object **blob, struct got_re goto done; err = read_blob_privsep(&outbuf, &size, &hdrlen, outfd, infd, repo); - close(infd); + if (close(infd) != 0 && errno != EBADF && err == NULL) + err = got_error_from_errno(); } if (err) goto done; @@ -1022,7 +1033,8 @@ open_blob(struct got_blob_object **blob, struct got_re } if (outbuf) { - close(outfd); + if (close(outfd) != 0 && err == NULL) + err = got_error_from_errno(); outfd = -1; (*blob)->f = fmemopen(outbuf, size, "rb"); if ((*blob)->f == NULL) { @@ -1245,7 +1257,8 @@ read_tag_privsep(struct got_tag_object **tag, int obj_ /* not reached */ } - close(imsg_fds[1]); + if (close(imsg_fds[1]) != 0) + return got_error_from_errno(); repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd = imsg_fds[0]; repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].pid = pid; @@ -1297,7 +1310,8 @@ open_tag(struct got_tag_object **tag, struct got_repos if (err) return err; err = read_tag_privsep(tag, fd, repo); - close(fd); + if (close(fd) != 0 && errno != EBADF && err == NULL) + err = got_error_from_errno(); } if (err == NULL) { blob - bc1c9a7acf2d7e09690c1584c4a6339208bdfea9 blob + 5c4ddf2636a04b5817c805886b18e361fd1d5de8 --- lib/pack.c +++ lib/pack.c @@ -427,7 +427,8 @@ got_packidx_close(struct got_packidx *packidx) free(packidx->hdr.large_offsets); free(packidx->hdr.trailer); } - close(packidx->fd); + if (close(packidx->fd) != 0 && err == NULL) + err = got_error_from_errno(); free(packidx); return err; @@ -503,7 +504,8 @@ 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(); - close(pack->fd); + if (pack->fd != -1 && close(pack->fd) != 0 && err == NULL) + err = got_error_from_errno(); pack->fd = -1; free(pack->path_packfile); pack->path_packfile = NULL; blob - 6112deb56be8456c3095b674cb324b737275cead blob + 18ad2bbd94ad2e7a2b6a810cce67893121ae71e4 --- lib/repository.c +++ lib/repository.c @@ -418,7 +418,9 @@ got_repo_close(struct got_repository *repo) repo->privsep_children[i].pid); if (child_err && err == NULL) err = child_err; - close(repo->privsep_children[i].imsg_fd); + if (close(repo->privsep_children[i].imsg_fd) != 0 && + err == NULL) + err = got_error_from_errno(); } free(repo); blob - 628c6689cbb1a5e701d984a3bf321bd8253bdfcb blob + 37c8744b87c5306d5226f968dcaefb4cc5bdd970 --- lib/worktree.c +++ lib/worktree.c @@ -412,9 +412,10 @@ got_worktree_open(struct got_worktree **worktree, cons return got_error(GOT_ERR_NOT_WORKTREE); } -void +const struct got_error * got_worktree_close(struct got_worktree *worktree) { + const struct got_error *err = NULL; free(worktree->root_path); free(worktree->repo_path); free(worktree->path_prefix); @@ -422,8 +423,10 @@ got_worktree_close(struct got_worktree *worktree) if (worktree->head_ref) got_ref_close(worktree->head_ref); if (worktree->lockfd != -1) - close(worktree->lockfd); + if (close(worktree->lockfd) != 0) + err = got_error_from_errno(); free(worktree); + return err; } const char * @@ -674,12 +677,12 @@ merge_blob(struct got_worktree *worktree, struct got_f err = got_fileindex_entry_update(ie, ondisk_path, blob1->id.sha1, worktree->base_commit_id->sha1, 0); done: - if (merged_fd != -1) - close(merged_fd); - if (f1) - fclose(f1); - if (f2) - fclose(f2); + if (merged_fd != -1 && close(merged_fd) != 0 && err == NULL) + err = got_error_from_errno(); + if (f1 && fclose(f1) != 0 && err == NULL) + err = got_error_from_errno(); + if (f2 && fclose(f2) != 0 && err == NULL) + err = got_error_from_errno(); if (blob2) got_object_blob_close(blob2); free(merged_path); @@ -800,8 +803,8 @@ install_blob(struct got_worktree *worktree, struct got err = got_fileindex_entry_add(fileindex, entry); } done: - if (fd != -1) - close(fd); + if (fd != -1 && close(fd) != 0 && err == NULL) + err = got_error_from_errno(); free(tmppath); return err; } blob - ac1918d7c9f2731cab8c77333e16148dafa27713 blob + ce22859b7c7bf03af2235be4c66f5fed1eb18a1c --- libexec/got-read-blob/got-read-blob.c +++ libexec/got-read-blob/got-read-blob.c @@ -164,10 +164,15 @@ done: if (f) { if (fclose(f) != 0 && err == NULL) err = got_error_from_errno(); - } else if (imsg.fd != -1) - close(imsg.fd); - if (imsg_outfd.fd != -1) - close(imsg_outfd.fd); + } else if (imsg.fd != -1) { + if (close(imsg.fd) != 0 && err == NULL) + err = got_error_from_errno(); + } + if (imsg_outfd.fd != -1) { + if (close(imsg_outfd.fd) != 0 && err == NULL) + err = got_error_from_errno(); + } + imsg_free(&imsg); imsg_free(&imsg_outfd); if (obj) @@ -183,6 +188,7 @@ done: got_privsep_send_error(&ibuf, err); } } - close(GOT_IMSG_FD_CHILD); + if (close(GOT_IMSG_FD_CHILD) != 0 && err == NULL) + err = got_error_from_errno(); return err ? 1 : 0; } blob - 98154b8bcf44c7c0d36a1aeb312cff4f575257c4 blob + e39345fd841e433db3512e2ca55ee8e9bb4cca89 --- libexec/got-read-commit/got-read-commit.c +++ libexec/got-read-commit/got-read-commit.c @@ -147,8 +147,10 @@ done: if (f) { if (fclose(f) != 0 && err == NULL) err = got_error_from_errno(); - } else if (imsg.fd != -1) - close(imsg.fd); + } else if (imsg.fd != -1) { + if (close(imsg.fd) != 0 && err == NULL) + err = got_error_from_errno(); + } imsg_free(&imsg); if (err) break; @@ -161,6 +163,7 @@ done: got_privsep_send_error(&ibuf, err); } } - close(GOT_IMSG_FD_CHILD); + if (close(GOT_IMSG_FD_CHILD) != 0 && err == NULL) + err = got_error_from_errno(); return err ? 1 : 0; } blob - e6339341b9614a7bf218a689c25d8212ef1025e2 blob + 6692587e2c5320fd5093c0bbd4105f54b5d384e2 --- libexec/got-read-object/got-read-object.c +++ libexec/got-read-object/got-read-object.c @@ -111,7 +111,8 @@ main(int argc, char *argv[]) err = got_privsep_send_obj(&ibuf, obj); done: - close(imsg.fd); + if (close(imsg.fd) != 0 && err == NULL) + err = got_error_from_errno(); imsg_free(&imsg); if (obj) got_object_close(obj); @@ -126,6 +127,7 @@ done: got_privsep_send_error(&ibuf, err); } } - close(GOT_IMSG_FD_CHILD); + if (close(GOT_IMSG_FD_CHILD) != 0 && err == NULL) + err = got_error_from_errno(); return err ? 1 : 0; } blob - 095e0314205e03ec8132ed3d072451a23fc3ebda blob + f88d42c77cfffbd6eb66f4a8878b805983106bd8 --- libexec/got-read-pack/got-read-pack.c +++ libexec/got-read-pack/got-read-pack.c @@ -203,8 +203,8 @@ receive_file(FILE **f, struct imsgbuf *ibuf, int imsg_ *f = fdopen(imsg.fd, "w+"); if (*f == NULL) { - close(imsg.fd); err = got_error_from_errno(); + close(imsg.fd); goto done; } done: @@ -551,8 +551,8 @@ main(int argc, char *argv[]) break; } - if (imsg.fd != -1) - close(imsg.fd); + if (imsg.fd != -1 && close(imsg.fd) != 0 && err == NULL) + err = got_error_from_errno(); imsg_free(&imsg); if (err) break; @@ -570,6 +570,7 @@ main(int argc, char *argv[]) got_privsep_send_error(&ibuf, err); } } - close(GOT_IMSG_FD_CHILD); + if (close(GOT_IMSG_FD_CHILD) != 0 && err == NULL) + err = got_error_from_errno(); return err ? 1 : 0; } blob - a9cd2de175529ff8238a4b2b481a03bad7f87012 blob + 62f4869c5952c26a84cda9fdfb57d6d634126247 --- libexec/got-read-tag/got-read-tag.c +++ libexec/got-read-tag/got-read-tag.c @@ -142,8 +142,10 @@ done: if (f) { if (fclose(f) != 0 && err == NULL) err = got_error_from_errno(); - } else if (imsg.fd != -1) - close(imsg.fd); + } else if (imsg.fd != -1) { + if (close(imsg.fd) != 0 && err == NULL) + err = got_error_from_errno(); + } imsg_free(&imsg); if (err) break; @@ -156,6 +158,7 @@ done: got_privsep_send_error(&ibuf, err); } } - close(GOT_IMSG_FD_CHILD); + if (close(GOT_IMSG_FD_CHILD) != 0 && err == NULL) + err = got_error_from_errno(); return err ? 1 : 0; } blob - df442f0754194bd9fb2a25b1a133f13d6e7fbd24 blob + 3edf27d89335964dd646e739f9ce3963555a7bcf --- libexec/got-read-tree/got-read-tree.c +++ libexec/got-read-tree/got-read-tree.c @@ -141,8 +141,10 @@ done: if (f) { if (fclose(f) != 0 && err == NULL) err = got_error_from_errno(); - } else if (imsg.fd != -1) - close(imsg.fd); + } else if (imsg.fd != -1) { + if (close(imsg.fd) != 0 && err == NULL) + err = got_error_from_errno(); + } imsg_free(&imsg); if (err) break; @@ -155,6 +157,7 @@ done: got_privsep_send_error(&ibuf, err); } } - close(GOT_IMSG_FD_CHILD); + if (close(GOT_IMSG_FD_CHILD) != 0 && err == NULL) + err = got_error_from_errno(); return err ? 1 : 0; }