*.[ch] refactoring: make use of the FREE_AND_NULL() macro

Replace occurrences of `free(ptr); ptr = NULL` which weren't caught by the coccinelle rule. These fall into two categories: - free/NULL assignments one after the other which coccinelle all put on one line, which is functionally equivalent code, but very ugly. - manually spotted occurrences where the NULL assignment isn't right after the free() call. Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Ævar Arnfjörð Bjarmason committed Jun 15, 2017 at 23:15 UTC 88ce3ef636b1385e861ec0e9e2155248b999b032
12 files changed +23 -49
builtin/am.c
+4 -11
@@ -1072,17 +1072,10 @@ static void am_next(struct am_state *state)
1072 {
1073 struct object_id head;
1074
1075 - free(state->author_name);
1076 - state->author_name = NULL;
1077 -
1078 - free(state->author_email);
1079 - state->author_email = NULL;
1080 -
1081 - free(state->author_date);
1082 - state->author_date = NULL;
1083 -
1084 - free(state->msg);
1085 - state->msg = NULL;
1075 + FREE_AND_NULL(state->author_name);
1076 + FREE_AND_NULL(state->author_email);
1077 + FREE_AND_NULL(state->author_date);
1078 + FREE_AND_NULL(state->msg);
1079 state->msg_len = 0;
1080
1081 unlink(am_path(state, "author-script"));
builtin/worktree.c
+2 -4
@@ -299,10 +299,8 @@ static int add_worktree(const char *path, const char *refname,
299 }
300
301 is_junk = 0;
302 - free(junk_work_tree);
303 - free(junk_git_dir);
304 - junk_work_tree = NULL;
305 - junk_git_dir = NULL;
302 + FREE_AND_NULL(junk_work_tree);
303 + FREE_AND_NULL(junk_git_dir);
304
305 done:
306 if (ret || !opts->keep_locked) {
commit-slab.h
+1 -2
@@ -82,8 +82,7 @@ static MAYBE_UNUSED void clear_ ##slabname(struct slabname *s) \
82 for (i = 0; i < s->slab_count; i++) \
83 free(s->slab[i]); \
84 s->slab_count = 0; \
85 - free(s->slab); \
86 - s->slab = NULL; \
85 + FREE_AND_NULL(s->slab); \
86 } \
87 \
88 static MAYBE_UNUSED elemtype *slabname## _at_peek(struct slabname *s, \
credential.c
+2 -4
@@ -313,10 +313,8 @@ void credential_reject(struct credential *c)
313 for (i = 0; i < c->helpers.nr; i++)
314 credential_do(c, c->helpers.items[i].string, "erase");
315
316 - free(c->username);
317 - c->username = NULL;
318 - free(c->password);
319 - c->password = NULL;
316 + FREE_AND_NULL(c->username);
317 + FREE_AND_NULL(c->password);
318 c->approved = 0;
319 }
320
gpg-interface.c
+5 -10
@@ -13,16 +13,11 @@ static const char *gpg_program = "gpg";
13
14 void signature_check_clear(struct signature_check *sigc)
15 {
16 - free(sigc->payload);
17 - free(sigc->gpg_output);
18 - free(sigc->gpg_status);
19 - free(sigc->signer);
20 - free(sigc->key);
21 - sigc->payload = NULL;
22 - sigc->gpg_output = NULL;
23 - sigc->gpg_status = NULL;
24 - sigc->signer = NULL;
25 - sigc->key = NULL;
16 + FREE_AND_NULL(sigc->payload);
17 + FREE_AND_NULL(sigc->gpg_output);
18 + FREE_AND_NULL(sigc->gpg_status);
19 + FREE_AND_NULL(sigc->signer);
20 + FREE_AND_NULL(sigc->key);
21 }
22
23 static struct {
grep.c
+3 -6
@@ -1763,12 +1763,9 @@ void grep_source_init(struct grep_source *gs, enum grep_source_type type,
1763
1764 void grep_source_clear(struct grep_source *gs)
1765 {
1766 - free(gs->name);
1767 - gs->name = NULL;
1768 - free(gs->path);
1769 - gs->path = NULL;
1770 - free(gs->identifier);
1771 - gs->identifier = NULL;
1766 + FREE_AND_NULL(gs->name);
1767 + FREE_AND_NULL(gs->path);
1768 + FREE_AND_NULL(gs->identifier);
1769 grep_source_clear_data(gs);
1770 }
1771
help.c
+1 -2
@@ -267,9 +267,8 @@ static void add_cmd_list(struct cmdnames *cmds, struct cmdnames *old)
267
268 for (i = 0; i < old->cnt; i++)
269 cmds->names[cmds->cnt++] = old->names[i];
270 - free(old->names);
270 + FREE_AND_NULL(old->names);
271 old->cnt = 0;
272 - old->names = NULL;
272 }
273
274 /* An empirically derived magic number */
line-log.c
+1 -2
@@ -34,9 +34,8 @@ void range_set_init(struct range_set *rs, size_t prealloc)
34
35 void range_set_release(struct range_set *rs)
36 {
37 - free(rs->ranges);
37 + FREE_AND_NULL(rs->ranges);
38 rs->alloc = rs->nr = 0;
39 - rs->ranges = NULL;
39 }
40
41 /* dst must be uninitialized! */
prio-queue.c
+1 -2
@@ -27,10 +27,9 @@ void prio_queue_reverse(struct prio_queue *queue)
27
28 void clear_prio_queue(struct prio_queue *queue)
29 {
30 - free(queue->array);
30 + FREE_AND_NULL(queue->array);
31 queue->nr = 0;
32 queue->alloc = 0;
33 - queue->array = NULL;
33 queue->insertion_ctr = 0;
34 }
35
refs/ref-cache.c
+1 -2
@@ -82,9 +82,8 @@ static void clear_ref_dir(struct ref_dir *dir)
82 int i;
83 for (i = 0; i < dir->nr; i++)
84 free_ref_entry(dir->entries[i]);
85 - free(dir->entries);
85 + FREE_AND_NULL(dir->entries);
86 dir->sorted = dir->nr = dir->alloc = 0;
87 - dir->entries = NULL;
87 }
88
89 struct ref_entry *create_dir_entry(struct ref_cache *cache,
rerere.c
+1 -2
@@ -39,9 +39,8 @@ static void free_rerere_dirs(void)
39 free(rerere_dir[i]->status);
40 free(rerere_dir[i]);
41 }
42 - free(rerere_dir);
42 + FREE_AND_NULL(rerere_dir);
43 rerere_dir_nr = rerere_dir_alloc = 0;
44 - rerere_dir = NULL;
44 }
45
46 static void free_rerere_id(struct string_list_item *item)
split-index.c
+1 -2
@@ -174,10 +174,9 @@ void merge_base_index(struct index_state *istate)
174
175 ewah_free(si->delete_bitmap);
176 ewah_free(si->replace_bitmap);
177 - free(si->saved_cache);
177 + FREE_AND_NULL(si->saved_cache);
178 si->delete_bitmap = NULL;
179 si->replace_bitmap = NULL;
180 - si->saved_cache = NULL;
180 si->saved_cache_nr = 0;
181 }
182