Skip to content

Commit 88ce3ef

Browse files
avargitster
authored andcommitted
*.[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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent e140f7a commit 88ce3ef

File tree

12 files changed

+23
-49
lines changed

12 files changed

+23
-49
lines changed

builtin/am.c

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1072,17 +1072,10 @@ static void am_next(struct am_state *state)
10721072
{
10731073
struct object_id head;
10741074

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);
10861079
state->msg_len = 0;
10871080

10881081
unlink(am_path(state, "author-script"));

builtin/worktree.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -299,10 +299,8 @@ static int add_worktree(const char *path, const char *refname,
299299
}
300300

301301
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);
306304

307305
done:
308306
if (ret || !opts->keep_locked) {

commit-slab.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,7 @@ static MAYBE_UNUSED void clear_ ##slabname(struct slabname *s) \
8282
for (i = 0; i < s->slab_count; i++) \
8383
free(s->slab[i]); \
8484
s->slab_count = 0; \
85-
free(s->slab); \
86-
s->slab = NULL; \
85+
FREE_AND_NULL(s->slab); \
8786
} \
8887
\
8988
static MAYBE_UNUSED elemtype *slabname## _at_peek(struct slabname *s, \

credential.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -313,10 +313,8 @@ void credential_reject(struct credential *c)
313313
for (i = 0; i < c->helpers.nr; i++)
314314
credential_do(c, c->helpers.items[i].string, "erase");
315315

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);
320318
c->approved = 0;
321319
}
322320

gpg-interface.c

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,11 @@ static const char *gpg_program = "gpg";
1313

1414
void signature_check_clear(struct signature_check *sigc)
1515
{
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);
2621
}
2722

2823
static struct {

grep.c

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1763,12 +1763,9 @@ void grep_source_init(struct grep_source *gs, enum grep_source_type type,
17631763

17641764
void grep_source_clear(struct grep_source *gs)
17651765
{
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);
17721769
grep_source_clear_data(gs);
17731770
}
17741771

help.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -267,9 +267,8 @@ static void add_cmd_list(struct cmdnames *cmds, struct cmdnames *old)
267267

268268
for (i = 0; i < old->cnt; i++)
269269
cmds->names[cmds->cnt++] = old->names[i];
270-
free(old->names);
270+
FREE_AND_NULL(old->names);
271271
old->cnt = 0;
272-
old->names = NULL;
273272
}
274273

275274
/* An empirically derived magic number */

line-log.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,8 @@ void range_set_init(struct range_set *rs, size_t prealloc)
3434

3535
void range_set_release(struct range_set *rs)
3636
{
37-
free(rs->ranges);
37+
FREE_AND_NULL(rs->ranges);
3838
rs->alloc = rs->nr = 0;
39-
rs->ranges = NULL;
4039
}
4140

4241
/* dst must be uninitialized! */

prio-queue.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,9 @@ void prio_queue_reverse(struct prio_queue *queue)
2727

2828
void clear_prio_queue(struct prio_queue *queue)
2929
{
30-
free(queue->array);
30+
FREE_AND_NULL(queue->array);
3131
queue->nr = 0;
3232
queue->alloc = 0;
33-
queue->array = NULL;
3433
queue->insertion_ctr = 0;
3534
}
3635

refs/ref-cache.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,8 @@ static void clear_ref_dir(struct ref_dir *dir)
8282
int i;
8383
for (i = 0; i < dir->nr; i++)
8484
free_ref_entry(dir->entries[i]);
85-
free(dir->entries);
85+
FREE_AND_NULL(dir->entries);
8686
dir->sorted = dir->nr = dir->alloc = 0;
87-
dir->entries = NULL;
8887
}
8988

9089
struct ref_entry *create_dir_entry(struct ref_cache *cache,

0 commit comments

Comments
 (0)