cocci: refactor common patterns to use xstrdup_or_null()
d64ea0f83b ("git-compat-util: add xstrdup_or_null helper", 2015-01-12) added a handy wrapper that allows us to get a duplicate of a string or NULL if the original is NULL, but a handful of codepath predate its introduction or just weren't aware of it. Signed-off-by: Junio C Hamano <gitster@pobox.com>
Junio C Hamano committed
Oct 12, 2016 at 11:20 UTC
13092a916d7b8211fd828a6a7ee0d3cefff995e1
7 files changed
+17
-20
contrib/coccinelle/xstrdup_or_null.cocci
new
+7
@@ -0,0 +1,7 @@
1
+@@
2
+expression E;
3
+expression V;
4
+@@
5
+- if (E)
6
+- V = xstrdup(E);
7
++ V = xstrdup_or_null(E);
git.c
+1
-2
@@ -35,8 +35,7 @@ static void save_env_before_alias(void)
35
orig_cwd = xgetcwd();
36
for (i = 0; i < ARRAY_SIZE(env_names); i++) {
37
orig_env[i] = getenv(env_names[i]);
38
- if (orig_env[i])
39
- orig_env[i] = xstrdup(orig_env[i]);
38
+ orig_env[i] = xstrdup_or_null(orig_env[i]);
39
}
40
}
41
imap-send.c
+2
-4
@@ -1082,10 +1082,8 @@ static struct imap_store *imap_open_store(struct imap_server_conf *srvc, char *f
1082
cred.protocol = xstrdup(srvc->use_ssl ? "imaps" : "imap");
1083
cred.host = xstrdup(srvc->host);
1084
1085
- if (srvc->user)
1086
- cred.username = xstrdup(srvc->user);
1087
- if (srvc->pass)
1088
- cred.password = xstrdup(srvc->pass);
1085
+ cred.username = xstrdup_or_null(srvc->user);
1086
+ cred.password = xstrdup_or_null(srvc->pass);
1087
1088
credential_fill(&cred);
1089
mailmap.c
+2
-4
@@ -103,10 +103,8 @@ static void add_mapping(struct string_list *map,
103
} else {
104
struct mailmap_info *mi = xcalloc(1, sizeof(struct mailmap_info));
105
debug_mm("mailmap: adding (complex) entry for '%s'\n", old_email);
106
- if (new_name)
107
- mi->name = xstrdup(new_name);
108
- if (new_email)
109
- mi->email = xstrdup(new_email);
106
+ mi->name = xstrdup_or_null(new_name);
107
+ mi->email = xstrdup_or_null(new_email);
108
string_list_insert(&me->namemap, old_name)->util = mi;
109
}
110
refs.c
+1
-2
@@ -791,8 +791,7 @@ struct ref_update *ref_transaction_add_update(
791
hashcpy(update->new_sha1, new_sha1);
792
if (flags & REF_HAVE_OLD)
793
hashcpy(update->old_sha1, old_sha1);
794
- if (msg)
795
- update->msg = xstrdup(msg);
794
+ update->msg = xstrdup_or_null(msg);
795
return update;
796
}
797
send-pack.c
+1
-2
@@ -181,8 +181,7 @@ static int receive_status(int in, struct ref *refs)
181
hint->status = REF_STATUS_REMOTE_REJECT;
182
ret = -1;
183
}
184
- if (msg)
185
- hint->remote_status = xstrdup(msg);
184
+ hint->remote_status = xstrdup_or_null(msg);
185
/* start our next search from the next ref */
186
hint = hint->next;
187
}
trailer.c
+3
-6
@@ -428,12 +428,9 @@ static int set_if_missing(struct conf_info *item, const char *value)
428
static void duplicate_conf(struct conf_info *dst, struct conf_info *src)
429
{
430
*dst = *src;
431
- if (src->name)
432
- dst->name = xstrdup(src->name);
433
- if (src->key)
434
- dst->key = xstrdup(src->key);
435
- if (src->command)
436
- dst->command = xstrdup(src->command);
431
+ dst->name = xstrdup_or_null(src->name);
432
+ dst->key = xstrdup_or_null(src->key);
433
+ dst->command = xstrdup_or_null(src->command);
434
}
435
436
static struct trailer_item *get_conf_item(const char *name)