use xstrncmpz()
PATCH 1/1 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 <[email protected]>
--- archive-tar.c | 2 +- builtin/fast-export.c | 3 +-- builtin/merge.c | 3 +-- builtin/reflog.c | 3 +-- contrib/coccinelle/xstrncmpz.cocci | 28 ++++++++++++++++++++++++++++ convert.c | 2 +- merge-ll.c | 2 +- object.c | 3 +-- remote.c | 5 ++--- userdiff.c | 3 +-- 10 files changed, 38 insertions(+), 16 deletions(-) create mode 100644 contrib/coccinelle/xstrncmpz.cocci
archive-tar.c +1 -1
@@ -365,7 +365,7 @@ static struct archiver *find_tar_filter(const char *name, size_t len)int i;for (i =0 ; i < nr_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 NULL ;
builtin/fast-export.c +1 -2
@@ -136,8 +136,7 @@ static int anonymized_entry_cmp(const void *cmp_data UNUSED, a = container_of(eptr, const struct anonymized_entry, hash);if (keydata) {const struct anonymized_entry_key *key = keydata; -int equal = !strncmp (a->orig, key->orig, key->orig_len) && - !a->orig[key->orig_len]; +int equal = !xstrncmpz (a->orig, key->orig, key->orig_len);return !equal; }
builtin/merge.c +1 -2
@@ -192,8 +192,7 @@ static struct strategy *get_strategy(const char *name)int j, found =0 ;struct cmdname *ent = main_cmds.names[i];for (j =0 ; !found && j <ARRAY_SIZE (all_strategy); j++) -if (!strncmp (ent->name, all_strategy[j].name, ent->len) - && !all_strategy[j].name[ent->len]) +if (!xstrncmpz (all_strategy[j].name, ent->name, ent->len)) found =1 ;if (!found)add_cmdname (¬_strategies, ent->name, ent->len);
builtin/reflog.c +1 -2
@@ -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;for (ent = reflog_expire_cfg; ent; ent = ent->next) -if (!strncmp (ent->pattern, pattern, len) && - ent->pattern[len] =='\0' ) +if (!xstrncmpz (ent->pattern, pattern, len))return ent;FLEX_ALLOC_MEM (ent, pattern, pattern, len);
contrib/coccinelle/xstrncmpz.cocci new file +28 -0
@@ -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) +)
convert.c +1 -1
@@ -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)return 0 ;for (drv = user_convert; drv; drv = drv->next) -if (!strncmp (drv->name, name, namelen) && !drv->name[namelen]) +if (!xstrncmpz (drv->name, name, namelen))break ;if (!drv) {CALLOC_ARRAY (drv,1 );
merge-ll.c +1 -1
@@ -292,7 +292,7 @@ static int read_merge_config(const char *var, const char *value,* after seeing merge.<name>.var1. */ 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 ;if (!fn) {CALLOC_ARRAY (fn,1 );
object.c +1 -2
@@ -47,8 +47,7 @@ int type_from_string_gently(const char *str, ssize_t len, int gentle) len =strlen (str);for (i =1 ; i <ARRAY_SIZE (object_type_strings); i++) -if (!strncmp (str, object_type_strings[i], len) && - object_type_strings[i][len] =='\0' ) +if (!xstrncmpz (object_type_strings[i], str, len))return i;if (gentle)
remote.c +2 -3
@@ -105,7 +105,7 @@ static int remotes_hash_cmp(const void *cmp_data UNUSED, b = container_of(entry_or_key, const struct remote, ent);if (key) -return strncmp (a->name, key->str, key->len) || a->name[key->len]; +return !!xstrncmpz (a->name, key->str, key->len);else 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);if (key) -return strncmp (a->name, key->str, key->len) || - a->name[key->len]; +return !!xstrncmpz (a->name, key->str, key->len);else return strcmp (a->name, b->name); }
userdiff.c +1 -2
@@ -323,8 +323,7 @@ static int userdiff_find_by_namelen_cb(struct userdiff_driver *driver, {struct find_by_namelen_data *cb_data = priv; -if (!strncmp (driver->name, cb_data->name, cb_data->len) && - !driver->name[cb_data->len]) { +if (!xstrncmpz (driver->name, cb_data->name, cb_data->len)) { cb_data->driver = driver;return 1 ;/* tell the caller to stop iterating */ }
--
2.43.0