Commit fb43ecf1500cf7f8b2e22348bd1635edcda97fee

Stefan Sperling 2019-02-11T11:59:09

check for errors from fclose()

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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
diff --git a/got/got.c b/got/got.c
index 1cd6152..ed6be66 100644
--- a/got/got.c
+++ b/got/got.c
@@ -1000,8 +1000,8 @@ print_diff(void *arg, unsigned char status, const char *path,
 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;
 }
diff --git a/include/got_object.h b/include/got_object.h
index 33e4a1a..c6f0b03 100644
--- a/include/got_object.h
+++ b/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.
diff --git a/lib/blame.c b/lib/blame.c
index e8e4e2b..fab4547 100644
--- a/lib/blame.c
+++ b/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 *start_commit_id,
 		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_object_id *commit_id,
     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_object_id *commit_id,
 	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;
 }
diff --git a/lib/delta.c b/lib/delta.c
index 874c753..730bb4f 100644
--- a/lib/delta.c
+++ b/lib/delta.c
@@ -390,7 +390,8 @@ got_delta_apply(FILE *base_file, const uint8_t *delta_buf,
 		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);
diff --git a/lib/diff.c b/lib/diff.c
index cefba42..db3b2c1 100644
--- a/lib/diff.c
+++ b/lib/diff.c
@@ -61,8 +61,9 @@ diff_blobs(struct got_blob_object *blob1, struct got_blob_object *blob2,
 	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_blob_object *blob2,
 	}
 	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 *f2, size_t size2,
 	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;
 }
 
diff --git a/lib/diff3.c b/lib/diff3.c
index dcfc275..afc751f 100644
--- a/lib/diff3.c
+++ b/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 diff3_state *d3s)
 		(*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
diff --git a/lib/diffreg.c b/lib/diffreg.c
index 8cb618f..8b76ce3 100644
--- a/lib/diffreg.c
+++ b/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);
 }
diff --git a/lib/object.c b/lib/object.c
index 4dd68b6..3e9aaf4 100644
--- a/lib/object.c
+++ b/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 *
diff --git a/lib/pack.c b/lib/pack.c
index 009cf2e..bc1c9a7 100644
--- a/lib/pack.c
+++ b/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;
diff --git a/lib/reference.c b/lib/reference.c
index 419ffd3..c0c0768 100644
--- a/lib/reference.c
+++ b/lib/reference.c
@@ -146,7 +146,8 @@ parse_ref_file(struct got_reference **ref, const char *name,
 	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_repository *repo,
 		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_repository *repo)
 	}
 done:
 	free(path_refs);
-	if (f)
-		fclose(f);
+	if (f && fclose(f) != 0 && err == NULL)
+		err = got_error_from_errno();
 	return err;
 }
diff --git a/lib/worktree.c b/lib/worktree.c
index 949678e..628c668 100644
--- a/lib/worktree.c
+++ b/lib/worktree.c
@@ -120,7 +120,8 @@ update_meta_file(const char *path_got, const char *name, const char *content)
 
 done:
 	free(tmppath);
-	fclose(tmpfile);
+	if (fclose(tmpfile) != 0 && err == NULL)
+		err = got_error_from_errno();
 	return err;
 }
 
diff --git a/libexec/got-read-blob/got-read-blob.c b/libexec/got-read-blob/got-read-blob.c
index 6c013a7..ac1918d 100644
--- a/libexec/got-read-blob/got-read-blob.c
+++ b/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);
diff --git a/libexec/got-read-commit/got-read-commit.c b/libexec/got-read-commit/got-read-commit.c
index c7431a4..98154b8 100644
--- a/libexec/got-read-commit/got-read-commit.c
+++ b/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)
diff --git a/libexec/got-read-pack/got-read-pack.c b/libexec/got-read-pack/got-read-pack.c
index 11bf13f..095e031 100644
--- a/libexec/got-read-pack/got-read-pack.c
+++ b/libexec/got-read-pack/got-read-pack.c
@@ -264,12 +264,12 @@ blob_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
 	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)
diff --git a/libexec/got-read-tag/got-read-tag.c b/libexec/got-read-tag/got-read-tag.c
index b4d7d9a..a9cd2de 100644
--- a/libexec/got-read-tag/got-read-tag.c
+++ b/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)
diff --git a/libexec/got-read-tree/got-read-tree.c b/libexec/got-read-tree/got-read-tree.c
index cfdb2fb..df442f0 100644
--- a/libexec/got-read-tree/got-read-tree.c
+++ b/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)
diff --git a/regress/delta/delta_test.c b/regress/delta/delta_test.c
index 74581bc..490fa9c 100644
--- a/regress/delta/delta_test.c
+++ b/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)
diff --git a/regress/repository/repository_test.c b/regress/repository/repository_test.c
index a2e8fe5..db57150 100644
--- a/regress/repository/repository_test.c
+++ b/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",
diff --git a/regress/worktree/worktree_test.c b/regress/worktree/worktree_test.c
index 238c089..5d9dfdd 100644
--- a/regress/worktree/worktree_test.c
+++ b/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_path, const char *name)
 		free(*path);
 		ret = 0;
 	}
-	fclose(f);
+	if (fclose(f) != 0)
+		ret = 0;
 	return ret;
 }
 
diff --git a/tog/tog.c b/tog/tog.c
index dd0a9ea..dfcd757 100644
--- a/tog/tog.c
+++ b/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) {