Commit 1453347d2463ce1b2700dd9fa810f30f2a443e5e

Stefan Sperling 2022-05-19T07:14:21

imsg_add() frees its msg argument on error; avoid double-free in error paths

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
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
diff --git a/lib/privsep.c b/lib/privsep.c
index 24cb88f..782f94a 100644
--- a/lib/privsep.c
+++ b/lib/privsep.c
@@ -309,18 +309,12 @@ got_privsep_send_raw_obj(struct imsgbuf *ibuf, off_t size, size_t hdrlen,
 		return err;
 	}
 
-	if (imsg_add(wbuf, &iobj, sizeof(iobj)) == -1) {
-		err = got_error_from_errno("imsg_add RAW_OBJECT");
-		ibuf_free(wbuf);
-		return err;
-	}
+	if (imsg_add(wbuf, &iobj, sizeof(iobj)) == -1)
+		return got_error_from_errno("imsg_add RAW_OBJECT");
 
 	if (data && size + hdrlen <= GOT_PRIVSEP_INLINE_OBJECT_DATA_MAX) {
-		if (imsg_add(wbuf, data, size + hdrlen) == -1) {
-			err = got_error_from_errno("imsg_add RAW_OBJECT");
-			ibuf_free(wbuf);
-			return err;
-		}
+		if (imsg_add(wbuf, data, size + hdrlen) == -1)
+			return got_error_from_errno("imsg_add RAW_OBJECT");
 	}
 
 	wbuf->fd = -1;
@@ -416,7 +410,6 @@ const struct got_error *
 got_privsep_send_tree_req(struct imsgbuf *ibuf, int fd,
     struct got_object_id *id, int pack_idx)
 {
-	const struct got_error *err = NULL;
 	struct ibuf *wbuf;
 	size_t len;
 
@@ -429,18 +422,12 @@ got_privsep_send_tree_req(struct imsgbuf *ibuf, int fd,
 	if (wbuf == NULL)
 		return got_error_from_errno("imsg_create TREE_REQUEST");
 
-	if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1) {
-		err = got_error_from_errno("imsg_add TREE_REQUEST");
-		ibuf_free(wbuf);
-		return err;
-	}
+	if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1)
+		return got_error_from_errno("imsg_add TREE_REQUEST");
 
 	if (pack_idx != -1) { /* tree is packed */
-		if (imsg_add(wbuf, &pack_idx, sizeof(pack_idx)) == -1) {
-			err = got_error_from_errno("imsg_add TREE_REQUEST");
-			ibuf_free(wbuf);
-			return err;
-		}
+		if (imsg_add(wbuf, &pack_idx, sizeof(pack_idx)) == -1)
+			return got_error_from_errno("imsg_add TREE_REQUEST");
 	}
 
 	wbuf->fd = fd;
@@ -611,21 +598,12 @@ got_privsep_send_fetch_req(struct imsgbuf *ibuf, int fd,
 			return got_error_from_errno("imsg_create FETCH_HAVE_REF");
 
 		/* Keep in sync with struct got_imsg_fetch_have_ref! */
-		if (imsg_add(wbuf, id->sha1, sizeof(id->sha1)) == -1) {
-			err = got_error_from_errno("imsg_add FETCH_HAVE_REF");
-			ibuf_free(wbuf);
-			return err;
-		}
-		if (imsg_add(wbuf, &name_len, sizeof(name_len)) == -1) {
-			err = got_error_from_errno("imsg_add FETCH_HAVE_REF");
-			ibuf_free(wbuf);
-			return err;
-		}
-		if (imsg_add(wbuf, name, name_len) == -1) {
-			err = got_error_from_errno("imsg_add FETCH_HAVE_REF");
-			ibuf_free(wbuf);
-			return err;
-		}
+		if (imsg_add(wbuf, id->sha1, sizeof(id->sha1)) == -1)
+			return got_error_from_errno("imsg_add FETCH_HAVE_REF");
+		if (imsg_add(wbuf, &name_len, sizeof(name_len)) == -1)
+			return got_error_from_errno("imsg_add FETCH_HAVE_REF");
+		if (imsg_add(wbuf, name, name_len) == -1)
+			return got_error_from_errno("imsg_add FETCH_HAVE_REF");
 
 		wbuf->fd = -1;
 		imsg_close(ibuf, wbuf);
@@ -646,18 +624,12 @@ got_privsep_send_fetch_req(struct imsgbuf *ibuf, int fd,
 			    "imsg_create FETCH_WANTED_BRANCH");
 
 		/* Keep in sync with struct got_imsg_fetch_wanted_branch! */
-		if (imsg_add(wbuf, &name_len, sizeof(name_len)) == -1) {
-			err = got_error_from_errno(
+		if (imsg_add(wbuf, &name_len, sizeof(name_len)) == -1)
+			return got_error_from_errno(
 			    "imsg_add FETCH_WANTED_BRANCH");
-			ibuf_free(wbuf);
-			return err;
-		}
-		if (imsg_add(wbuf, name, name_len) == -1) {
-			err = got_error_from_errno(
+		if (imsg_add(wbuf, name, name_len) == -1)
+			return got_error_from_errno(
 			    "imsg_add FETCH_WANTED_BRANCH");
-			ibuf_free(wbuf);
-			return err;
-		}
 
 		wbuf->fd = -1;
 		imsg_close(ibuf, wbuf);
@@ -678,18 +650,12 @@ got_privsep_send_fetch_req(struct imsgbuf *ibuf, int fd,
 			    "imsg_create FETCH_WANTED_REF");
 
 		/* Keep in sync with struct got_imsg_fetch_wanted_ref! */
-		if (imsg_add(wbuf, &name_len, sizeof(name_len)) == -1) {
-			err = got_error_from_errno(
+		if (imsg_add(wbuf, &name_len, sizeof(name_len)) == -1)
+			return got_error_from_errno(
 			    "imsg_add FETCH_WANTED_REF");
-			ibuf_free(wbuf);
-			return err;
-		}
-		if (imsg_add(wbuf, name, name_len) == -1) {
-			err = got_error_from_errno(
+		if (imsg_add(wbuf, name, name_len) == -1)
+			return got_error_from_errno(
 			    "imsg_add FETCH_WANTED_REF");
-			ibuf_free(wbuf);
-			return err;
-		}
 
 		wbuf->fd = -1;
 		imsg_close(ibuf, wbuf);
@@ -868,7 +834,6 @@ static const struct got_error *
 send_send_ref(const char *name, size_t name_len, struct got_object_id *id,
     int delete, struct imsgbuf *ibuf)
 {
-	const struct got_error *err = NULL;
 	size_t len;
 	struct ibuf *wbuf;
 
@@ -878,26 +843,14 @@ send_send_ref(const char *name, size_t name_len, struct got_object_id *id,
 		return got_error_from_errno("imsg_create SEND_REF");
 
 	/* Keep in sync with struct got_imsg_send_ref! */
-	if (imsg_add(wbuf, id->sha1, sizeof(id->sha1)) == -1) {
-		err = got_error_from_errno("imsg_add SEND_REF");
-		ibuf_free(wbuf);
-		return err;
-	}
-	if (imsg_add(wbuf, &delete, sizeof(delete)) == -1) {
-		err = got_error_from_errno("imsg_add SEND_REF");
-		ibuf_free(wbuf);
-		return err;
-	}
-	if (imsg_add(wbuf, &name_len, sizeof(name_len)) == -1) {
-		err = got_error_from_errno("imsg_add SEND_REF");
-		ibuf_free(wbuf);
-		return err;
-	}
-	if (imsg_add(wbuf, name, name_len) == -1) {
-		err = got_error_from_errno("imsg_add SEND_REF");
-		ibuf_free(wbuf);
-		return err;
-	}
+	if (imsg_add(wbuf, id->sha1, sizeof(id->sha1)) == -1)
+		return got_error_from_errno("imsg_add SEND_REF");
+	if (imsg_add(wbuf, &delete, sizeof(delete)) == -1)
+		return got_error_from_errno("imsg_add SEND_REF");
+	if (imsg_add(wbuf, &name_len, sizeof(name_len)) == -1)
+		return got_error_from_errno("imsg_add SEND_REF");
+	if (imsg_add(wbuf, name, name_len) == -1)
+		return got_error_from_errno("imsg_add SEND_REF");
 
 	wbuf->fd = -1;
 	imsg_close(ibuf, wbuf);
@@ -1493,7 +1446,6 @@ static const struct got_error *
 send_tree_entries(struct imsgbuf *ibuf, struct got_parsed_tree_entry *entries,
     int idx0, int idxN, size_t len)
 {
-	static const struct got_error *err;
 	struct ibuf *wbuf;
 	struct got_imsg_tree_entries ientries;
 	int i;
@@ -1503,38 +1455,23 @@ send_tree_entries(struct imsgbuf *ibuf, struct got_parsed_tree_entry *entries,
 		return got_error_from_errno("imsg_create TREE_ENTRY");
 
 	ientries.nentries = idxN - idx0 + 1;
-	if (imsg_add(wbuf, &ientries, sizeof(ientries)) == -1) {
-		err = got_error_from_errno("imsg_add TREE_ENTRY");
-		ibuf_free(wbuf);
-		return err;
-	}
+	if (imsg_add(wbuf, &ientries, sizeof(ientries)) == -1)
+		return got_error_from_errno("imsg_add TREE_ENTRY");
 
 	for (i = idx0; i <= idxN; i++) {
 		struct got_parsed_tree_entry *pte = &entries[i];
 
 		/* Keep in sync with struct got_imsg_tree_object definition! */
-		if (imsg_add(wbuf, pte->id, SHA1_DIGEST_LENGTH) == -1) {
-			err = got_error_from_errno("imsg_add TREE_ENTRY");
-			ibuf_free(wbuf);
-			return err;
-		}
-		if (imsg_add(wbuf, &pte->mode, sizeof(pte->mode)) == -1) {
-			err = got_error_from_errno("imsg_add TREE_ENTRY");
-			ibuf_free(wbuf);
-			return err;
-		}
-		if (imsg_add(wbuf, &pte->namelen, sizeof(pte->namelen)) == -1) {
-			err = got_error_from_errno("imsg_add TREE_ENTRY");
-			ibuf_free(wbuf);
-			return err;
-		}
+		if (imsg_add(wbuf, pte->id, SHA1_DIGEST_LENGTH) == -1)
+			return got_error_from_errno("imsg_add TREE_ENTRY");
+		if (imsg_add(wbuf, &pte->mode, sizeof(pte->mode)) == -1)
+			return got_error_from_errno("imsg_add TREE_ENTRY");
+		if (imsg_add(wbuf, &pte->namelen, sizeof(pte->namelen)) == -1)
+			return got_error_from_errno("imsg_add TREE_ENTRY");
 		
 		/* Remaining bytes are the entry's name. */
-		if (imsg_add(wbuf, pte->name, pte->namelen) == -1) {
-			err = got_error_from_errno("imsg_add TREE_ENTRY");
-			ibuf_free(wbuf);
-			return err;
-		}
+		if (imsg_add(wbuf, pte->name, pte->namelen) == -1)
+			return got_error_from_errno("imsg_add TREE_ENTRY");
 	}
 
 	wbuf->fd = -1;
@@ -2693,7 +2630,6 @@ const struct got_error *
 got_privsep_send_commit_traversal_request(struct imsgbuf *ibuf,
     struct got_object_id *id, int idx, const char *path)
 {
-	const struct got_error *err = NULL;
 	struct ibuf *wbuf;
 	size_t path_len = strlen(path) + 1;
 
@@ -2702,21 +2638,15 @@ got_privsep_send_commit_traversal_request(struct imsgbuf *ibuf,
 	if (wbuf == NULL)
 		return got_error_from_errno(
 		    "imsg_create COMMIT_TRAVERSAL_REQUEST");
-	if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1) {
-		err = got_error_from_errno("imsg_add COMMIT_TRAVERSAL_REQUEST");
-		ibuf_free(wbuf);
-		return err;
-	}
-	if (imsg_add(wbuf, &idx, sizeof(idx)) == -1) {
-		err = got_error_from_errno("imsg_add COMMIT_TRAVERSAL_REQUEST");
-		ibuf_free(wbuf);
-		return err;
-	}
-	if (imsg_add(wbuf, path, path_len) == -1) {
-		err = got_error_from_errno("imsg_add COMMIT_TRAVERSAL_REQUEST");
-		ibuf_free(wbuf);
-		return err;
-	}
+	if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1)
+		return got_error_from_errno("imsg_add "
+		    "COMMIT_TRAVERSAL_REQUEST");
+	if (imsg_add(wbuf, &idx, sizeof(idx)) == -1)
+		return got_error_from_errno("imsg_add "
+		    "COMMIT_TRAVERSAL_REQUEST");
+	if (imsg_add(wbuf, path, path_len) == -1)
+		return got_error_from_errno("imsg_add "
+		    "COMMIT_TRAVERSAL_REQUEST");
 
 	wbuf->fd = -1;
 	imsg_close(ibuf, wbuf);
@@ -2924,19 +2854,13 @@ got_privsep_send_object_idlist(struct imsgbuf *ibuf,
 	}
 
 	idlist.nids = nids;
-	if (imsg_add(wbuf, &idlist, sizeof(idlist)) == -1) {
-		err = got_error_from_errno("imsg_add OBJ_ID_LIST");
-		ibuf_free(wbuf);
-		return err;
-	}
+	if (imsg_add(wbuf, &idlist, sizeof(idlist)) == -1)
+		return got_error_from_errno("imsg_add OBJ_ID_LIST");
 
 	for (i = 0; i < nids; i++) {
 		struct got_object_id *id = ids[i];
-		if (imsg_add(wbuf, id, sizeof(*id)) == -1) {
-			err = got_error_from_errno("imsg_add OBJ_ID_LIST");
-			ibuf_free(wbuf);
-			return err;
-		}
+		if (imsg_add(wbuf, id, sizeof(*id)) == -1)
+			return got_error_from_errno("imsg_add OBJ_ID_LIST");
 	}
 	
 	wbuf->fd = -1;
@@ -3036,19 +2960,13 @@ got_privsep_send_reused_deltas(struct imsgbuf *ibuf,
 	}
 
 	ideltas.ndeltas = ndeltas;
-	if (imsg_add(wbuf, &ideltas, sizeof(ideltas)) == -1) {
-		err = got_error_from_errno("imsg_add REUSED_DELTAS");
-		ibuf_free(wbuf);
-		return err;
-	}
+	if (imsg_add(wbuf, &ideltas, sizeof(ideltas)) == -1)
+		return got_error_from_errno("imsg_add REUSED_DELTAS");
 
 	for (i = 0; i < ndeltas; i++) {
 		struct got_imsg_reused_delta *delta = &deltas[i];
-		if (imsg_add(wbuf, delta, sizeof(*delta)) == -1) {
-			err = got_error_from_errno("imsg_add REUSED_DELTAS");
-			ibuf_free(wbuf);
-			return err;
-		}
+		if (imsg_add(wbuf, delta, sizeof(*delta)) == -1)
+			return got_error_from_errno("imsg_add REUSED_DELTAS");
 	}
 	
 	wbuf->fd = -1;
diff --git a/libexec/got-fetch-pack/got-fetch-pack.c b/libexec/got-fetch-pack/got-fetch-pack.c
index 68f212c..1cbce6b 100644
--- a/libexec/got-fetch-pack/got-fetch-pack.c
+++ b/libexec/got-fetch-pack/got-fetch-pack.c
@@ -216,7 +216,6 @@ fetch_error(const char *buf, size_t len)
 static const struct got_error *
 send_fetch_symrefs(struct imsgbuf *ibuf, struct got_pathlist_head *symrefs)
 {
-	const struct got_error *err = NULL;
 	struct ibuf *wbuf;
 	size_t len, nsymrefs = 0;
 	struct got_pathlist_entry *pe;
@@ -237,11 +236,8 @@ send_fetch_symrefs(struct imsgbuf *ibuf, struct got_pathlist_head *symrefs)
 		return got_error_from_errno("imsg_create FETCH_SYMREFS");
 
 	/* Keep in sync with struct got_imsg_fetch_symrefs definition! */
-	if (imsg_add(wbuf, &nsymrefs, sizeof(nsymrefs)) == -1) {
-		err = got_error_from_errno("imsg_add FETCH_SYMREFS");
-		ibuf_free(wbuf);
-		return err;
-	}
+	if (imsg_add(wbuf, &nsymrefs, sizeof(nsymrefs)) == -1)
+		return got_error_from_errno("imsg_add FETCH_SYMREFS");
 
 	TAILQ_FOREACH(pe, symrefs, entry) {
 		const char *name = pe->path;
@@ -250,26 +246,14 @@ send_fetch_symrefs(struct imsgbuf *ibuf, struct got_pathlist_head *symrefs)
 		size_t target_len = strlen(target);
 
 		/* Keep in sync with struct got_imsg_fetch_symref definition! */
-		if (imsg_add(wbuf, &name_len, sizeof(name_len)) == -1) {
-			err = got_error_from_errno("imsg_add FETCH_SYMREFS");
-			ibuf_free(wbuf);
-			return err;
-		}
-		if (imsg_add(wbuf, &target_len, sizeof(target_len)) == -1) {
-			err = got_error_from_errno("imsg_add FETCH_SYMREFS");
-			ibuf_free(wbuf);
-			return err;
-		}
-		if (imsg_add(wbuf, name, name_len) == -1) {
-			err = got_error_from_errno("imsg_add FETCH_SYMREFS");
-			ibuf_free(wbuf);
-			return err;
-		}
-		if (imsg_add(wbuf, target, target_len) == -1) {
-			err = got_error_from_errno("imsg_add FETCH_SYMREFS");
-			ibuf_free(wbuf);
-			return err;
-		}
+		if (imsg_add(wbuf, &name_len, sizeof(name_len)) == -1)
+			return got_error_from_errno("imsg_add FETCH_SYMREFS");
+		if (imsg_add(wbuf, &target_len, sizeof(target_len)) == -1)
+			return got_error_from_errno("imsg_add FETCH_SYMREFS");
+		if (imsg_add(wbuf, name, name_len) == -1)
+			return got_error_from_errno("imsg_add FETCH_SYMREFS");
+		if (imsg_add(wbuf, target, target_len) == -1)
+			return got_error_from_errno("imsg_add FETCH_SYMREFS");
 	}
 
 	wbuf->fd = -1;
@@ -281,7 +265,6 @@ static const struct got_error *
 send_fetch_ref(struct imsgbuf *ibuf, struct got_object_id *refid,
     const char *refname)
 {
-	const struct got_error *err = NULL;
 	struct ibuf *wbuf;
 	size_t len, reflen = strlen(refname);
 
@@ -294,16 +277,10 @@ send_fetch_ref(struct imsgbuf *ibuf, struct got_object_id *refid,
 		return got_error_from_errno("imsg_create FETCH_REF");
 
 	/* Keep in sync with struct got_imsg_fetch_ref definition! */
-	if (imsg_add(wbuf, refid->sha1, SHA1_DIGEST_LENGTH) == -1) {
-		err = got_error_from_errno("imsg_add FETCH_REF");
-		ibuf_free(wbuf);
-		return err;
-	}
-	if (imsg_add(wbuf, refname, reflen) == -1) {
-		err = got_error_from_errno("imsg_add FETCH_REF");
-		ibuf_free(wbuf);
-		return err;
-	}
+	if (imsg_add(wbuf, refid->sha1, SHA1_DIGEST_LENGTH) == -1)
+		return got_error_from_errno("imsg_add FETCH_REF");
+	if (imsg_add(wbuf, refname, reflen) == -1)
+		return got_error_from_errno("imsg_add FETCH_REF");
 
 	wbuf->fd = -1;
 	imsg_close(ibuf, wbuf);
diff --git a/libexec/got-read-gitconfig/got-read-gitconfig.c b/libexec/got-read-gitconfig/got-read-gitconfig.c
index a7d8b89..eb2cb32 100644
--- a/libexec/got-read-gitconfig/got-read-gitconfig.c
+++ b/libexec/got-read-gitconfig/got-read-gitconfig.c
@@ -131,31 +131,19 @@ send_gitconfig_remotes(struct imsgbuf *ibuf, struct got_remote_repo *remotes,
 			return got_error_from_errno(
 			    "imsg_create GITCONFIG_REMOTE");
 
-		if (imsg_add(wbuf, &iremote, sizeof(iremote)) == -1) {
-			err = got_error_from_errno(
+		if (imsg_add(wbuf, &iremote, sizeof(iremote)) == -1)
+			return got_error_from_errno(
 			    "imsg_add GITCONFIG_REMOTE");
-			ibuf_free(wbuf);
-			return err;
-		}
 
-		if (imsg_add(wbuf, remotes[i].name, iremote.name_len) == -1) {
-			err = got_error_from_errno(
+		if (imsg_add(wbuf, remotes[i].name, iremote.name_len) == -1)
+			return got_error_from_errno(
 			    "imsg_add GITCONFIG_REMOTE");
-			ibuf_free(wbuf);
-			return err;
-		}
-		if (imsg_add(wbuf, remotes[i].fetch_url, iremote.fetch_url_len) == -1) {
-			err = got_error_from_errno(
+		if (imsg_add(wbuf, remotes[i].fetch_url, iremote.fetch_url_len) == -1)
+			return got_error_from_errno(
 			    "imsg_add GITCONFIG_REMOTE");
-			ibuf_free(wbuf);
-			return err;
-		}
-		if (imsg_add(wbuf, remotes[i].send_url, iremote.send_url_len) == -1) {
-			err = got_error_from_errno(
+		if (imsg_add(wbuf, remotes[i].send_url, iremote.send_url_len) == -1)
+			return got_error_from_errno(
 			    "imsg_add GITCONFIG_REMOTE");
-			ibuf_free(wbuf);
-			return err;
-		}
 
 		wbuf->fd = -1;
 		imsg_close(ibuf, wbuf);
diff --git a/libexec/got-read-gotconfig/got-read-gotconfig.c b/libexec/got-read-gotconfig/got-read-gotconfig.c
index 3eb4560..aa2c975 100644
--- a/libexec/got-read-gotconfig/got-read-gotconfig.c
+++ b/libexec/got-read-gotconfig/got-read-gotconfig.c
@@ -310,26 +310,22 @@ send_gotconfig_remotes(struct imsgbuf *ibuf,
 		if (imsg_add(wbuf, &iremote, sizeof(iremote)) == -1) {
 			err = got_error_from_errno(
 			    "imsg_add GOTCONFIG_REMOTE");
-			ibuf_free(wbuf);
 			break;
 		}
 
 		if (imsg_add(wbuf, repo->name, iremote.name_len) == -1) {
 			err = got_error_from_errno(
 			    "imsg_add GOTCONFIG_REMOTE");
-			ibuf_free(wbuf);
 			break;
 		}
 		if (imsg_add(wbuf, fetch_url, iremote.fetch_url_len) == -1) {
 			err = got_error_from_errno(
 			    "imsg_add GOTCONFIG_REMOTE");
-			ibuf_free(wbuf);
 			break;
 		}
 		if (imsg_add(wbuf, send_url, iremote.send_url_len) == -1) {
 			err = got_error_from_errno(
 			    "imsg_add GOTCONFIG_REMOTE");
-			ibuf_free(wbuf);
 			break;
 		}
 
diff --git a/libexec/got-read-pack/got-read-pack.c b/libexec/got-read-pack/got-read-pack.c
index b6d3c89..ea9a7e5 100644
--- a/libexec/got-read-pack/got-read-pack.c
+++ b/libexec/got-read-pack/got-read-pack.c
@@ -551,7 +551,6 @@ static const struct got_error *
 send_traversed_commits(struct got_object_id *commit_ids, size_t ncommits,
     struct imsgbuf *ibuf)
 {
-	const struct got_error *err;
 	struct ibuf *wbuf;
 	size_t i;
 
@@ -561,18 +560,14 @@ send_traversed_commits(struct got_object_id *commit_ids, size_t ncommits,
 	if (wbuf == NULL)
 		return got_error_from_errno("imsg_create TRAVERSED_COMMITS");
 
-	if (imsg_add(wbuf, &ncommits, sizeof(ncommits)) == -1) {
-		err = got_error_from_errno("imsg_add TRAVERSED_COMMITS");
-		ibuf_free(wbuf);
-		return err;
-	}
+	if (imsg_add(wbuf, &ncommits, sizeof(ncommits)) == -1)
+		return got_error_from_errno("imsg_add TRAVERSED_COMMITS");
+
 	for (i = 0; i < ncommits; i++) {
 		struct got_object_id *id = &commit_ids[i];
 		if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1) {
-			err = got_error_from_errno(
+			return got_error_from_errno(
 			    "imsg_add TRAVERSED_COMMITS");
-			ibuf_free(wbuf);
-			return err;
 		}
 	}
 
diff --git a/libexec/got-send-pack/got-send-pack.c b/libexec/got-send-pack/got-send-pack.c
index e3a4b18..1f78b77 100644
--- a/libexec/got-send-pack/got-send-pack.c
+++ b/libexec/got-send-pack/got-send-pack.c
@@ -195,7 +195,6 @@ static const struct got_error *
 send_their_ref(struct imsgbuf *ibuf, struct got_object_id *refid,
     const char *refname)
 {
-	const struct got_error *err = NULL;
 	struct ibuf *wbuf;
 	size_t len, reflen = strlen(refname);
 
@@ -208,21 +207,12 @@ send_their_ref(struct imsgbuf *ibuf, struct got_object_id *refid,
 		return got_error_from_errno("imsg_create SEND_REMOTE_REF");
 
 	/* Keep in sync with struct got_imsg_send_remote_ref definition! */
-	if (imsg_add(wbuf, refid->sha1, SHA1_DIGEST_LENGTH) == -1) {
-		err = got_error_from_errno("imsg_add SEND_REMOTE_REF");
-		ibuf_free(wbuf);
-		return err;
-	}
-	if (imsg_add(wbuf, &reflen, sizeof(reflen)) == -1) {
-		err = got_error_from_errno("imsg_add SEND_REMOTE_REF");
-		ibuf_free(wbuf);
-		return err;
-	}
-	if (imsg_add(wbuf, refname, reflen) == -1) {
-		err = got_error_from_errno("imsg_add SEND_REMOTE_REF");
-		ibuf_free(wbuf);
-		return err;
-	}
+	if (imsg_add(wbuf, refid->sha1, SHA1_DIGEST_LENGTH) == -1)
+		return got_error_from_errno("imsg_add SEND_REMOTE_REF");
+	if (imsg_add(wbuf, &reflen, sizeof(reflen)) == -1)
+		return got_error_from_errno("imsg_add SEND_REMOTE_REF");
+	if (imsg_add(wbuf, refname, reflen) == -1)
+		return got_error_from_errno("imsg_add SEND_REMOTE_REF");
 
 	wbuf->fd = -1;
 	imsg_close(ibuf, wbuf);
@@ -233,7 +223,6 @@ static const struct got_error *
 send_ref_status(struct imsgbuf *ibuf, const char *refname, int success,
     struct got_pathlist_head *refs, struct got_pathlist_head *delete_refs)
 {
-	const struct got_error *err = NULL;
 	struct ibuf *wbuf;
 	size_t len, reflen = strlen(refname);
 	struct got_pathlist_entry *pe;
@@ -276,21 +265,12 @@ send_ref_status(struct imsgbuf *ibuf, const char *refname, int success,
 		return got_error_from_errno("imsg_create SEND_REF_STATUS");
 
 	/* Keep in sync with struct got_imsg_send_ref_status definition! */
-	if (imsg_add(wbuf, &success, sizeof(success)) == -1) {
-		err = got_error_from_errno("imsg_add SEND_REF_STATUS");
-		ibuf_free(wbuf);
-		return err;
-	}
-	if (imsg_add(wbuf, &reflen, sizeof(reflen)) == -1) {
-		err = got_error_from_errno("imsg_add SEND_REF_STATUS");
-		ibuf_free(wbuf);
-		return err;
-	}
-	if (imsg_add(wbuf, refname, reflen) == -1) {
-		err = got_error_from_errno("imsg_add SEND_REF_STATUS");
-		ibuf_free(wbuf);
-		return err;
-	}
+	if (imsg_add(wbuf, &success, sizeof(success)) == -1)
+		return got_error_from_errno("imsg_add SEND_REF_STATUS");
+	if (imsg_add(wbuf, &reflen, sizeof(reflen)) == -1)
+		return got_error_from_errno("imsg_add SEND_REF_STATUS");
+	if (imsg_add(wbuf, refname, reflen) == -1)
+		return got_error_from_errno("imsg_add SEND_REF_STATUS");
 
 	wbuf->fd = -1;
 	imsg_close(ibuf, wbuf);