Commit 9094d30b932ca4b47dba81e76011efe05455a44a

Sascha Cunz 2012-11-23T11:41:56

Reset all static variables to NULL in clar's __cleanup Without this change, any failed assertion in the second (or a later) test inside a test suite has a chance of double deleting memory, resulting in a heap corruption. See #1096 for details. This leaves alone the test cases where we "just" use cl_git_sandbox_init() and cl_git_sandbox_cleanup(). These methods already take good care to not double delete a repository. Fixes #1096

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
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
diff --git a/tests-clar/checkout/tree.c b/tests-clar/checkout/tree.c
index 9834253..534c460 100644
--- a/tests-clar/checkout/tree.c
+++ b/tests-clar/checkout/tree.c
@@ -18,6 +18,7 @@ void test_checkout_tree__initialize(void)
 void test_checkout_tree__cleanup(void)
 {
 	git_object_free(g_object);
+	g_object = NULL;
 
 	cl_git_sandbox_cleanup();
 }
diff --git a/tests-clar/clone/network.c b/tests-clar/clone/network.c
index 1304f77..def5214 100644
--- a/tests-clar/clone/network.c
+++ b/tests-clar/clone/network.c
@@ -17,8 +17,10 @@ void test_clone_network__initialize(void)
 
 static void cleanup_repository(void *path)
 {
-	if (g_repo)
+	if (g_repo) {
 		git_repository_free(g_repo);
+		g_repo = NULL;
+	}
 	cl_fixture_cleanup((const char *)path);
 }
 
diff --git a/tests-clar/clone/nonetwork.c b/tests-clar/clone/nonetwork.c
index 59f4336..fbebe54 100644
--- a/tests-clar/clone/nonetwork.c
+++ b/tests-clar/clone/nonetwork.c
@@ -14,8 +14,11 @@ void test_clone_nonetwork__initialize(void)
 
 static void cleanup_repository(void *path)
 {
-	if (g_repo)
+	if (g_repo) {
 		git_repository_free(g_repo);
+		g_repo = NULL;
+	}
+
 	cl_fixture_cleanup((const char *)path);
 }
 
diff --git a/tests-clar/commit/commit.c b/tests-clar/commit/commit.c
index 1205e52..4cedcea 100644
--- a/tests-clar/commit/commit.c
+++ b/tests-clar/commit/commit.c
@@ -11,6 +11,8 @@ void test_commit_commit__initialize(void)
 void test_commit_commit__cleanup(void)
 {
 	git_repository_free(_repo);
+	_repo = NULL;
+
 	cl_fixture_cleanup("testrepo.git");
 }
 
diff --git a/tests-clar/commit/parent.c b/tests-clar/commit/parent.c
index a007577..18ce0bb 100644
--- a/tests-clar/commit/parent.c
+++ b/tests-clar/commit/parent.c
@@ -16,7 +16,10 @@ void test_commit_parent__initialize(void)
 void test_commit_parent__cleanup(void)
 {
 	git_commit_free(commit);
+	commit = NULL;
+
 	git_repository_free(_repo);
+	_repo = NULL;
 }
 
 static void assert_nth_gen_parent(unsigned int gen, const char *expected_oid)
diff --git a/tests-clar/commit/write.c b/tests-clar/commit/write.c
index 9c4d077..6d62809 100644
--- a/tests-clar/commit/write.c
+++ b/tests-clar/commit/write.c
@@ -17,16 +17,22 @@ void test_commit_write__initialize(void)
 {
    g_repo = cl_git_sandbox_init("testrepo");
 }
+
 void test_commit_write__cleanup(void)
 {
-   git_reference_free(head);
-   git_reference_free(branch);
+	git_reference_free(head);
+	head = NULL;
+
+	git_reference_free(branch);
+	branch = NULL;
 
-   git_commit_free(commit);
+	git_commit_free(commit);
+	commit = NULL;
 
-   git__free(head_old);
+	git__free(head_old);
+	head_old = NULL;
 
-   cl_git_sandbox_cleanup();
+	cl_git_sandbox_cleanup();
 }
 
 
diff --git a/tests-clar/core/env.c b/tests-clar/core/env.c
index d849f76..9eb2fe5 100644
--- a/tests-clar/core/env.c
+++ b/tests-clar/core/env.c
@@ -29,6 +29,7 @@ void test_core_env__cleanup(void)
 #ifdef GIT_WIN32
 		git__free(env_save[i]);
 #endif
+		env_save[i] = NULL;
 	}
 }
 
diff --git a/tests-clar/diff/blob.c b/tests-clar/diff/blob.c
index 0a37e82..a9ebcc2 100644
--- a/tests-clar/diff/blob.c
+++ b/tests-clar/diff/blob.c
@@ -30,7 +30,10 @@ void test_diff_blob__initialize(void)
 void test_diff_blob__cleanup(void)
 {
 	git_blob_free(d);
+	d = NULL;
+
 	git_blob_free(alien);
+	alien = NULL;
 
 	cl_git_sandbox_cleanup();
 }
diff --git a/tests-clar/fetchhead/network.c b/tests-clar/fetchhead/network.c
index fa3b92c..0710480 100644
--- a/tests-clar/fetchhead/network.c
+++ b/tests-clar/fetchhead/network.c
@@ -18,8 +18,11 @@ void test_fetchhead_network__initialize(void)
 
 static void cleanup_repository(void *path)
 {
-	if (g_repo)
+	if (g_repo) {
 		git_repository_free(g_repo);
+		g_repo = NULL;
+	}
+
 	cl_fixture_cleanup((const char *)path);
 }
 
diff --git a/tests-clar/fetchhead/nonetwork.c b/tests-clar/fetchhead/nonetwork.c
index 1de5280..c86ae74 100644
--- a/tests-clar/fetchhead/nonetwork.c
+++ b/tests-clar/fetchhead/nonetwork.c
@@ -15,8 +15,11 @@ void test_fetchhead_nonetwork__initialize(void)
 
 static void cleanup_repository(void *path)
 {
-	if (g_repo)
+	if (g_repo) {
 		git_repository_free(g_repo);
+		g_repo = NULL;
+	}
+
 	cl_fixture_cleanup((const char *)path);
 }
 
diff --git a/tests-clar/index/conflicts.c b/tests-clar/index/conflicts.c
index e101b16..91ff926 100644
--- a/tests-clar/index/conflicts.c
+++ b/tests-clar/index/conflicts.c
@@ -30,6 +30,8 @@ void test_index_conflicts__initialize(void)
 void test_index_conflicts__cleanup(void)
 {
 	git_index_free(repo_index);
+	repo_index = NULL;
+
 	cl_git_sandbox_cleanup();
 }
 
diff --git a/tests-clar/index/reuc.c b/tests-clar/index/reuc.c
index c7c3944..3a139aa 100644
--- a/tests-clar/index/reuc.c
+++ b/tests-clar/index/reuc.c
@@ -26,6 +26,8 @@ void test_index_reuc__initialize(void)
 void test_index_reuc__cleanup(void)
 {
 	git_index_free(repo_index);
+	repo_index = NULL;
+
 	cl_git_sandbox_cleanup();
 }
 
diff --git a/tests-clar/index/stage.c b/tests-clar/index/stage.c
index ba6f1b8..9c9d296 100644
--- a/tests-clar/index/stage.c
+++ b/tests-clar/index/stage.c
@@ -18,6 +18,8 @@ void test_index_stage__initialize(void)
 void test_index_stage__cleanup(void)
 {
 	git_index_free(repo_index);
+	repo_index = NULL;
+
 	cl_git_sandbox_cleanup();
 }
 
diff --git a/tests-clar/network/createremotethenload.c b/tests-clar/network/createremotethenload.c
index 45931d3..b64c2cc 100644
--- a/tests-clar/network/createremotethenload.c
+++ b/tests-clar/network/createremotethenload.c
@@ -22,7 +22,11 @@ void test_network_createremotethenload__initialize(void)
 void test_network_createremotethenload__cleanup(void)
 {
 	git_remote_free(_remote);
+	_remote = NULL;
+
 	git_repository_free(_repo);
+	_repo = NULL;
+
 	cl_fixture_cleanup("testrepo.git");
 }
 
diff --git a/tests-clar/network/fetch.c b/tests-clar/network/fetch.c
index d2140b5..9c37d72 100644
--- a/tests-clar/network/fetch.c
+++ b/tests-clar/network/fetch.c
@@ -13,6 +13,8 @@ void test_network_fetch__initialize(void)
 void test_network_fetch__cleanup(void)
 {
 	git_repository_free(_repo);
+	_repo = NULL;
+
 	cl_fixture_cleanup("./fetch");
 }
 
diff --git a/tests-clar/network/remotelocal.c b/tests-clar/network/remotelocal.c
index f7ae834..f7dcfc0 100644
--- a/tests-clar/network/remotelocal.c
+++ b/tests-clar/network/remotelocal.c
@@ -15,9 +15,14 @@ void test_network_remotelocal__initialize(void)
 
 void test_network_remotelocal__cleanup(void)
 {
-	git_remote_free(remote);
 	git_buf_free(&file_path_buf);
+
+	git_remote_free(remote);
+	remote = NULL;
+
 	git_repository_free(repo);
+	repo = NULL;
+
 	cl_fixture_cleanup("remotelocal");
 }
 
diff --git a/tests-clar/network/remoterename.c b/tests-clar/network/remoterename.c
index 70041f4..e9a7fc0 100644
--- a/tests-clar/network/remoterename.c
+++ b/tests-clar/network/remoterename.c
@@ -16,6 +16,7 @@ void test_network_remoterename__initialize(void)
 void test_network_remoterename__cleanup(void)
 {
 	git_remote_free(_remote);
+	_remote = NULL;
 
 	cl_git_sandbox_cleanup();
 }
diff --git a/tests-clar/network/remotes.c b/tests-clar/network/remotes.c
index 1d58aba..4fe3ebe 100644
--- a/tests-clar/network/remotes.c
+++ b/tests-clar/network/remotes.c
@@ -20,6 +20,8 @@ void test_network_remotes__initialize(void)
 void test_network_remotes__cleanup(void)
 {
 	git_remote_free(_remote);
+	_remote = NULL;
+
 	cl_git_sandbox_cleanup();
 }
 
diff --git a/tests-clar/notes/notes.c b/tests-clar/notes/notes.c
index dfd7f52..706bc03 100644
--- a/tests-clar/notes/notes.c
+++ b/tests-clar/notes/notes.c
@@ -12,6 +12,8 @@ void test_notes_notes__initialize(void)
 void test_notes_notes__cleanup(void)
 {
 	git_signature_free(_sig);
+	_sig = NULL;
+
 	cl_git_sandbox_cleanup();
 }
 
diff --git a/tests-clar/notes/notesref.c b/tests-clar/notes/notesref.c
index 79ad0af..67c4003 100644
--- a/tests-clar/notes/notesref.c
+++ b/tests-clar/notes/notesref.c
@@ -16,10 +16,17 @@ void test_notes_notesref__initialize(void)
 void test_notes_notesref__cleanup(void)
 {
 	git_note_free(_note);
+	_note = NULL;
+
 	git_signature_free(_sig);
+	_sig = NULL;
+
 	git_config_free(_cfg);
+	_cfg = NULL;
 
 	git_repository_free(_repo);
+	_repo = NULL;
+
 	cl_fixture_cleanup("testrepo.git");
 }
 
diff --git a/tests-clar/object/commit/commitstagedfile.c b/tests-clar/object/commit/commitstagedfile.c
index eb78ced..6dc536e 100644
--- a/tests-clar/object/commit/commitstagedfile.c
+++ b/tests-clar/object/commit/commitstagedfile.c
@@ -13,6 +13,8 @@ void test_object_commit_commitstagedfile__initialize(void)
 void test_object_commit_commitstagedfile__cleanup(void)
 {
 	git_repository_free(repo);
+	repo = NULL;
+
 	cl_fixture_cleanup("treebuilder");
 }
 
diff --git a/tests-clar/object/lookup.c b/tests-clar/object/lookup.c
index 7cbcc61..01435bc 100644
--- a/tests-clar/object/lookup.c
+++ b/tests-clar/object/lookup.c
@@ -11,7 +11,8 @@ void test_object_lookup__initialize(void)
 
 void test_object_lookup__cleanup(void)
 {
-   git_repository_free(g_repo);
+	git_repository_free(g_repo);
+	g_repo = NULL;
 }
 
 void test_object_lookup__lookup_wrong_type_returns_enotfound(void)
diff --git a/tests-clar/object/peel.c b/tests-clar/object/peel.c
index f748be7..a197728 100644
--- a/tests-clar/object/peel.c
+++ b/tests-clar/object/peel.c
@@ -10,6 +10,7 @@ void test_object_peel__initialize(void)
 void test_object_peel__cleanup(void)
 {
 	git_repository_free(g_repo);
+	g_repo = NULL;
 }
 
 static void assert_peel(
diff --git a/tests-clar/object/tag/peel.c b/tests-clar/object/tag/peel.c
index 97c5a7d..e2cd8d6 100644
--- a/tests-clar/object/tag/peel.c
+++ b/tests-clar/object/tag/peel.c
@@ -14,8 +14,13 @@ void test_object_tag_peel__initialize(void)
 void test_object_tag_peel__cleanup(void)
 {
 	git_tag_free(tag);
+	tag = NULL;
+
 	git_object_free(target);
+	target = NULL;
+
 	git_repository_free(repo);
+	repo = NULL;
 
 	cl_fixture_cleanup("testrepo.git");
 }
diff --git a/tests-clar/object/tree/frompath.c b/tests-clar/object/tree/frompath.c
index fd42551..86ca47e 100644
--- a/tests-clar/object/tree/frompath.c
+++ b/tests-clar/object/tree/frompath.c
@@ -19,7 +19,10 @@ void test_object_tree_frompath__initialize(void)
 void test_object_tree_frompath__cleanup(void)
 {
 	git_tree_free(tree);
+	tree = NULL;
+
 	git_repository_free(repo);
+	repo = NULL;
 }
 
 static void assert_tree_from_path(
diff --git a/tests-clar/odb/mixed.c b/tests-clar/odb/mixed.c
index 0bd23e1..da0ed97 100644
--- a/tests-clar/odb/mixed.c
+++ b/tests-clar/odb/mixed.c
@@ -11,6 +11,7 @@ void test_odb_mixed__initialize(void)
 void test_odb_mixed__cleanup(void)
 {
 	git_odb_free(_odb);
+	_odb = NULL;
 }
 
 void test_odb_mixed__dup_oid(void) {
diff --git a/tests-clar/odb/packed.c b/tests-clar/odb/packed.c
index 4bce41b..90e9f3a 100644
--- a/tests-clar/odb/packed.c
+++ b/tests-clar/odb/packed.c
@@ -12,6 +12,7 @@ void test_odb_packed__initialize(void)
 void test_odb_packed__cleanup(void)
 {
 	git_odb_free(_odb);
+	_odb = NULL;
 }
 
 void test_odb_packed__mass_read(void)
diff --git a/tests-clar/odb/packed_one.c b/tests-clar/odb/packed_one.c
index a064829..e9d246c 100644
--- a/tests-clar/odb/packed_one.c
+++ b/tests-clar/odb/packed_one.c
@@ -17,6 +17,7 @@ void test_odb_packed_one__initialize(void)
 void test_odb_packed_one__cleanup(void)
 {
 	git_odb_free(_odb);
+	_odb = NULL;
 }
 
 void test_odb_packed_one__mass_read(void)
diff --git a/tests-clar/pack/packbuilder.c b/tests-clar/pack/packbuilder.c
index 208141c..1ec768d 100644
--- a/tests-clar/pack/packbuilder.c
+++ b/tests-clar/pack/packbuilder.c
@@ -7,6 +7,7 @@ static git_revwalk *_revwalker;
 static git_packbuilder *_packbuilder;
 static git_indexer *_indexer;
 static git_vector _commits;
+static int _commits_is_initialized;
 
 void test_pack_packbuilder__initialize(void)
 {
@@ -14,6 +15,7 @@ void test_pack_packbuilder__initialize(void)
 	cl_git_pass(git_revwalk_new(&_revwalker, _repo));
 	cl_git_pass(git_packbuilder_new(&_packbuilder, _repo));
 	cl_git_pass(git_vector_init(&_commits, 0, NULL));
+	_commits_is_initialized = 1;
 }
 
 void test_pack_packbuilder__cleanup(void)
@@ -21,15 +23,25 @@ void test_pack_packbuilder__cleanup(void)
 	git_oid *o;
 	unsigned int i;
 
-	git_vector_foreach(&_commits, i, o) {
-		git__free(o);
+	if (_commits_is_initialized) {
+		_commits_is_initialized = 0;
+		git_vector_foreach(&_commits, i, o) {
+			git__free(o);
+		}
+		git_vector_free(&_commits);
 	}
-	git_vector_free(&_commits);
+
 	git_packbuilder_free(_packbuilder);
+	_packbuilder = NULL;
+
 	git_revwalk_free(_revwalker);
+	_revwalker = NULL;
+
 	git_indexer_free(_indexer);
 	_indexer = NULL;
+
 	git_repository_free(_repo);
+	_repo = NULL;
 }
 
 static void seed_packbuilder(void)
diff --git a/tests-clar/refs/branches/create.c b/tests-clar/refs/branches/create.c
index 9026b0d..5ecb428 100644
--- a/tests-clar/refs/branches/create.c
+++ b/tests-clar/refs/branches/create.c
@@ -16,9 +16,13 @@ void test_refs_branches_create__initialize(void)
 void test_refs_branches_create__cleanup(void)
 {
 	git_reference_free(branch);
+	branch = NULL;
 
 	git_object_free(target);
+	target = NULL;
+
 	git_repository_free(repo);
+	repo = NULL;
 
 	cl_fixture_cleanup("testrepo.git");
 }
diff --git a/tests-clar/refs/branches/delete.c b/tests-clar/refs/branches/delete.c
index da7db13..75271a2 100644
--- a/tests-clar/refs/branches/delete.c
+++ b/tests-clar/refs/branches/delete.c
@@ -20,7 +20,10 @@ void test_refs_branches_delete__initialize(void)
 void test_refs_branches_delete__cleanup(void)
 {
 	git_reference_free(fake_remote);
+	fake_remote = NULL;
+
 	git_repository_free(repo);
+	repo = NULL;
 
 	cl_fixture_cleanup("testrepo.git");
 }
diff --git a/tests-clar/refs/branches/foreach.c b/tests-clar/refs/branches/foreach.c
index 92d5b1f..dfa0439 100644
--- a/tests-clar/refs/branches/foreach.c
+++ b/tests-clar/refs/branches/foreach.c
@@ -18,7 +18,10 @@ void test_refs_branches_foreach__initialize(void)
 void test_refs_branches_foreach__cleanup(void)
 {
 	git_reference_free(fake_remote);
+	fake_remote = NULL;
+
 	git_repository_free(repo);
+	repo = NULL;
 
 	cl_fixture_cleanup("testrepo.git");
 }
diff --git a/tests-clar/refs/branches/ishead.c b/tests-clar/refs/branches/ishead.c
index 52a0a19..2ab488f 100644
--- a/tests-clar/refs/branches/ishead.c
+++ b/tests-clar/refs/branches/ishead.c
@@ -13,7 +13,10 @@ void test_refs_branches_ishead__initialize(void)
 void test_refs_branches_ishead__cleanup(void)
 {
 	git_reference_free(branch);
+	branch = NULL;
+
 	git_repository_free(repo);
+	repo = NULL;
 }
 
 void test_refs_branches_ishead__can_tell_if_a_branch_is_pointed_at_by_HEAD(void)
diff --git a/tests-clar/refs/branches/lookup.c b/tests-clar/refs/branches/lookup.c
index 2aabf98..d07ed0e 100644
--- a/tests-clar/refs/branches/lookup.c
+++ b/tests-clar/refs/branches/lookup.c
@@ -14,8 +14,10 @@ void test_refs_branches_lookup__initialize(void)
 void test_refs_branches_lookup__cleanup(void)
 {
 	git_reference_free(branch);
+	branch = NULL;
 
 	git_repository_free(repo);
+	repo = NULL;
 }
 
 void test_refs_branches_lookup__can_retrieve_a_local_branch(void)
diff --git a/tests-clar/refs/branches/move.c b/tests-clar/refs/branches/move.c
index 0424690..4bf1d69 100644
--- a/tests-clar/refs/branches/move.c
+++ b/tests-clar/refs/branches/move.c
@@ -15,6 +15,8 @@ void test_refs_branches_move__initialize(void)
 void test_refs_branches_move__cleanup(void)
 {
 	git_reference_free(ref);
+	ref = NULL;
+
 	cl_git_sandbox_cleanup();
 }
 
diff --git a/tests-clar/refs/branches/tracking.c b/tests-clar/refs/branches/tracking.c
index 9cf435e..9378eca 100644
--- a/tests-clar/refs/branches/tracking.c
+++ b/tests-clar/refs/branches/tracking.c
@@ -14,8 +14,10 @@ void test_refs_branches_tracking__initialize(void)
 void test_refs_branches_tracking__cleanup(void)
 {
 	git_reference_free(branch);
+	branch = NULL;
 
 	git_repository_free(repo);
+	repo = NULL;
 }
 
 void test_refs_branches_tracking__can_retrieve_the_remote_tracking_reference_of_a_local_branch(void)
diff --git a/tests-clar/refs/foreachglob.c b/tests-clar/refs/foreachglob.c
index 1213429..8ecce9c 100644
--- a/tests-clar/refs/foreachglob.c
+++ b/tests-clar/refs/foreachglob.c
@@ -18,7 +18,10 @@ void test_refs_foreachglob__initialize(void)
 void test_refs_foreachglob__cleanup(void)
 {
 	git_reference_free(fake_remote);
+	fake_remote = NULL;
+
 	git_repository_free(repo);
+	repo = NULL;
 
 	cl_fixture_cleanup("testrepo.git");
 }
diff --git a/tests-clar/refs/peel.c b/tests-clar/refs/peel.c
index 35a290b..6fa6009 100644
--- a/tests-clar/refs/peel.c
+++ b/tests-clar/refs/peel.c
@@ -10,6 +10,7 @@ void test_refs_peel__initialize(void)
 void test_refs_peel__cleanup(void)
 {
 	git_repository_free(g_repo);
+	g_repo = NULL;
 }
 
 static void assert_peel(
diff --git a/tests-clar/refs/read.c b/tests-clar/refs/read.c
index c2647e2..b867c97 100644
--- a/tests-clar/refs/read.c
+++ b/tests-clar/refs/read.c
@@ -22,6 +22,7 @@ void test_refs_read__initialize(void)
 void test_refs_read__cleanup(void)
 {
 	git_repository_free(g_repo);
+	g_repo = NULL;
 }
 
 void test_refs_read__loose_tag(void)
diff --git a/tests-clar/refs/reflog/drop.c b/tests-clar/refs/reflog/drop.c
index 512e8ba..d805d1e 100644
--- a/tests-clar/refs/reflog/drop.c
+++ b/tests-clar/refs/reflog/drop.c
@@ -22,6 +22,7 @@ void test_refs_reflog_drop__initialize(void)
 void test_refs_reflog_drop__cleanup(void)
 {
 	git_reflog_free(g_reflog);
+	g_reflog = NULL;
 
 	cl_git_sandbox_cleanup();
 }
diff --git a/tests-clar/refs/unicode.c b/tests-clar/refs/unicode.c
index 889c856..fbe95b1 100644
--- a/tests-clar/refs/unicode.c
+++ b/tests-clar/refs/unicode.c
@@ -12,6 +12,8 @@ void test_refs_unicode__initialize(void)
 void test_refs_unicode__cleanup(void)
 {
 	git_repository_free(repo);
+	repo = NULL;
+
 	cl_fixture_cleanup("testrepo.git");
 }
 
diff --git a/tests-clar/repo/head.c b/tests-clar/repo/head.c
index 551e834..23d14d6 100644
--- a/tests-clar/repo/head.c
+++ b/tests-clar/repo/head.c
@@ -3,7 +3,7 @@
 #include "repo_helpers.h"
 #include "posix.h"
 
-git_repository *repo;
+static git_repository *repo;
 
 void test_repo_head__initialize(void)
 {
diff --git a/tests-clar/repo/init.c b/tests-clar/repo/init.c
index f76e8bc..f29f540 100644
--- a/tests-clar/repo/init.c
+++ b/tests-clar/repo/init.c
@@ -19,6 +19,8 @@ void test_repo_init__initialize(void)
 static void cleanup_repository(void *path)
 {
 	git_repository_free(_repo);
+	_repo = NULL;
+
 	cl_fixture_cleanup((const char *)path);
 }
 
diff --git a/tests-clar/repo/setters.c b/tests-clar/repo/setters.c
index cd6e389..7e482de 100644
--- a/tests-clar/repo/setters.c
+++ b/tests-clar/repo/setters.c
@@ -17,6 +17,8 @@ void test_repo_setters__initialize(void)
 void test_repo_setters__cleanup(void)
 {
 	git_repository_free(repo);
+	repo = NULL;
+
 	cl_fixture_cleanup("testrepo.git");
 	cl_fixture_cleanup("new_workdir");
 }
diff --git a/tests-clar/reset/hard.c b/tests-clar/reset/hard.c
index bddbd17..9381007 100644
--- a/tests-clar/reset/hard.c
+++ b/tests-clar/reset/hard.c
@@ -16,6 +16,8 @@ void test_reset_hard__initialize(void)
 void test_reset_hard__cleanup(void)
 {
 	git_object_free(target);
+	target = NULL;
+
 	cl_git_sandbox_cleanup();
 }
 
diff --git a/tests-clar/reset/mixed.c b/tests-clar/reset/mixed.c
index d5f8e10..7b90c23 100644
--- a/tests-clar/reset/mixed.c
+++ b/tests-clar/reset/mixed.c
@@ -15,6 +15,8 @@ void test_reset_mixed__initialize(void)
 void test_reset_mixed__cleanup(void)
 {
 	git_object_free(target);
+	target = NULL;
+
 	cl_git_sandbox_cleanup();
 }
 
diff --git a/tests-clar/reset/soft.c b/tests-clar/reset/soft.c
index d51e3f1..7914d46 100644
--- a/tests-clar/reset/soft.c
+++ b/tests-clar/reset/soft.c
@@ -15,6 +15,8 @@ void test_reset_soft__initialize(void)
 void test_reset_soft__cleanup(void)
 {
 	git_object_free(target);
+	target = NULL;
+
 	cl_git_sandbox_cleanup();
 }
 
diff --git a/tests-clar/revwalk/basic.c b/tests-clar/revwalk/basic.c
index 126ca7d..438ec01 100644
--- a/tests-clar/revwalk/basic.c
+++ b/tests-clar/revwalk/basic.c
@@ -103,7 +103,9 @@ void test_revwalk_basic__initialize(void)
 void test_revwalk_basic__cleanup(void)
 {
 	git_revwalk_free(_walk);
+	_walk = NULL;
 	git_repository_free(_repo);
+	_repo = NULL;
 }
 
 void test_revwalk_basic__sorting_modes(void)
diff --git a/tests-clar/revwalk/mergebase.c b/tests-clar/revwalk/mergebase.c
index 8434901..268574e 100644
--- a/tests-clar/revwalk/mergebase.c
+++ b/tests-clar/revwalk/mergebase.c
@@ -12,6 +12,7 @@ void test_revwalk_mergebase__initialize(void)
 void test_revwalk_mergebase__cleanup(void)
 {
 	git_repository_free(_repo);
+	_repo = NULL;
 }
 
 void test_revwalk_mergebase__single1(void)
diff --git a/tests-clar/revwalk/signatureparsing.c b/tests-clar/revwalk/signatureparsing.c
index 94de1a3..cf1d31e 100644
--- a/tests-clar/revwalk/signatureparsing.c
+++ b/tests-clar/revwalk/signatureparsing.c
@@ -12,7 +12,10 @@ void test_revwalk_signatureparsing__initialize(void)
 void test_revwalk_signatureparsing__cleanup(void)
 {
 	git_revwalk_free(_walk);
+	_walk = NULL;
+
 	git_repository_free(_repo);
+	_repo = NULL;
 }
 
 void test_revwalk_signatureparsing__do_not_choke_when_name_contains_angle_brackets(void)
diff --git a/tests-clar/stash/drop.c b/tests-clar/stash/drop.c
index b4f73b9..9d1aeda 100644
--- a/tests-clar/stash/drop.c
+++ b/tests-clar/stash/drop.c
@@ -14,7 +14,11 @@ void test_stash_drop__initialize(void)
 void test_stash_drop__cleanup(void)
 {
 	git_signature_free(signature);
+	signature = NULL;
+
 	git_repository_free(repo);
+	repo = NULL;
+
 	cl_git_pass(git_futils_rmdir_r("stash", NULL, GIT_RMDIR_REMOVE_FILES));
 }
 
diff --git a/tests-clar/stash/foreach.c b/tests-clar/stash/foreach.c
index d7127a9..c7d59a3 100644
--- a/tests-clar/stash/foreach.c
+++ b/tests-clar/stash/foreach.c
@@ -29,7 +29,11 @@ void test_stash_foreach__initialize(void)
 void test_stash_foreach__cleanup(void)
 {
 	git_signature_free(signature);
+	signature = NULL;
+
 	git_repository_free(repo);
+	repo = NULL;
+
 	cl_git_pass(git_futils_rmdir_r(REPO_NAME, NULL, GIT_RMDIR_REMOVE_FILES));
 }
 
diff --git a/tests-clar/stash/save.c b/tests-clar/stash/save.c
index fadb894..4eaf2a3 100644
--- a/tests-clar/stash/save.c
+++ b/tests-clar/stash/save.c
@@ -26,7 +26,11 @@ void test_stash_save__initialize(void)
 void test_stash_save__cleanup(void)
 {
 	git_signature_free(signature);
+	signature = NULL;
+
 	git_repository_free(repo);
+	repo = NULL;
+
 	cl_git_pass(git_futils_rmdir_r("stash", NULL, GIT_RMDIR_REMOVE_FILES));
 }