Skip to content

Commit 13092a9

Browse files
committed
cocci: refactor common patterns to use xstrdup_or_null()
d64ea0f ("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 <[email protected]>
1 parent 74eeaf7 commit 13092a9

File tree

7 files changed

+17
-20
lines changed

7 files changed

+17
-20
lines changed
+7
Original file line numberDiff line numberDiff line change
@@ -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
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,7 @@ static void save_env_before_alias(void)
3535
orig_cwd = xgetcwd();
3636
for (i = 0; i < ARRAY_SIZE(env_names); i++) {
3737
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]);
4039
}
4140
}
4241

imap-send.c

+2-4
Original file line numberDiff line numberDiff line change
@@ -1082,10 +1082,8 @@ static struct imap_store *imap_open_store(struct imap_server_conf *srvc, char *f
10821082
cred.protocol = xstrdup(srvc->use_ssl ? "imaps" : "imap");
10831083
cred.host = xstrdup(srvc->host);
10841084

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);
10891087

10901088
credential_fill(&cred);
10911089

mailmap.c

+2-4
Original file line numberDiff line numberDiff line change
@@ -103,10 +103,8 @@ static void add_mapping(struct string_list *map,
103103
} else {
104104
struct mailmap_info *mi = xcalloc(1, sizeof(struct mailmap_info));
105105
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);
110108
string_list_insert(&me->namemap, old_name)->util = mi;
111109
}
112110

refs.c

+1-2
Original file line numberDiff line numberDiff line change
@@ -791,8 +791,7 @@ struct ref_update *ref_transaction_add_update(
791791
hashcpy(update->new_sha1, new_sha1);
792792
if (flags & REF_HAVE_OLD)
793793
hashcpy(update->old_sha1, old_sha1);
794-
if (msg)
795-
update->msg = xstrdup(msg);
794+
update->msg = xstrdup_or_null(msg);
796795
return update;
797796
}
798797

send-pack.c

+1-2
Original file line numberDiff line numberDiff line change
@@ -181,8 +181,7 @@ static int receive_status(int in, struct ref *refs)
181181
hint->status = REF_STATUS_REMOTE_REJECT;
182182
ret = -1;
183183
}
184-
if (msg)
185-
hint->remote_status = xstrdup(msg);
184+
hint->remote_status = xstrdup_or_null(msg);
186185
/* start our next search from the next ref */
187186
hint = hint->next;
188187
}

trailer.c

+3-6
Original file line numberDiff line numberDiff line change
@@ -428,12 +428,9 @@ static int set_if_missing(struct conf_info *item, const char *value)
428428
static void duplicate_conf(struct conf_info *dst, struct conf_info *src)
429429
{
430430
*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);
437434
}
438435

439436
static struct trailer_item *get_conf_item(const char *name)

0 commit comments

Comments
 (0)