make close(2) failure checks consistent; check 'close() == -1' everywhere ok millert, naddy
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397
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;
}