Commit 8aa04a37d180240edb68db4267a0895ce38e9fc3

Patrick Steinhardt 2020-02-19T12:14:16

Merge pull request #5391 from pks-t/pks/coverity-fixes Coverity fixes

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
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
diff --git a/src/cache.c b/src/cache.c
index 32ba993..af42b39 100644
--- a/src/cache.c
+++ b/src/cache.c
@@ -208,10 +208,14 @@ static void *cache_store(git_cache *cache, git_cached_obj *entry)
 			entry = stored_entry;
 		} else if (stored_entry->flags == GIT_CACHE_STORE_RAW &&
 			   entry->flags == GIT_CACHE_STORE_PARSED) {
-			git_cached_obj_decref(stored_entry);
-			git_cached_obj_incref(entry);
-
-			git_oidmap_set(cache->map, &entry->oid, entry);
+			if (git_oidmap_set(cache->map, &entry->oid, entry) == 0) {
+				git_cached_obj_decref(stored_entry);
+				git_cached_obj_incref(entry);
+			} else {
+				git_cached_obj_decref(entry);
+				git_cached_obj_incref(stored_entry);
+				entry = stored_entry;
+			}
 		} else {
 			/* NO OP */
 		}
diff --git a/src/indexer.c b/src/indexer.c
index 717549f..68fdd85 100644
--- a/src/indexer.c
+++ b/src/indexer.c
@@ -150,11 +150,11 @@ int git_indexer_new(
 	idx->progress_cb = opts.progress_cb;
 	idx->progress_payload = opts.progress_cb_payload;
 	idx->mode = mode ? mode : GIT_PACK_FILE_MODE;
-	git_hash_ctx_init(&idx->hash_ctx);
-	git_hash_ctx_init(&idx->trailer);
 	git_buf_init(&idx->entry_data, 0);
 
-	if ((error = git_oidmap_new(&idx->expected_oids)) < 0)
+	if ((error = git_hash_ctx_init(&idx->hash_ctx)) < 0 ||
+	    (error = git_hash_ctx_init(&idx->trailer)) < 0 ||
+	    (error = git_oidmap_new(&idx->expected_oids)) < 0)
 		goto cleanup;
 
 	idx->do_verify = opts.verify;
diff --git a/src/notes.c b/src/notes.c
index 4633a16..68d2ae9 100644
--- a/src/notes.c
+++ b/src/notes.c
@@ -808,8 +808,11 @@ int git_note_next(
 
 	git_oid_cpy(note_id, &item->id);
 
-	if (!(error = process_entry_path(item->path, annotated_id)))
-		git_iterator_advance(NULL, it);
+	if ((error = process_entry_path(item->path, annotated_id)) < 0)
+		return error;
 
-	return error;
+	if ((error = git_iterator_advance(NULL, it)) < 0 && error != GIT_ITEROVER)
+		return error;
+
+	return 0;
 }
diff --git a/src/pack-objects.c b/src/pack-objects.c
index bdd5171..49b4e47 100644
--- a/src/pack-objects.c
+++ b/src/pack-objects.c
@@ -374,7 +374,9 @@ static int write_object(
 		GIT_ERROR_CHECK_ALLOC(zbuf);
 
 		git_zstream_reset(&pb->zstream);
-		git_zstream_set_input(&pb->zstream, data, data_len);
+
+		if ((error = git_zstream_set_input(&pb->zstream, data, data_len)) < 0)
+			goto done;
 
 		while (!git_zstream_done(&pb->zstream)) {
 			if ((error = git_zstream_get_output(zbuf, &zbuf_len, &pb->zstream)) < 0 ||
diff --git a/src/push.c b/src/push.c
index 67ebcfb..34867c2 100644
--- a/src/push.c
+++ b/src/push.c
@@ -349,8 +349,9 @@ static int queue_objects(git_push *push)
 		if (git_oid_is_zero(&head->oid))
 			continue;
 
-		/* TODO */
-		git_revwalk_hide(rw, &head->oid);
+		if ((error = git_revwalk_hide(rw, &head->oid)) < 0 &&
+		    error != GIT_ENOTFOUND && error != GIT_EINVALIDSPEC && error != GIT_EPEEL)
+			goto on_error;
 	}
 
 	error = git_packbuilder_insert_walk(push->pb, rw);
diff --git a/src/repository.c b/src/repository.c
index 2469e13..fe0d696 100644
--- a/src/repository.c
+++ b/src/repository.c
@@ -189,19 +189,25 @@ void git_repository_free(git_repository *repo)
  *
  * Open a repository object from its path
  */
-static bool valid_repository_path(git_buf *repository_path, git_buf *common_path)
+static int is_valid_repository_path(bool *out, git_buf *repository_path, git_buf *common_path)
 {
+	int error;
+
+	*out = false;
+
 	/* Check if we have a separate commondir (e.g. we have a
 	 * worktree) */
 	if (git_path_contains_file(repository_path, GIT_COMMONDIR_FILE)) {
 		git_buf common_link  = GIT_BUF_INIT;
-		git_buf_joinpath(&common_link, repository_path->ptr, GIT_COMMONDIR_FILE);
 
-		git_futils_readbuffer(&common_link, common_link.ptr);
-		git_buf_rtrim(&common_link);
+		if ((error = git_buf_joinpath(&common_link, repository_path->ptr, GIT_COMMONDIR_FILE)) < 0 ||
+		    (error = git_futils_readbuffer(&common_link, common_link.ptr)) < 0)
+			return error;
 
+		git_buf_rtrim(&common_link);
 		if (git_path_is_relative(common_link.ptr)) {
-			git_buf_joinpath(common_path, repository_path->ptr, common_link.ptr);
+			if ((error = git_buf_joinpath(common_path, repository_path->ptr, common_link.ptr)) < 0)
+				return error;
 		} else {
 			git_buf_swap(common_path, &common_link);
 		}
@@ -209,24 +215,26 @@ static bool valid_repository_path(git_buf *repository_path, git_buf *common_path
 		git_buf_dispose(&common_link);
 	}
 	else {
-		git_buf_set(common_path, repository_path->ptr, repository_path->size);
+		if ((error = git_buf_set(common_path, repository_path->ptr, repository_path->size)) < 0)
+			return error;
 	}
 
 	/* Make sure the commondir path always has a trailing * slash */
 	if (git_buf_rfind(common_path, '/') != (ssize_t)common_path->size - 1)
-		git_buf_putc(common_path, '/');
+		if ((error = git_buf_putc(common_path, '/')) < 0)
+			return error;
 
 	/* Ensure HEAD file exists */
 	if (git_path_contains_file(repository_path, GIT_HEAD_FILE) == false)
-		return false;
-
+		return 0;
 	/* Check files in common dir */
 	if (git_path_contains_dir(common_path, GIT_OBJECTS_DIR) == false)
-		return false;
+		return 0;
 	if (git_path_contains_dir(common_path, GIT_REFS_DIR) == false)
-		return false;
+		return 0;
 
-	return true;
+	*out = true;
+	return 0;
 }
 
 static git_repository *repository_alloc(void)
@@ -441,15 +449,15 @@ static int find_repo(
 	uint32_t flags,
 	const char *ceiling_dirs)
 {
-	int error;
 	git_buf path = GIT_BUF_INIT;
 	git_buf repo_link = GIT_BUF_INIT;
 	git_buf common_link = GIT_BUF_INIT;
 	struct stat st;
 	dev_t initial_device = 0;
 	int min_iterations;
-	bool in_dot_git;
+	bool in_dot_git, is_valid;
 	size_t ceiling_offset = 0;
+	int error;
 
 	git_buf_clear(gitdir_path);
 
@@ -475,9 +483,8 @@ static int find_repo(
 	for (;;) {
 		if (!(flags & GIT_REPOSITORY_OPEN_NO_DOTGIT)) {
 			if (!in_dot_git) {
-				error = git_buf_joinpath(&path, path.ptr, DOT_GIT);
-				if (error < 0)
-					break;
+				if ((error = git_buf_joinpath(&path, path.ptr, DOT_GIT)) < 0)
+					goto out;
 			}
 			in_dot_git = !in_dot_git;
 		}
@@ -491,28 +498,33 @@ static int find_repo(
 				break;
 
 			if (S_ISDIR(st.st_mode)) {
-				if (valid_repository_path(&path, &common_link)) {
-					git_path_to_dir(&path);
-					git_buf_set(gitdir_path, path.ptr, path.size);
+				if ((error = is_valid_repository_path(&is_valid, &path, &common_link)) < 0)
+					goto out;
+
+				if (is_valid) {
+					if ((error = git_path_to_dir(&path)) < 0 ||
+					    (error = git_buf_set(gitdir_path, path.ptr, path.size)) < 0)
+						goto out;
 
 					if (gitlink_path)
-						git_buf_attach(gitlink_path,
-							git_worktree__read_link(path.ptr, GIT_GITDIR_FILE), 0);
+						if ((error = git_buf_attach(gitlink_path, git_worktree__read_link(path.ptr, GIT_GITDIR_FILE), 0)) < 0)
+							goto out;
 					if (commondir_path)
 						git_buf_swap(&common_link, commondir_path);
 
 					break;
 				}
-			}
-			else if (S_ISREG(st.st_mode) && git__suffixcmp(path.ptr, "/" DOT_GIT) == 0) {
-				error = read_gitfile(&repo_link, path.ptr);
-				if (error < 0)
-					break;
-				if (valid_repository_path(&repo_link, &common_link)) {
+			} else if (S_ISREG(st.st_mode) && git__suffixcmp(path.ptr, "/" DOT_GIT) == 0) {
+				if ((error = read_gitfile(&repo_link, path.ptr)) < 0 ||
+				    (error = is_valid_repository_path(&is_valid, &repo_link, &common_link)) < 0)
+					goto out;
+
+				if (is_valid) {
 					git_buf_swap(gitdir_path, &repo_link);
 
 					if (gitlink_path)
-						error = git_buf_put(gitlink_path, path.ptr, path.size);
+						if ((error = git_buf_put(gitlink_path, path.ptr, path.size)) < 0)
+							goto out;
 					if (commondir_path)
 						git_buf_swap(&common_link, commondir_path);
 				}
@@ -523,10 +535,8 @@ static int find_repo(
 		/* Move up one directory. If we're in_dot_git, we'll search the
 		 * parent itself next. If we're !in_dot_git, we'll search .git
 		 * in the parent directory next (added at the top of the loop). */
-		if (git_path_dirname_r(&path, path.ptr) < 0) {
-			error = -1;
-			break;
-		}
+		if ((error = git_path_dirname_r(&path, path.ptr)) < 0)
+			goto out;
 
 		/* Once we've checked the directory (and .git if applicable),
 		 * find the ceiling for a search. */
@@ -534,31 +544,28 @@ static int find_repo(
 			ceiling_offset = find_ceiling_dir_offset(path.ptr, ceiling_dirs);
 
 		/* Check if we should stop searching here. */
-		if (min_iterations == 0
-		    && (path.ptr[ceiling_offset] == 0
-			|| (flags & GIT_REPOSITORY_OPEN_NO_SEARCH)))
+		if (min_iterations == 0 &&
+		    (path.ptr[ceiling_offset] == 0 || (flags & GIT_REPOSITORY_OPEN_NO_SEARCH)))
 			break;
 	}
 
-	if (!error && workdir_path && !(flags & GIT_REPOSITORY_OPEN_BARE)) {
+	if (workdir_path && !(flags & GIT_REPOSITORY_OPEN_BARE)) {
 		if (!git_buf_len(gitdir_path))
 			git_buf_clear(workdir_path);
-		else {
-			git_path_dirname_r(workdir_path, path.ptr);
-			git_path_to_dir(workdir_path);
-		}
-		if (git_buf_oom(workdir_path))
-			return -1;
+		else if ((error = git_path_dirname_r(workdir_path, path.ptr)) < 0 ||
+			 (error = git_path_to_dir(workdir_path)) < 0)
+			goto out;
 	}
 
 	/* If we didn't find the repository, and we don't have any other error
 	 * to report, report that. */
-	if (!git_buf_len(gitdir_path) && !error) {
-		git_error_set(GIT_ERROR_REPOSITORY,
-			"could not find repository from '%s'", start_path);
+	if (!git_buf_len(gitdir_path)) {
+		git_error_set(GIT_ERROR_REPOSITORY, "could not find repository from '%s'", start_path);
 		error = GIT_ENOTFOUND;
+		goto out;
 	}
 
+out:
 	git_buf_dispose(&path);
 	git_buf_dispose(&repo_link);
 	git_buf_dispose(&common_link);
@@ -569,14 +576,16 @@ int git_repository_open_bare(
 	git_repository **repo_ptr,
 	const char *bare_path)
 {
-	int error;
 	git_buf path = GIT_BUF_INIT, common_path = GIT_BUF_INIT;
 	git_repository *repo = NULL;
+	bool is_valid;
+	int error;
 
-	if ((error = git_path_prettify_dir(&path, bare_path, NULL)) < 0)
+	if ((error = git_path_prettify_dir(&path, bare_path, NULL)) < 0 ||
+	    (error = is_valid_repository_path(&is_valid, &path, &common_path)) < 0)
 		return error;
 
-	if (!valid_repository_path(&path, &common_path)) {
+	if (!is_valid) {
 		git_buf_dispose(&path);
 		git_buf_dispose(&common_path);
 		git_error_set(GIT_ERROR_REPOSITORY, "path is not a repository: %s", bare_path);
@@ -2055,6 +2064,7 @@ int git_repository_init_ext(
 	git_buf repo_path = GIT_BUF_INIT, wd_path = GIT_BUF_INIT,
 		common_path = GIT_BUF_INIT, head_path = GIT_BUF_INIT;
 	const char *wd;
+	bool is_valid;
 	int error;
 
 	assert(out && given_repo && opts);
@@ -2066,7 +2076,10 @@ int git_repository_init_ext(
 
 	wd = (opts->flags & GIT_REPOSITORY_INIT_BARE) ? NULL : git_buf_cstr(&wd_path);
 
-	if (valid_repository_path(&repo_path, &common_path)) {
+	if ((error = is_valid_repository_path(&is_valid, &repo_path, &common_path)) < 0)
+		goto out;
+
+	if (is_valid) {
 		if ((opts->flags & GIT_REPOSITORY_INIT_NO_REINIT) != 0) {
 			git_error_set(GIT_ERROR_REPOSITORY,
 				"attempt to reinitialize '%s'", given_repo);
diff --git a/src/revwalk.c b/src/revwalk.c
index 4587b5a..abbd65a 100644
--- a/src/revwalk.c
+++ b/src/revwalk.c
@@ -58,7 +58,7 @@ int git_revwalk__push_commit(git_revwalk *walk, const git_oid *oid, const git_re
 			return 0;
 
 		git_error_set(GIT_ERROR_INVALID, "object is not a committish");
-		return -1;
+		return error;
 	}
 	if (error < 0)
 		return error;
diff --git a/src/streams/openssl.c b/src/streams/openssl.c
index 458810c..5b66352 100644
--- a/src/streams/openssl.c
+++ b/src/streams/openssl.c
@@ -156,7 +156,7 @@ static void openssl_locking_function(
 	lock = mode & CRYPTO_LOCK;
 
 	if (lock) {
-		git_mutex_lock(&openssl_locks[n]);
+		(void)git_mutex_lock(&openssl_locks[n]);
 	} else {
 		git_mutex_unlock(&openssl_locks[n]);
 	}
diff --git a/src/worktree.c b/src/worktree.c
index ef4ebfd..e171afb 100644
--- a/src/worktree.c
+++ b/src/worktree.c
@@ -136,11 +136,11 @@ static int open_worktree_dir(git_worktree **out, const char *parent, const char 
 		goto out;
 	}
 
-	if ((wt->name = git__strdup(name)) == NULL
-	    || (wt->commondir_path = git_worktree__read_link(dir, "commondir")) == NULL
-	    || (wt->gitlink_path = git_worktree__read_link(dir, "gitdir")) == NULL
-	    || (parent && (wt->parent_path = git__strdup(parent)) == NULL)
-	    || (wt->worktree_path = git_path_dirname(wt->gitlink_path)) == NULL) {
+	if ((wt->name = git__strdup(name)) == NULL ||
+	    (wt->commondir_path = git_worktree__read_link(dir, "commondir")) == NULL ||
+	    (wt->gitlink_path = git_worktree__read_link(dir, "gitdir")) == NULL ||
+	    (parent && (wt->parent_path = git__strdup(parent)) == NULL) ||
+	    (wt->worktree_path = git_path_dirname(wt->gitlink_path)) == NULL) {
 		error = -1;
 		goto out;
 	}
@@ -149,7 +149,10 @@ static int open_worktree_dir(git_worktree **out, const char *parent, const char 
 		goto out;
 	wt->gitdir_path = git_buf_detach(&gitdir);
 
-	wt->locked = !!git_worktree_is_locked(NULL, wt);
+	if ((error = git_worktree_is_locked(NULL, wt)) < 0)
+		goto out;
+	wt->locked = !!error;
+	error = 0;
 
 	*out = wt;
 
@@ -403,20 +406,24 @@ out:
 int git_worktree_lock(git_worktree *wt, const char *reason)
 {
 	git_buf buf = GIT_BUF_INIT, path = GIT_BUF_INIT;
-	int err;
+	int error;
 
 	assert(wt);
 
-	if ((err = git_worktree_is_locked(NULL, wt)) < 0)
+	if ((error = git_worktree_is_locked(NULL, wt)) < 0)
+		goto out;
+	if (error) {
+		error = GIT_ELOCKED;
 		goto out;
+	}
 
-	if ((err = git_buf_joinpath(&path, wt->gitdir_path, "locked")) < 0)
+	if ((error = git_buf_joinpath(&path, wt->gitdir_path, "locked")) < 0)
 		goto out;
 
 	if (reason)
 		git_buf_attach_notowned(&buf, reason, strlen(reason));
 
-	if ((err = git_futils_writebuffer(&buf, path.ptr, O_CREAT|O_EXCL|O_WRONLY, 0644)) < 0)
+	if ((error = git_futils_writebuffer(&buf, path.ptr, O_CREAT|O_EXCL|O_WRONLY, 0644)) < 0)
 		goto out;
 
 	wt->locked = 1;
@@ -424,16 +431,19 @@ int git_worktree_lock(git_worktree *wt, const char *reason)
 out:
 	git_buf_dispose(&path);
 
-	return err;
+	return error;
 }
 
 int git_worktree_unlock(git_worktree *wt)
 {
 	git_buf path = GIT_BUF_INIT;
+	int error;
 
 	assert(wt);
 
-	if (!git_worktree_is_locked(NULL, wt))
+	if ((error = git_worktree_is_locked(NULL, wt)) < 0)
+		return error;
+	if (!error)
 		return 1;
 
 	if (git_buf_joinpath(&path, wt->gitdir_path, "locked") < 0)
@@ -454,22 +464,25 @@ int git_worktree_unlock(git_worktree *wt)
 int git_worktree_is_locked(git_buf *reason, const git_worktree *wt)
 {
 	git_buf path = GIT_BUF_INIT;
-	int ret;
+	int error, locked;
 
 	assert(wt);
 
 	if (reason)
 		git_buf_clear(reason);
 
-	if ((ret = git_buf_joinpath(&path, wt->gitdir_path, "locked")) < 0)
+	if ((error = git_buf_joinpath(&path, wt->gitdir_path, "locked")) < 0)
+		goto out;
+	locked = git_path_exists(path.ptr);
+	if (locked && reason &&
+	    (error = git_futils_readbuffer(reason, path.ptr)) < 0)
 		goto out;
-	if ((ret = git_path_exists(path.ptr)) && reason)
-		git_futils_readbuffer(reason, path.ptr);
 
+	error = locked;
 out:
 	git_buf_dispose(&path);
 
-	return ret;
+	return error;
 }
 
 const char *git_worktree_name(const git_worktree *wt)
@@ -502,7 +515,6 @@ int git_worktree_pruneinit_options(git_worktree_prune_options *opts,
 int git_worktree_is_prunable(git_worktree *wt,
 	git_worktree_prune_options *opts)
 {
-	git_buf reason = GIT_BUF_INIT;
 	git_worktree_prune_options popts = GIT_WORKTREE_PRUNE_OPTIONS_INIT;
 
 	GIT_ERROR_CHECK_VERSION(
@@ -512,20 +524,24 @@ int git_worktree_is_prunable(git_worktree *wt,
 	if (opts)
 		memcpy(&popts, opts, sizeof(popts));
 
-	if ((popts.flags & GIT_WORKTREE_PRUNE_LOCKED) == 0 &&
-		git_worktree_is_locked(&reason, wt))
-	{
-		if (!reason.size)
-			git_buf_attach_notowned(&reason, "no reason given", 15);
-		git_error_set(GIT_ERROR_WORKTREE, "not pruning locked working tree: '%s'", reason.ptr);
-		git_buf_dispose(&reason);
+	if ((popts.flags & GIT_WORKTREE_PRUNE_LOCKED) == 0) {
+		git_buf reason = GIT_BUF_INIT;
+		int error;
 
-		return 0;
+		if ((error = git_worktree_is_locked(&reason, wt)) < 0)
+			return error;
+
+		if (error) {
+			if (!reason.size)
+				git_buf_attach_notowned(&reason, "no reason given", 15);
+			git_error_set(GIT_ERROR_WORKTREE, "not pruning locked working tree: '%s'", reason.ptr);
+			git_buf_dispose(&reason);
+			return 0;
+		}
 	}
 
 	if ((popts.flags & GIT_WORKTREE_PRUNE_VALID) == 0 &&
-		git_worktree_validate(wt) == 0)
-	{
+	    git_worktree_validate(wt) == 0) {
 		git_error_set(GIT_ERROR_WORKTREE, "not pruning valid working tree");
 		return 0;
 	}
diff --git a/tests/blame/blame_helpers.c b/tests/blame/blame_helpers.c
index 61e8735..6b3ce67 100644
--- a/tests/blame/blame_helpers.c
+++ b/tests/blame/blame_helpers.c
@@ -31,13 +31,13 @@ void check_blame_hunk_index(git_repository *repo, git_blame *blame, int idx,
 	}
 
 	if (hunk->final_start_line_number != start_line) {
-		hunk_message(idx, hunk, "mismatched start line number: expected %d, got %d",
+		hunk_message(idx, hunk, "mismatched start line number: expected %"PRIuZ", got %"PRIuZ,
 				start_line, hunk->final_start_line_number);
 	}
 	cl_assert_equal_i(hunk->final_start_line_number, start_line);
 
 	if (hunk->lines_in_hunk != len) {
-		hunk_message(idx, hunk, "mismatched line count: expected %d, got %d",
+		hunk_message(idx, hunk, "mismatched line count: expected %"PRIuZ", got %"PRIuZ,
 				len, hunk->lines_in_hunk);
 	}
 	cl_assert_equal_i(hunk->lines_in_hunk, len);
diff --git a/tests/blame/blame_helpers.h b/tests/blame/blame_helpers.h
index fd5a35d..5b34b4a 100644
--- a/tests/blame/blame_helpers.h
+++ b/tests/blame/blame_helpers.h
@@ -1,7 +1,7 @@
 #include "clar_libgit2.h"
 #include "blame.h"
 
-void hunk_message(size_t idx, const git_blame_hunk *hunk, const char *fmt, ...);
+void hunk_message(size_t idx, const git_blame_hunk *hunk, const char *fmt, ...) GIT_FORMAT_PRINTF(3, 4);
 
 void check_blame_hunk_index(
 		git_repository *repo,
diff --git a/tests/config/global.c b/tests/config/global.c
index b64b716..ed47d02 100644
--- a/tests/config/global.c
+++ b/tests/config/global.c
@@ -81,7 +81,7 @@ void test_config_global__lock_missing_global_config(void)
 	git_config_entry *entry;
 	git_transaction *transaction;
 
-	p_unlink("home/.gitconfig"); /* No global config */
+	(void)p_unlink("home/.gitconfig"); /* No global config */
 
 	cl_git_pass(git_config_open_default(&cfg));
 	cl_git_pass(git_config_lock(&transaction, cfg));
diff --git a/tests/config/new.c b/tests/config/new.c
index 2f5d83d..22c330f 100644
--- a/tests/config/new.c
+++ b/tests/config/new.c
@@ -30,5 +30,5 @@ void test_config_new__write_new_config(void)
 	git_buf_dispose(&buf);
 	git_config_free(config);
 
-	p_unlink(TEST_CONFIG);
+	cl_must_pass(p_unlink(TEST_CONFIG));
 }
diff --git a/tests/ignore/path.c b/tests/ignore/path.c
index 864fba4..e23ac77 100644
--- a/tests/ignore/path.c
+++ b/tests/ignore/path.c
@@ -255,7 +255,7 @@ void test_ignore_path__globs_without_star(void)
 void test_ignore_path__skip_gitignore_directory(void)
 {
 	cl_git_rewritefile("attr/.git/info/exclude", "/NewFolder\n/NewFolder/NewFolder");
-	p_unlink("attr/.gitignore");
+	cl_must_pass(p_unlink("attr/.gitignore"));
 	cl_assert(!git_path_exists("attr/.gitignore"));
 	p_mkdir("attr/.gitignore", 0777);
 	cl_git_mkfile("attr/.gitignore/garbage.txt", "new_file\n");
@@ -268,12 +268,11 @@ void test_ignore_path__skip_gitignore_directory(void)
 
 void test_ignore_path__subdirectory_gitignore(void)
 {
-	p_unlink("attr/.gitignore");
+	cl_must_pass(p_unlink("attr/.gitignore"));
 	cl_assert(!git_path_exists("attr/.gitignore"));
 	cl_git_mkfile(
 		"attr/.gitignore",
 		"file1\n");
-	p_mkdir("attr/dir", 0777);
 	cl_git_mkfile(
 		"attr/dir/.gitignore",
 		"file2/\n");
diff --git a/tests/index/nsec.c b/tests/index/nsec.c
index dee1509..6edcf03 100644
--- a/tests/index/nsec.c
+++ b/tests/index/nsec.c
@@ -54,7 +54,7 @@ static bool should_expect_nsecs(void)
 
 	expect = try_create_file_with_nsec_timestamp(nsec_path.ptr);
 
-	p_unlink(nsec_path.ptr);
+	cl_must_pass(p_unlink(nsec_path.ptr));
 
 	git_buf_dispose(&nsec_path);
 
diff --git a/tests/index/tests.c b/tests/index/tests.c
index 4c9deaa..d9d8371 100644
--- a/tests/index/tests.c
+++ b/tests/index/tests.c
@@ -566,8 +566,7 @@ void test_index_tests__cannot_add_invalid_filename(void)
 {
 	git_repository *repo;
 
-	p_mkdir("invalid", 0700);
-
+	cl_must_pass(p_mkdir("invalid", 0700));
 	cl_git_pass(git_repository_init(&repo, "./invalid", 0));
 	cl_must_pass(p_mkdir("./invalid/subdir", 0777));
 
diff --git a/tests/network/refspecs.c b/tests/network/refspecs.c
index 7347590..5c8eb15 100644
--- a/tests/network/refspecs.c
+++ b/tests/network/refspecs.c
@@ -153,7 +153,7 @@ static void assert_invalid_rtransform(const char *refspec, const char *name)
 	git_refspec spec;
 	git_buf buf = GIT_BUF_INIT;
 
-	git_refspec__parse(&spec, refspec, true);
+	cl_git_pass(git_refspec__parse(&spec, refspec, true));
 	cl_git_fail(git_refspec_rtransform(&buf, &spec, name));
 
 	git_buf_dispose(&buf);
diff --git a/tests/notes/notes.c b/tests/notes/notes.c
index f245766..af96867 100644
--- a/tests/notes/notes.c
+++ b/tests/notes/notes.c
@@ -400,15 +400,11 @@ void test_notes_notes__can_read_a_note_from_a_commit(void)
 	git_note *note;
 
 	cl_git_pass(git_oid_fromstr(&oid, "4a202b346bb0fb0db7eff3cffeb3c70babbd2045"));
-
 	cl_git_pass(git_note_commit_create(&notes_commit_oid, NULL, _repo, NULL, _sig, _sig, &oid, "I decorate 4a20\n", 1));
-
-	git_commit_lookup(&notes_commit, _repo, &notes_commit_oid);
-
+	cl_git_pass(git_commit_lookup(&notes_commit, _repo, &notes_commit_oid));
 	cl_assert(notes_commit);
 
 	cl_git_pass(git_note_commit_read(&note, _repo, notes_commit, &oid));
-
 	cl_assert_equal_s(git_note_message(note), "I decorate 4a20\n");
 
 	git_commit_free(notes_commit);
@@ -457,7 +453,7 @@ void test_notes_notes__can_insert_a_note_in_an_existing_fanout(void)
 	git_note *_note;
 
 	cl_git_pass(git_oid_fromstr(&target_oid, "08b041783f40edfe12bb406c9c9a8a040177c125"));
-	
+
 	for (i = 0; i <  MESSAGES_COUNT; i++) {
 		cl_git_pass(git_note_create(&note_oid, _repo, "refs/notes/fanout", _sig, _sig, &target_oid, messages[i], 0));
 		cl_git_pass(git_note_read(&_note, _repo, "refs/notes/fanout", &target_oid));
@@ -511,7 +507,7 @@ void test_notes_notes__can_remove_a_note_from_commit(void)
 
 	cl_git_pass(git_note_commit_create(&notes_commit_oid, NULL, _repo, NULL, _sig, _sig, &oid, "I decorate 4a20\n", 0));
 
-	git_commit_lookup(&existing_notes_commit, _repo, &notes_commit_oid);
+	cl_git_pass(git_commit_lookup(&existing_notes_commit, _repo, &notes_commit_oid));
 
 	cl_assert(existing_notes_commit);
 
@@ -547,7 +543,7 @@ void test_notes_notes__removing_a_note_which_doesnt_exists_returns_ENOTFOUND(voi
 
 	cl_git_pass(git_oid_fromstr(&target_oid, "8496071c1b46c854b31185ea97743be6a8774479"));
 	cl_git_pass(git_note_remove(_repo, "refs/notes/fanout", _sig, _sig, &target_oid));
-	
+
 	error = git_note_remove(_repo, "refs/notes/fanout", _sig, _sig, &target_oid);
 	cl_git_fail(error);
 	cl_assert_equal_i(GIT_ENOTFOUND, error);