Commit 56b63ca4ab1049de6fa2d6910ce22c16e2b42a53

Stefan Sperling 2021-01-22T11:04:47

make fclose(3) failure checks consistent; check 'fclose() == EOF' 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
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
diff --git a/got/got.c b/got/got.c
index 47e8801..4cb72ca 100644
--- a/got/got.c
+++ b/got/got.c
@@ -8442,7 +8442,7 @@ histedit_run_editor(struct got_histedit_list *histedit_cmds,
 
 	err = histedit_check_script(histedit_cmds, commits, repo);
 done:
-	if (f && fclose(f) != 0 && err == NULL)
+	if (f && fclose(f) == EOF && err == NULL)
 		err = got_error_from_errno("fclose");
 	free(editor);
 	return err;
@@ -8479,7 +8479,7 @@ histedit_edit_script(struct got_histedit_list *histedit_cmds,
 		rewind(f);
 		err = histedit_parse_list(histedit_cmds, f, repo);
 	} else {
-		if (fclose(f) != 0) {
+		if (fclose(f) == EOF) {
 			err = got_error_from_errno("fclose");
 			goto done;
 		}
@@ -8494,7 +8494,7 @@ histedit_edit_script(struct got_histedit_list *histedit_cmds,
 		}
 	}
 done:
-	if (f && fclose(f) != 0 && err == NULL)
+	if (f && fclose(f) == EOF && err == NULL)
 		err = got_error_from_errno("fclose");
 	if (path && unlink(path) != 0 && err == NULL)
 		err = got_error_from_errno2("unlink", path);
@@ -8537,7 +8537,7 @@ histedit_save_list(struct got_histedit_list *histedit_cmds,
 		}
 	}
 done:
-	if (f && fclose(f) != 0 && err == NULL)
+	if (f && fclose(f) == EOF && err == NULL)
 		err = got_error_from_errno("fclose");
 	free(path);
 	if (commit)
@@ -8571,7 +8571,7 @@ histedit_load_list(struct got_histedit_list *histedit_cmds,
 
 	err = histedit_parse_list(histedit_cmds, f, repo);
 done:
-	if (f && fclose(f) != 0 && err == NULL)
+	if (f && fclose(f) == EOF && err == NULL)
 		err = got_error_from_errno("fclose");
 	return err;
 }
diff --git a/gotweb/gotweb.c b/gotweb/gotweb.c
index b657ec5..5251b5a 100644
--- a/gotweb/gotweb.c
+++ b/gotweb/gotweb.c
@@ -2696,7 +2696,7 @@ gw_get_repo_description(char **description, struct gw_trans *gw_trans,
 	if (n == 0 && ferror(f))
 		error = got_ferror(f, GOT_ERR_IO);
 done:
-	if (f != NULL && fclose(f) == -1 && error == NULL)
+	if (f != NULL && fclose(f) == EOF && error == NULL)
 		error = got_error_from_errno("fclose");
 	free(d_file);
 	return error;
@@ -2911,7 +2911,7 @@ gw_output_diff(struct gw_trans *gw_trans, struct gw_header *header)
 	if (linelen == -1 && ferror(f))
 		error = got_error_from_errno("getline");
 done:
-	if (f && fclose(f) == -1 && error == NULL)
+	if (f && fclose(f) == EOF && error == NULL)
 		error = got_error_from_errno("fclose");
 	free(line);
 	free(label1);
@@ -2994,7 +2994,7 @@ gw_get_clone_url(char **url, struct gw_trans *gw_trans, char *dir)
 	if (n == 0 && ferror(f))
 		error = got_ferror(f, GOT_ERR_IO);
 done:
-	if (f && fclose(f) == -1 && error == NULL)
+	if (f && fclose(f) == EOF && error == NULL)
 		error = got_error_from_errno("fclose");
 	free(d_file);
 	return NULL;
diff --git a/lib/blame.c b/lib/blame.c
index c8bf3d3..8c1e08c 100644
--- a/lib/blame.c
+++ b/lib/blame.c
@@ -293,9 +293,9 @@ blame_close(struct got_blame *blame)
 		if (munmap(blame->map2, blame->size2) == -1 && err == NULL)
 			err = got_error_from_errno("munmap");
 	}
-	if (blame->f1 && fclose(blame->f1) != 0 && err == NULL)
+	if (blame->f1 && fclose(blame->f1) == EOF && err == NULL)
 		err = got_error_from_errno("fclose");
-	if (blame->f2 && fclose(blame->f2) != 0 && err == NULL)
+	if (blame->f2 && fclose(blame->f2) == EOF && err == NULL)
 		err = got_error_from_errno("fclose");
 	free(blame->lines);
 	free(blame->line_offsets1);
diff --git a/lib/delta.c b/lib/delta.c
index 194fedc..d261659 100644
--- a/lib/delta.c
+++ b/lib/delta.c
@@ -382,7 +382,7 @@ got_delta_apply(FILE *base_file, const uint8_t *delta_buf,
 		err = got_error(GOT_ERR_BAD_DELTA);
 
 	if (memstream != NULL) {
-		if (fclose(memstream) != 0)
+		if (fclose(memstream) == EOF)
 			err = got_error_from_errno("fclose");
 		if (err == NULL) {
 			size_t n;
diff --git a/lib/diff.c b/lib/diff.c
index ab9338f..54a0944 100644
--- a/lib/diff.c
+++ b/lib/diff.c
@@ -189,9 +189,9 @@ diff_blobs(off_t **line_offsets, size_t *nlines,
 			err = free_err;
 	}
 done:
-	if (f1 && fclose(f1) != 0 && err == NULL)
+	if (f1 && fclose(f1) == EOF && err == NULL)
 		err = got_error_from_errno("fclose");
-	if (f2 && fclose(f2) != 0 && err == NULL)
+	if (f2 && fclose(f2) == EOF && err == NULL)
 		err = got_error_from_errno("fclose");
 	return err;
 }
@@ -279,7 +279,7 @@ diff_blob_file(struct got_diffreg_result **resultp,
 			err = free_err;
 	}
 done:
-	if (f1 && fclose(f1) != 0 && err == NULL)
+	if (f1 && fclose(f1) == EOF && err == NULL)
 		err = got_error_from_errno("fclose");
 	return err;
 }
diff --git a/lib/diff3.c b/lib/diff3.c
index 861cb6f..837a0e0 100644
--- a/lib/diff3.c
+++ b/lib/diff3.c
@@ -244,11 +244,11 @@ done:
 			err = got_error_from_errno2("unlink", outpath);
 		free(outpath);
 	}
-	if (outfile && fclose(outfile) != 0 && err == NULL)
+	if (outfile && fclose(outfile) == EOF && err == NULL)
 		err = got_error_from_errno("fclose");
-	if (f1 && fclose(f1) != 0 && err == NULL)
+	if (f1 && fclose(f1) == EOF && err == NULL)
 		err = got_error_from_errno("fclose");
-	if (f2 && fclose(f2) != 0 && err == NULL)
+	if (f2 && fclose(f2) == EOF && err == NULL)
 		err = got_error_from_errno("fclose");
 	return err;
 }
@@ -394,7 +394,7 @@ out:
 	free(patch);
 
 	for (i = 0; i < nitems(d3s->fp); i++) {
-		if (d3s->fp[i] && fclose(d3s->fp[i]) != 0 && err == NULL)
+		if (d3s->fp[i] && fclose(d3s->fp[i]) == EOF && err == NULL)
 			err = got_error_from_errno("fclose");
 	}
 	if (err == NULL && diffb) {
@@ -629,7 +629,7 @@ readin(size_t *n, char *name, struct diff **dd, struct diff3_state *d3s)
 		(*dd)[i].new.from = (*dd)[i - 1].new.to;
 	}
 done:
-	if (fclose(f) != 0 && err == NULL)
+	if (fclose(f) == EOF && err == NULL)
 		err = got_error_from_errno("fclose");
 	if (err == NULL)
 		*n = i;
diff --git a/lib/diffreg.c b/lib/diffreg.c
index 918b50b..7bfd862 100644
--- a/lib/diffreg.c
+++ b/lib/diffreg.c
@@ -98,9 +98,9 @@ got_diffreg_close(FILE *f1, char *p1, size_t size1,
 		err = got_error_from_errno("munmap");
 	if (p2 && munmap(p2, size2) == -1 && err == NULL)
 		err = got_error_from_errno("munmap");
-	if (f1 && fclose(f1) != 0 && err == NULL)
+	if (f1 && fclose(f1) == EOF && err == NULL)
 		err = got_error_from_errno("fclose");
-	if (f2 && fclose(f2) != 0 && err == NULL)
+	if (f2 && fclose(f2) == EOF && err == NULL)
 		err = got_error_from_errno("fclose");
 	return err;
 }
diff --git a/lib/object.c b/lib/object.c
index 7bd5592..7fe404a 100644
--- a/lib/object.c
+++ b/lib/object.c
@@ -1229,7 +1229,7 @@ got_object_blob_close(struct got_blob_object *blob)
 {
 	const struct got_error *err = NULL;
 	free(blob->read_buf);
-	if (blob->f && fclose(blob->f) != 0)
+	if (blob->f && fclose(blob->f) == EOF)
 		err = got_error_from_errno("fclose");
 	free(blob->data);
 	free(blob);
diff --git a/lib/object_create.c b/lib/object_create.c
index 71e76e0..69a6e96 100644
--- a/lib/object_create.c
+++ b/lib/object_create.c
@@ -104,7 +104,7 @@ done:
 			err = got_error_from_errno2("unlink", tmppath);
 		free(tmppath);
 	}
-	if (tmpfile && fclose(tmpfile) != 0 && err == NULL)
+	if (tmpfile && fclose(tmpfile) == EOF && err == NULL)
 		err = got_error_from_errno("fclose");
 	if (lf)
 		unlock_err = got_lockfile_unlock(lf);
@@ -394,7 +394,7 @@ got_object_tree_create(struct got_object_id **id,
 done:
 	free(header);
 	free(sorted_entries);
-	if (treefile && fclose(treefile) != 0 && err == NULL)
+	if (treefile && fclose(treefile) == EOF && err == NULL)
 		err = got_error_from_errno("fclose");
 	if (err) {
 		free(*id);
@@ -573,7 +573,7 @@ done:
 	free(tree_str);
 	free(author_str);
 	free(committer_str);
-	if (commitfile && fclose(commitfile) != 0 && err == NULL)
+	if (commitfile && fclose(commitfile) == EOF && err == NULL)
 		err = got_error_from_errno("fclose");
 	if (err) {
 		free(*id);
@@ -751,7 +751,7 @@ done:
 	free(header);
 	free(obj_str);
 	free(tagger_str);
-	if (tagfile && fclose(tagfile) != 0 && err == NULL)
+	if (tagfile && fclose(tagfile) == EOF && err == NULL)
 		err = got_error_from_errno("fclose");
 	if (err) {
 		free(*id);
diff --git a/lib/reference.c b/lib/reference.c
index 29ca0f7..71840eb 100644
--- a/lib/reference.c
+++ b/lib/reference.c
@@ -227,7 +227,7 @@ parse_ref_file(struct got_reference **ref, const char *name,
 	}
 done:
 	free(line);
-	if (fclose(f) != 0 && err == NULL) {
+	if (fclose(f) == EOF && err == NULL) {
 		err = got_error_from_errno("fclose");
 		if (*ref) {
 			if (lock)
@@ -486,7 +486,7 @@ got_ref_open(struct got_reference **ref, struct got_repository *repo,
 			err = open_packed_ref(ref, f, subdirs, nitems(subdirs),
 			    refname);
 			if (!err) {
-				if (fclose(f) != 0) {
+				if (fclose(f) == EOF) {
 					err = got_error_from_errno("fclose");
 					got_ref_close(*ref);
 					*ref = NULL;
@@ -1006,7 +1006,7 @@ done:
 	free(buf);
 	free(line);
 	free(path_refs);
-	if (f && fclose(f) != 0 && err == NULL)
+	if (f && fclose(f) == EOF && err == NULL)
 		err = got_error_from_errno("fclose");
 	return err;
 }
@@ -1159,7 +1159,7 @@ done:
 	if (ref->lf == NULL && lf)
 		unlock_err = got_lockfile_unlock(lf);
 	if (f) {
-		if (fclose(f) != 0 && err == NULL)
+		if (fclose(f) == EOF && err == NULL)
 			err = got_error_from_errno("fclose");
 	}
 	free(path_refs);
@@ -1304,12 +1304,12 @@ done:
 	if (delref->lf == NULL && lf)
 		unlock_err = got_lockfile_unlock(lf);
 	if (f) {
-		if (fclose(f) != 0 && err == NULL)
+		if (fclose(f) == EOF && err == NULL)
 			err = got_error_from_errno("fclose");
 	}
 	if (tmpf) {
 		unlink(tmppath);
-		if (fclose(tmpf) != 0 && err == NULL)
+		if (fclose(tmpf) == EOF && err == NULL)
 			err = got_error_from_errno("fclose");
 	}
 	free(tmppath);
diff --git a/lib/worktree.c b/lib/worktree.c
index 9eb2d79..9874757 100644
--- a/lib/worktree.c
+++ b/lib/worktree.c
@@ -111,7 +111,7 @@ update_meta_file(const char *path_got, const char *name, const char *content)
 	}
 
 done:
-	if (fclose(tmpfile) != 0 && err == NULL)
+	if (fclose(tmpfile) == EOF && err == NULL)
 		err = got_error_from_errno2("fclose", tmppath);
 	free(tmppath);
 	return err;
@@ -763,9 +763,9 @@ check_files_equal(int *same, const char *f1_path, const char *f2_path)
 
 	err = check_file_contents_equal(same, f1, f2);
 done:
-	if (f1 && fclose(f1) != 0 && err == NULL)
+	if (f1 && fclose(f1) == EOF && err == NULL)
 		err = got_error_from_errno("fclose");
-	if (f2 && fclose(f2) != 0 && err == NULL)
+	if (f2 && fclose(f2) == EOF && err == NULL)
 		err = got_error_from_errno("fclose");
 
 	return err;
@@ -916,7 +916,7 @@ done:
 	free(symlink_path);
 	if (merged_fd != -1 && close(merged_fd) != 0 && err == NULL)
 		err = got_error_from_errno("close");
-	if (f_orig && fclose(f_orig) != 0 && err == NULL)
+	if (f_orig && fclose(f_orig) == EOF && err == NULL)
 		err = got_error_from_errno("fclose");
 	free(merged_path);
 	free(base_path);
@@ -1169,7 +1169,7 @@ merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
 	    ondisk_path, path, st_mode, blob_deriv_path, label_orig,
 	    label_deriv, repo, progress_cb, progress_arg);
 done:
-	if (f_deriv && fclose(f_deriv) != 0 && err == NULL)
+	if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
 		err = got_error_from_errno("fclose");
 	free(base_path);
 	if (blob_deriv_path) {
@@ -2389,7 +2389,7 @@ open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
 			err = got_error_from_errno2("fopen", *fileindex_path);
 	} else {
 		err = got_fileindex_read(*fileindex, index);
-		if (fclose(index) != 0 && err == NULL)
+		if (fclose(index) == EOF && err == NULL)
 			err = got_error_from_errno("fclose");
 	}
 done:
@@ -7694,7 +7694,7 @@ done:
 	if (path_new_staged_content &&
 	    unlink(path_new_staged_content) == -1 && err == NULL)
 		err = got_error_from_errno2("unlink", path_new_staged_content);
-	if (f && fclose(f) != 0 && err == NULL)
+	if (f && fclose(f) == EOF && err == NULL)
 		err = got_error_from_errno2("fclose", path_unstaged_content);
 	free(path_unstaged_content);
 	free(path_new_staged_content);
diff --git a/libexec/got-read-blob/got-read-blob.c b/libexec/got-read-blob/got-read-blob.c
index 7e40657..e2a99e9 100644
--- a/libexec/got-read-blob/got-read-blob.c
+++ b/libexec/got-read-blob/got-read-blob.c
@@ -164,7 +164,7 @@ main(int argc, char *argv[])
 done:
 		free(buf);
 		if (f) {
-			if (fclose(f) != 0 && err == NULL)
+			if (fclose(f) == EOF && err == NULL)
 				err = got_error_from_errno("fclose");
 		} else if (imsg.fd != -1) {
 			if (close(imsg.fd) != 0 && err == NULL)
diff --git a/libexec/got-read-commit/got-read-commit.c b/libexec/got-read-commit/got-read-commit.c
index 02685f9..c87a051 100644
--- a/libexec/got-read-commit/got-read-commit.c
+++ b/libexec/got-read-commit/got-read-commit.c
@@ -148,7 +148,7 @@ main(int argc, char *argv[])
 		got_object_commit_close(commit);
 done:
 		if (f) {
-			if (fclose(f) != 0 && err == NULL)
+			if (fclose(f) == EOF && err == NULL)
 				err = got_error_from_errno("fclose");
 		} else if (imsg.fd != -1) {
 			if (close(imsg.fd) != 0 && err == NULL)
diff --git a/libexec/got-read-pack/got-read-pack.c b/libexec/got-read-pack/got-read-pack.c
index 27d10ab..f753b89 100644
--- a/libexec/got-read-pack/got-read-pack.c
+++ b/libexec/got-read-pack/got-read-pack.c
@@ -346,11 +346,11 @@ 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) != 0 && err == NULL)
+	if (outfile && fclose(outfile) == EOF && err == NULL)
 		err = got_error_from_errno("fclose");
-	if (basefile && fclose(basefile) != 0 && err == NULL)
+	if (basefile && fclose(basefile) == EOF && err == NULL)
 		err = got_error_from_errno("fclose");
-	if (accumfile && fclose(accumfile) != 0 && err == NULL)
+	if (accumfile && fclose(accumfile) == EOF && err == NULL)
 		err = got_error_from_errno("fclose");
 	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 aad8331..69d50c0 100644
--- a/libexec/got-read-tag/got-read-tag.c
+++ b/libexec/got-read-tag/got-read-tag.c
@@ -140,7 +140,7 @@ main(int argc, char *argv[])
 		err = got_privsep_send_tag(&ibuf, tag);
 done:
 		if (f) {
-			if (fclose(f) != 0 && err == NULL)
+			if (fclose(f) == EOF && err == NULL)
 				err = got_error_from_errno("fclose");
 		} else if (imsg.fd != -1) {
 			if (close(imsg.fd) != 0 && err == NULL)
diff --git a/libexec/got-read-tree/got-read-tree.c b/libexec/got-read-tree/got-read-tree.c
index 741d51f..a2f17b2 100644
--- a/libexec/got-read-tree/got-read-tree.c
+++ b/libexec/got-read-tree/got-read-tree.c
@@ -146,7 +146,7 @@ done:
 		got_object_parsed_tree_entries_free(&entries);
 		free(buf);
 		if (f) {
-			if (fclose(f) != 0 && err == NULL)
+			if (fclose(f) == EOF && err == NULL)
 				err = got_error_from_errno("fclose");
 		} else if (imsg.fd != -1) {
 			if (close(imsg.fd) != 0 && err == NULL)
diff --git a/regress/delta/delta_test.c b/regress/delta/delta_test.c
index 5a34b46..984dc73 100644
--- a/regress/delta/delta_test.c
+++ b/regress/delta/delta_test.c
@@ -87,7 +87,7 @@ delta_apply(void)
 
 		err = got_delta_apply(base_file, dt->delta, dt->delta_len,
 		    result_file, &result_len);
-		if (fclose(base_file) != 0 && err == NULL)
+		if (fclose(base_file) == EOF && err == NULL)
 			err = got_error_from_errno("fclose");
 		if (dt->expected == NULL) {
 			/* Invalid delta, expect an error. */
diff --git a/tog/tog.c b/tog/tog.c
index 767893a..915bc83 100644
--- a/tog/tog.c
+++ b/tog/tog.c
@@ -3252,7 +3252,7 @@ create_diff(struct tog_diff_view_state *s)
 		err = got_error_from_errno("got_opentemp");
 		goto done;
 	}
-	if (s->f && fclose(s->f) != 0) {
+	if (s->f && fclose(s->f) == EOF) {
 		err = got_error_from_errno("fclose");
 		goto done;
 	}
@@ -4163,7 +4163,7 @@ stop_blame(struct tog_blame *blame)
 		blame->thread_args.repo = NULL;
 	}
 	if (blame->f) {
-		if (fclose(blame->f) != 0 && err == NULL)
+		if (fclose(blame->f) == EOF && err == NULL)
 			err = got_error_from_errno("fclose");
 		blame->f = NULL;
 	}