Skip to content

Commit 50f03c6

Browse files
committed
Merge branch 'ab/free-and-null'
A common pattern to free a piece of memory and assign NULL to the pointer that used to point at it has been replaced with a new FREE_AND_NULL() macro. * ab/free-and-null: *.[ch] refactoring: make use of the FREE_AND_NULL() macro coccinelle: make use of the "expression" FREE_AND_NULL() rule coccinelle: add a rule to make "expression" code use FREE_AND_NULL() coccinelle: make use of the "type" FREE_AND_NULL() rule coccinelle: add a rule to make "type" code use FREE_AND_NULL() git-compat-util: add a FREE_AND_NULL() wrapper around free(ptr); ptr = NULL
2 parents cda4ba3 + 88ce3ef commit 50f03c6

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+117
-195
lines changed

alias.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,7 @@ int split_cmdline(char *cmdline, const char ***argv)
6262
src++;
6363
c = cmdline[src];
6464
if (!c) {
65-
free(*argv);
66-
*argv = NULL;
65+
FREE_AND_NULL(*argv);
6766
return -SPLIT_CMDLINE_BAD_ENDING;
6867
}
6968
}
@@ -75,8 +74,7 @@ int split_cmdline(char *cmdline, const char ***argv)
7574
cmdline[dst] = 0;
7675

7776
if (quoted) {
78-
free(*argv);
79-
*argv = NULL;
77+
FREE_AND_NULL(*argv);
8078
return -SPLIT_CMDLINE_UNCLOSED_QUOTE;
8179
}
8280

apply.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3695,8 +3695,7 @@ static int check_preimage(struct apply_state *state,
36953695
is_new:
36963696
patch->is_new = 1;
36973697
patch->is_delete = 0;
3698-
free(patch->old_name);
3699-
patch->old_name = NULL;
3698+
FREE_AND_NULL(patch->old_name);
37003699
return 0;
37013700
}
37023701

attr.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -639,13 +639,11 @@ void attr_check_reset(struct attr_check *check)
639639

640640
void attr_check_clear(struct attr_check *check)
641641
{
642-
free(check->items);
643-
check->items = NULL;
642+
FREE_AND_NULL(check->items);
644643
check->alloc = 0;
645644
check->nr = 0;
646645

647-
free(check->all_attrs);
648-
check->all_attrs = NULL;
646+
FREE_AND_NULL(check->all_attrs);
649647
check->all_attrs_nr = 0;
650648

651649
drop_attr_stack(&check->stack);

blame.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -314,8 +314,7 @@ static void fill_origin_blob(struct diff_options *opt,
314314
static void drop_origin_blob(struct blame_origin *o)
315315
{
316316
if (o->file.ptr) {
317-
free(o->file.ptr);
318-
o->file.ptr = NULL;
317+
FREE_AND_NULL(o->file.ptr);
319318
}
320319
}
321320

branch.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@ static int find_tracked_branch(struct remote *remote, void *priv)
2525
} else {
2626
free(tracking->spec.src);
2727
if (tracking->src) {
28-
free(tracking->src);
29-
tracking->src = NULL;
28+
FREE_AND_NULL(tracking->src);
3029
}
3130
}
3231
tracking->spec.src = NULL;

builtin/am.c

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -484,8 +484,7 @@ static int run_applypatch_msg_hook(struct am_state *state)
484484
ret = run_hook_le(NULL, "applypatch-msg", am_path(state, "final-commit"), NULL);
485485

486486
if (!ret) {
487-
free(state->msg);
488-
state->msg = NULL;
487+
FREE_AND_NULL(state->msg);
489488
if (read_commit_msg(state) < 0)
490489
die(_("'%s' was deleted by the applypatch-msg hook"),
491490
am_path(state, "final-commit"));
@@ -1074,17 +1073,10 @@ static void am_next(struct am_state *state)
10741073
{
10751074
struct object_id head;
10761075

1077-
free(state->author_name);
1078-
state->author_name = NULL;
1079-
1080-
free(state->author_email);
1081-
state->author_email = NULL;
1082-
1083-
free(state->author_date);
1084-
state->author_date = NULL;
1085-
1086-
free(state->msg);
1087-
state->msg = NULL;
1076+
FREE_AND_NULL(state->author_name);
1077+
FREE_AND_NULL(state->author_email);
1078+
FREE_AND_NULL(state->author_date);
1079+
FREE_AND_NULL(state->msg);
10881080
state->msg_len = 0;
10891081

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

builtin/clean.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -838,8 +838,7 @@ static void interactive_main_loop(void)
838838
int ret;
839839
ret = menus[*chosen].fn();
840840
if (ret != MENU_RETURN_NO_LOOP) {
841-
free(chosen);
842-
chosen = NULL;
841+
FREE_AND_NULL(chosen);
843842
if (!del_list.nr) {
844843
clean_print_color(CLEAN_COLOR_ERROR);
845844
printf_ln(_("No more files to clean, exiting."));
@@ -852,8 +851,7 @@ static void interactive_main_loop(void)
852851
quit_cmd();
853852
}
854853

855-
free(chosen);
856-
chosen = NULL;
854+
FREE_AND_NULL(chosen);
857855
break;
858856
}
859857
}

builtin/config.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -215,8 +215,7 @@ static int get_value(const char *key_, const char *regex_)
215215
key_regexp = (regex_t*)xmalloc(sizeof(regex_t));
216216
if (regcomp(key_regexp, key, REG_EXTENDED)) {
217217
error("invalid key pattern: %s", key_);
218-
free(key_regexp);
219-
key_regexp = NULL;
218+
FREE_AND_NULL(key_regexp);
220219
ret = CONFIG_INVALID_PATTERN;
221220
goto free_strings;
222221
}
@@ -236,8 +235,7 @@ static int get_value(const char *key_, const char *regex_)
236235
regexp = (regex_t*)xmalloc(sizeof(regex_t));
237236
if (regcomp(regexp, regex_, REG_EXTENDED)) {
238237
error("invalid pattern: %s", regex_);
239-
free(regexp);
240-
regexp = NULL;
238+
FREE_AND_NULL(regexp);
241239
ret = CONFIG_INVALID_PATTERN;
242240
goto free_strings;
243241
}

builtin/index-pack.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -389,8 +389,7 @@ static struct base_data *alloc_base_data(void)
389389
static void free_base_data(struct base_data *c)
390390
{
391391
if (c->data) {
392-
free(c->data);
393-
c->data = NULL;
392+
FREE_AND_NULL(c->data);
394393
get_thread_data()->base_cache_used -= c->size;
395394
}
396395
}
@@ -606,8 +605,7 @@ static void *unpack_data(struct object_entry *obj,
606605
git_inflate_end(&stream);
607606
free(inbuf);
608607
if (consume) {
609-
free(data);
610-
data = NULL;
608+
FREE_AND_NULL(data);
611609
}
612610
return data;
613611
}

builtin/pack-objects.c

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -265,8 +265,7 @@ static unsigned long write_no_reuse_object(struct sha1file *f, struct object_ent
265265
* make sure no cached delta data remains from a
266266
* previous attempt before a pack split occurred.
267267
*/
268-
free(entry->delta_data);
269-
entry->delta_data = NULL;
268+
FREE_AND_NULL(entry->delta_data);
270269
entry->z_delta_size = 0;
271270
} else if (entry->delta_data) {
272271
size = entry->delta_size;
@@ -1376,12 +1375,10 @@ static void cleanup_preferred_base(void)
13761375
if (!pbase_tree_cache[i])
13771376
continue;
13781377
free(pbase_tree_cache[i]->tree_data);
1379-
free(pbase_tree_cache[i]);
1380-
pbase_tree_cache[i] = NULL;
1378+
FREE_AND_NULL(pbase_tree_cache[i]);
13811379
}
13821380

1383-
free(done_pbase_paths);
1384-
done_pbase_paths = NULL;
1381+
FREE_AND_NULL(done_pbase_paths);
13851382
done_pbase_paths_num = done_pbase_paths_alloc = 0;
13861383
}
13871384

@@ -1971,8 +1968,7 @@ static unsigned long free_unpacked(struct unpacked *n)
19711968
n->index = NULL;
19721969
if (n->data) {
19731970
freed_mem += n->entry->size;
1974-
free(n->data);
1975-
n->data = NULL;
1971+
FREE_AND_NULL(n->data);
19761972
}
19771973
n->entry = NULL;
19781974
n->depth = 0;

0 commit comments

Comments
 (0)