use xstrncmpz()

Add and apply a semantic patch for calling xstrncmpz() to compare a
NUL-terminated string with a buffer of a known length instead of using
strncmp() and checking the terminating NUL explicitly.  This simplifies
callers by reducing code duplication.

I had to adjust remote.c manually because Coccinelle inexplicably
changed the indent of the else branches.

Signed-off-by: René Scharfe <l.s.r@web.de>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
René Scharfe 2024-02-10 08:43:01 +01:00 committed by Junio C Hamano
parent 3526e67d91
commit f0e578c69c
10 changed files with 38 additions and 16 deletions

View file

@ -365,7 +365,7 @@ static struct archiver *find_tar_filter(const char *name, size_t len)
int i; int i;
for (i = 0; i < nr_tar_filters; i++) { for (i = 0; i < nr_tar_filters; i++) {
struct archiver *ar = tar_filters[i]; struct archiver *ar = tar_filters[i];
if (!strncmp(ar->name, name, len) && !ar->name[len]) if (!xstrncmpz(ar->name, name, len))
return ar; return ar;
} }
return NULL; return NULL;

View file

@ -136,8 +136,7 @@ static int anonymized_entry_cmp(const void *cmp_data UNUSED,
a = container_of(eptr, const struct anonymized_entry, hash); a = container_of(eptr, const struct anonymized_entry, hash);
if (keydata) { if (keydata) {
const struct anonymized_entry_key *key = keydata; const struct anonymized_entry_key *key = keydata;
int equal = !strncmp(a->orig, key->orig, key->orig_len) && int equal = !xstrncmpz(a->orig, key->orig, key->orig_len);
!a->orig[key->orig_len];
return !equal; return !equal;
} }

View file

@ -192,8 +192,7 @@ static struct strategy *get_strategy(const char *name)
int j, found = 0; int j, found = 0;
struct cmdname *ent = main_cmds.names[i]; struct cmdname *ent = main_cmds.names[i];
for (j = 0; !found && j < ARRAY_SIZE(all_strategy); j++) for (j = 0; !found && j < ARRAY_SIZE(all_strategy); j++)
if (!strncmp(ent->name, all_strategy[j].name, ent->len) if (!xstrncmpz(all_strategy[j].name, ent->name, ent->len))
&& !all_strategy[j].name[ent->len])
found = 1; found = 1;
if (!found) if (!found)
add_cmdname(&not_strategies, ent->name, ent->len); add_cmdname(&not_strategies, ent->name, ent->len);

View file

@ -96,8 +96,7 @@ static struct reflog_expire_cfg *find_cfg_ent(const char *pattern, size_t len)
reflog_expire_cfg_tail = &reflog_expire_cfg; reflog_expire_cfg_tail = &reflog_expire_cfg;
for (ent = reflog_expire_cfg; ent; ent = ent->next) for (ent = reflog_expire_cfg; ent; ent = ent->next)
if (!strncmp(ent->pattern, pattern, len) && if (!xstrncmpz(ent->pattern, pattern, len))
ent->pattern[len] == '\0')
return ent; return ent;
FLEX_ALLOC_MEM(ent, pattern, pattern, len); FLEX_ALLOC_MEM(ent, pattern, pattern, len);

View file

@ -0,0 +1,28 @@
@@
expression S, T, L;
@@
(
- strncmp(S, T, L) || S[L]
+ !!xstrncmpz(S, T, L)
|
- strncmp(S, T, L) || S[L] != '\0'
+ !!xstrncmpz(S, T, L)
|
- strncmp(S, T, L) || T[L]
+ !!xstrncmpz(T, S, L)
|
- strncmp(S, T, L) || T[L] != '\0'
+ !!xstrncmpz(T, S, L)
|
- !strncmp(S, T, L) && !S[L]
+ !xstrncmpz(S, T, L)
|
- !strncmp(S, T, L) && S[L] == '\0'
+ !xstrncmpz(S, T, L)
|
- !strncmp(S, T, L) && !T[L]
+ !xstrncmpz(T, S, L)
|
- !strncmp(S, T, L) && T[L] == '\0'
+ !xstrncmpz(T, S, L)
)

View file

@ -1028,7 +1028,7 @@ static int read_convert_config(const char *var, const char *value,
if (parse_config_key(var, "filter", &name, &namelen, &key) < 0 || !name) if (parse_config_key(var, "filter", &name, &namelen, &key) < 0 || !name)
return 0; return 0;
for (drv = user_convert; drv; drv = drv->next) for (drv = user_convert; drv; drv = drv->next)
if (!strncmp(drv->name, name, namelen) && !drv->name[namelen]) if (!xstrncmpz(drv->name, name, namelen))
break; break;
if (!drv) { if (!drv) {
CALLOC_ARRAY(drv, 1); CALLOC_ARRAY(drv, 1);

View file

@ -286,7 +286,7 @@ static int read_merge_config(const char *var, const char *value,
* after seeing merge.<name>.var1. * after seeing merge.<name>.var1.
*/ */
for (fn = ll_user_merge; fn; fn = fn->next) for (fn = ll_user_merge; fn; fn = fn->next)
if (!strncmp(fn->name, name, namelen) && !fn->name[namelen]) if (!xstrncmpz(fn->name, name, namelen))
break; break;
if (!fn) { if (!fn) {
CALLOC_ARRAY(fn, 1); CALLOC_ARRAY(fn, 1);

View file

@ -47,8 +47,7 @@ int type_from_string_gently(const char *str, ssize_t len, int gentle)
len = strlen(str); len = strlen(str);
for (i = 1; i < ARRAY_SIZE(object_type_strings); i++) for (i = 1; i < ARRAY_SIZE(object_type_strings); i++)
if (!strncmp(str, object_type_strings[i], len) && if (!xstrncmpz(object_type_strings[i], str, len))
object_type_strings[i][len] == '\0')
return i; return i;
if (gentle) if (gentle)

View file

@ -105,7 +105,7 @@ static int remotes_hash_cmp(const void *cmp_data UNUSED,
b = container_of(entry_or_key, const struct remote, ent); b = container_of(entry_or_key, const struct remote, ent);
if (key) if (key)
return strncmp(a->name, key->str, key->len) || a->name[key->len]; return !!xstrncmpz(a->name, key->str, key->len);
else else
return strcmp(a->name, b->name); return strcmp(a->name, b->name);
} }
@ -189,8 +189,7 @@ static int branches_hash_cmp(const void *cmp_data UNUSED,
b = container_of(entry_or_key, const struct branch, ent); b = container_of(entry_or_key, const struct branch, ent);
if (key) if (key)
return strncmp(a->name, key->str, key->len) || return !!xstrncmpz(a->name, key->str, key->len);
a->name[key->len];
else else
return strcmp(a->name, b->name); return strcmp(a->name, b->name);
} }

View file

@ -323,8 +323,7 @@ static int userdiff_find_by_namelen_cb(struct userdiff_driver *driver,
{ {
struct find_by_namelen_data *cb_data = priv; struct find_by_namelen_data *cb_data = priv;
if (!strncmp(driver->name, cb_data->name, cb_data->len) && if (!xstrncmpz(driver->name, cb_data->name, cb_data->len)) {
!driver->name[cb_data->len]) {
cb_data->driver = driver; cb_data->driver = driver;
return 1; /* tell the caller to stop iterating */ return 1; /* tell the caller to stop iterating */
} }