commit fb43ecf1500cf7f8b2e22348bd1635edcda97fee from: Stefan Sperling date: Mon Feb 11 10:59:09 2019 UTC check for errors from fclose() commit - cbe7f84890b5be0eb0e47f67f1606f66db181eae commit + fb43ecf1500cf7f8b2e22348bd1635edcda97fee blob - 1cd61521b11013a94063f2b53ddec0e8c0252379 blob + ed6be662c4c6b69e33a58601fd7c8d7cc2eae3c9 --- got/got.c +++ got/got.c @@ -1000,8 +1000,8 @@ print_diff(void *arg, unsigned char status, const char done: if (blob1) got_object_blob_close(blob1); - if (f2) - fclose(f2); + if (f2 && fclose(f2) != 0 && err == NULL) + err = got_error_from_errno(); free(abspath); return err; } blob - 33e4a1aa330d929e58c6ade1fc66a14ea85c252e blob + c6f0b0364810e7c3a2c73ea7e9a422356e3cefac --- include/got_object.h +++ include/got_object.h @@ -176,7 +176,7 @@ got_object_open_as_blob(struct got_blob_object **, struct got_repository *, struct got_object_id *, size_t); /* Dispose of a blob object. */ -void got_object_blob_close(struct got_blob_object *); +const struct got_error *got_object_blob_close(struct got_blob_object *); /* * Get the length of header data at the beginning of the blob's read buffer. blob - e8e4e2b505da78e54c8c0d88c7df21b3aee29442 blob + fab454738ef35e0a79f179d4d18cc12f07be0ec7 --- lib/blame.c +++ lib/blame.c @@ -268,13 +268,14 @@ done: return err; } -static void +static const struct got_error * blame_close(struct got_blame *blame) { + const struct got_error *err = NULL; struct got_blame_diff_offsets *diff_offsets; - if (blame->f) - fclose(blame->f); + if (blame->f && fclose(blame->f) != 0) + err = got_error_from_errno(); free(blame->lines); while (!SLIST_EMPTY(&blame->diff_offsets_list)) { diff_offsets = SLIST_FIRST(&blame->diff_offsets_list); @@ -282,6 +283,7 @@ blame_close(struct got_blame *blame) free_diff_offsets(diff_offsets); } free(blame); + return err; } static const struct got_error * @@ -432,7 +434,7 @@ const struct got_error * got_blame(const char *path, struct got_object_id *start_commit_id, struct got_repository *repo, FILE *outfile) { - const struct got_error *err = NULL; + const struct got_error *err = NULL, *close_err = NULL; struct got_blame *blame; int lineno; char *abspath; @@ -472,9 +474,9 @@ got_blame(const char *path, struct got_object_id *star free(id_str); } - blame_close(blame); + close_err = blame_close(blame); free(abspath); - return err; + return err ? err : close_err; } const struct got_error * @@ -483,7 +485,7 @@ got_blame_incremental(const char *path, struct got_obj const struct got_error *(*cb)(void *, int, int, struct got_object_id *), void *arg) { - const struct got_error *err = NULL; + const struct got_error *err = NULL, *close_err = NULL; struct got_blame *blame; char *abspath; @@ -493,6 +495,6 @@ got_blame_incremental(const char *path, struct got_obj err = blame_open(&blame, abspath, commit_id, repo, cb, arg); free(abspath); if (blame) - blame_close(blame); - return err; + close_err = blame_close(blame); + return err ? err : close_err; } blob - 874c7535c3108bccd764b3e2e206e12189b9aa97 blob + 730bb4f921b6fb457b977f7f128336551b10e0c1 --- lib/delta.c +++ lib/delta.c @@ -390,7 +390,8 @@ got_delta_apply(FILE *base_file, const uint8_t *delta_ err = got_error(GOT_ERR_BAD_DELTA); if (memstream != NULL) { - fclose(memstream); + if (fclose(memstream) != 0) + err = got_error_from_errno(); if (err == NULL) { size_t n; n = fwrite(memstream_buf, 1, memstream_size, outfile); blob - cefba42370c417b69460c1dcf59530a7dd5e05ab blob + db3b2c1513a367b64f3a9fe786aa2c9c0cfb8f4c --- lib/diff.c +++ lib/diff.c @@ -61,8 +61,9 @@ diff_blobs(struct got_blob_object *blob1, struct got_b if (blob2) { f2 = got_opentemp(); if (f2 == NULL) { + err = got_error_from_errno(); fclose(f1); - return got_error_from_errno(); + return err; } } else flags |= D_EMPTY2; @@ -110,10 +111,10 @@ diff_blobs(struct got_blob_object *blob1, struct got_b } err = got_diffreg(&res, f1, f2, flags, &args, &ds, outfile, changes); done: - if (f1) - fclose(f1); - if (f2) - fclose(f2); + if (f1 && fclose(f1) != 0 && err == NULL) + err = got_error_from_errno(); + if (f2 && fclose(f2) != 0 && err == NULL) + err = got_error_from_errno(); return err; } @@ -177,8 +178,8 @@ got_diff_blob_file(struct got_blob_object *blob1, FILE fprintf(outfile, "file + %s\n", label2); err = got_diffreg(&res, f1, f2, flags, &args, &ds, outfile, NULL); done: - if (f1) - fclose(f1); + if (f1 && fclose(f1) != 0 && err == NULL) + err = got_error_from_errno(); return err; } blob - dcfc275c4a821d058821851f9fad0ed53f752e51 blob + afc751faf58a09c1340a372d99e9f4faeaebfb11 --- lib/diff3.c +++ lib/diff3.c @@ -253,12 +253,12 @@ done: unlink(outpath); free(outpath); } - if (outfile) - fclose(outfile); - if (f1) - fclose(f1); - if (f2) - fclose(f2); + if (outfile && fclose(outfile) != 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(); return err; } @@ -401,8 +401,8 @@ out: worklist_clean(&temp_files, worklist_unlink); for (i = 0; i < nitems(d3s->fp); i++) { - if (d3s->fp[i]) - fclose(d3s->fp[i]); + if (d3s->fp[i] && fclose(d3s->fp[i]) != 0 && err == NULL) + err = got_error_from_errno(); } if (err == NULL && diffb) { if (buf_write_fd(diffb, outfd) < 0) @@ -618,10 +618,11 @@ readin(size_t *n, char *name, struct diff **dd, struct (*dd)[i].new.from = (*dd)[i-1].new.to; } - (void)fclose(d3s->fp[0]); + if (fclose(d3s->fp[0]) != 0) + err = got_error_from_errno(); *n = i; - return NULL; + return err; } static int blob - 8cb618fd7f9460b75750ea998b1c440335b854ce blob + 8b76ce3a69336a0795d6f8d4b49c6d858dcf0b17 --- lib/diffreg.c +++ lib/diffreg.c @@ -419,10 +419,10 @@ closem: if (*rval == D_SAME) *rval = D_DIFFER; } - if (f1 != NULL) - fclose(f1); - if (f2 != NULL) - fclose(f2); + if (f1 != NULL && fclose(f1) != 0 && err == NULL) + err = got_error_from_errno(); + if (f2 != NULL && fclose(f2) != 0 && err == NULL) + err = got_error_from_errno(); return (err); } blob - 4dd68b61711b0c1ffeead295235e4c837415af37 blob + 3e9aaf42345eb80afae9e7e148bfe4a90cb8bcda --- lib/object.c +++ lib/object.c @@ -1085,13 +1085,16 @@ got_object_blob_open(struct got_blob_object **blob, return open_blob(blob, repo, got_object_get_id(obj), blocksize); } -void +const struct got_error * got_object_blob_close(struct got_blob_object *blob) { + const struct got_error *err = NULL; free(blob->read_buf); - fclose(blob->f); + if (fclose(blob->f) != 0) + err = got_error_from_errno(); free(blob->data); free(blob); + return err; } char * blob - 009cf2ed9c2c7c96cb64716dd79be5d5abac2905 blob + bc1c9a7acf2d7e09690c1584c4a6339208bdfea9 --- lib/pack.c +++ lib/pack.c @@ -1132,10 +1132,10 @@ done: if (len != accum_size) err = got_ferror(outfile, GOT_ERR_IO); } - if (base_file) - fclose(base_file); - if (accum_file) - fclose(accum_file); + if (base_file && fclose(base_file) != 0 && err == NULL) + err = got_error_from_errno(); + if (accum_file && fclose(accum_file) != 0 && err == NULL) + err = got_error_from_errno(); rewind(outfile); if (err == NULL) *result_size = accum_size; blob - 419ffd3592d2e92851797182541245fcb5cbcdba blob + c0c07681f652088d43eb788a5cf2e88974ee275a --- lib/reference.c +++ lib/reference.c @@ -146,7 +146,8 @@ parse_ref_file(struct got_reference **ref, const char err = parse_ref_line(ref, name, line); done: free(line); - fclose(f); + if (fclose(f) != 0 && err == NULL) + err = got_error_from_errno(); return err; } @@ -322,7 +323,8 @@ got_ref_open(struct got_reference **ref, struct got_re if (f != NULL) { err = open_packed_ref(ref, f, subdirs, nitems(subdirs), refname); - fclose(f); + if (fclose(f) != 0 && err == NULL) + err = got_error_from_errno(); if (err || *ref) goto done; } @@ -628,7 +630,7 @@ got_ref_list(struct got_reflist_head *refs, struct got } done: free(path_refs); - if (f) - fclose(f); + if (f && fclose(f) != 0 && err == NULL) + err = got_error_from_errno(); return err; } blob - 949678e5c0659175b5f4de710c0d32db3121fbc7 blob + 628c6689cbb1a5e701d984a3bf321bd8253bdfcb --- lib/worktree.c +++ lib/worktree.c @@ -120,7 +120,8 @@ update_meta_file(const char *path_got, const char *nam done: free(tmppath); - fclose(tmpfile); + if (fclose(tmpfile) != 0 && err == NULL) + err = got_error_from_errno(); return err; } blob - 6c013a7b5814a8a603ea3de127670f1725eb8e8d blob + ac1918d7c9f2731cab8c77333e16148dafa27713 --- libexec/got-read-blob/got-read-blob.c +++ libexec/got-read-blob/got-read-blob.c @@ -161,9 +161,10 @@ main(int argc, char *argv[]) err = got_privsep_send_blob(&ibuf, size, obj->hdrlen, buf); done: - if (f) - fclose(f); - else if (imsg.fd != -1) + 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); blob - c7431a4d45fb85ca0154f8278fad20793f975d66 blob + 98154b8bcf44c7c0d36a1aeb312cff4f575257c4 --- libexec/got-read-commit/got-read-commit.c +++ libexec/got-read-commit/got-read-commit.c @@ -144,9 +144,10 @@ main(int argc, char *argv[]) err = got_privsep_send_commit(&ibuf, commit); done: - if (f) - fclose(f); - else if (imsg.fd != -1) + if (f) { + if (fclose(f) != 0 && err == NULL) + err = got_error_from_errno(); + } else if (imsg.fd != -1) close(imsg.fd); imsg_free(&imsg); if (err) blob - 11bf13fb72e1aed324609da86c813f934627206f blob + 095e0314205e03ec8132ed3d072451a23fc3ebda --- libexec/got-read-pack/got-read-pack.c +++ libexec/got-read-pack/got-read-pack.c @@ -264,12 +264,12 @@ blob_request(struct imsg *imsg, struct imsgbuf *ibuf, err = got_privsep_send_blob(ibuf, obj->size, obj->hdrlen, buf); done: free(buf); - if (outfile) - fclose(outfile); - if (basefile) - fclose(basefile); - if (accumfile) - fclose(accumfile); + if (outfile && fclose(outfile) != 0 && err == NULL) + err = got_error_from_errno(); + if (basefile && fclose(basefile) != 0 && err == NULL) + err = got_error_from_errno(); + if (accumfile && fclose(accumfile) != 0 && err == NULL) + err = got_error_from_errno(); if (obj) got_object_close(obj); if (err && err->code != GOT_ERR_PRIVSEP_PIPE) blob - b4d7d9a95b8ff42eb377dda1390f126aa798b5ce blob + a9cd2de175529ff8238a4b2b481a03bad7f87012 --- libexec/got-read-tag/got-read-tag.c +++ libexec/got-read-tag/got-read-tag.c @@ -139,9 +139,10 @@ main(int argc, char *argv[]) err = got_privsep_send_tag(&ibuf, tag); done: - if (f) - fclose(f); - else if (imsg.fd != -1) + if (f) { + if (fclose(f) != 0 && err == NULL) + err = got_error_from_errno(); + } else if (imsg.fd != -1) close(imsg.fd); imsg_free(&imsg); if (err) blob - cfdb2fb226fd413169bd4ae922f28b13832e7dbb blob + df442f0754194bd9fb2a25b1a133f13d6e7fbd24 --- libexec/got-read-tree/got-read-tree.c +++ libexec/got-read-tree/got-read-tree.c @@ -138,9 +138,10 @@ main(int argc, char *argv[]) err = got_privsep_send_tree(&ibuf, tree); done: - if (f) - fclose(f); - else if (imsg.fd != -1) + if (f) { + if (fclose(f) != 0 && err == NULL) + err = got_error_from_errno(); + } else if (imsg.fd != -1) close(imsg.fd); imsg_free(&imsg); if (err) blob - 74581bc42859b9abd9fe0d6f4432d3e158e5ca6a blob + 490fa9cc29d86b5e10874cade681cf9d2e515326 --- regress/delta/delta_test.c +++ regress/delta/delta_test.c @@ -86,7 +86,8 @@ delta_apply(void) err = got_delta_apply(base_file, dt->delta, dt->delta_len, result_file, &result_len); - fclose(base_file); + if (fclose(base_file) != 0 && err == NULL) + err = got_error_from_errno(); if (dt->expected == NULL) { /* Invalid delta, expect an error. */ if (err == NULL) blob - a2e8fe577030dd581e858b6ae6dfd20d197a17c3 blob + db57150665e1556a687d6d952c8c2549b53fec0e --- regress/repository/repository_test.c +++ regress/repository/repository_test.c @@ -338,7 +338,8 @@ repo_diff_blob(const char *repo_path) } i++; } - fclose(outfile); + if (fclose(outfile) != 0 && err == NULL) + err = got_error_from_errno(); test_printf("\n"); if (i != nitems(expected_output) + 1) { test_printf("number of lines expected: %d; actual: %d\n", blob - 238c089037e6a5b76af616643dcfdc781fea6743 blob + 5d9dfdde2dc4af8c6097550fcfb0958cbc8a4331 --- regress/worktree/worktree_test.c +++ regress/worktree/worktree_test.c @@ -121,7 +121,8 @@ read_meta_file(char **content, const char *path) *content = fparseln(f, &len, NULL, delim, 0); if (*content == NULL) ret = errno; - fclose(f); + if (fclose(f) != 0 && ret == 0) + ret = errno; return ret; } @@ -218,7 +219,8 @@ obstruct_meta_file(char **path, const char *worktree_p free(*path); ret = 0; } - fclose(f); + if (fclose(f) != 0) + ret = 0; return ret; } blob - dd0a9ea2287596227b3ce25a45daa5cba4350894 blob + dfcd7578dc2c6fec0889929b939addb4f743f132 --- tog/tog.c +++ tog/tog.c @@ -1920,8 +1920,10 @@ create_diff(struct tog_diff_view_state *s) err = got_error_from_errno(); goto done; } - if (s->f) - fclose(s->f); + if (s->f && fclose(s->f) != 0) { + err = got_error_from_errno(); + goto done; + } s->f = f; if (s->id1) @@ -2446,7 +2448,8 @@ stop_blame(struct tog_blame *blame) blame->thread_args.repo = NULL; } if (blame->f) { - fclose(blame->f); + if (fclose(blame->f) != 0 && err == NULL) + err = got_error_from_errno(); blame->f = NULL; } if (blame->lines) {