From patchwork Sat Feb 10 07:43:01 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Ren=C3=A9_Scharfe?= X-Patchwork-Id: 13552198 Received: from mout.web.de (mout.web.de [217.72.192.78]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 25EA02B9B7 for ; Sat, 10 Feb 2024 07:43:03 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.72.192.78 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707550987; cv=none; b=pWiwVAFfUSkFoPwZh1VEFwBF3KBZ1fP9mk9xWd80Z0rTrBDPRySMM7OY0K5rMsRDk+MXmJj3uNMv9J3pWJsg+jaQrJFcBgmpx+aSke4I9krVI/90OzcBFzCZwuYtyphDt9eKhwrLFzvIl+2u3cRqyQr45Yl0fR2qP/XgTKsFUi4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707550987; c=relaxed/simple; bh=zeBK73pT4F5np74w9oY/Wja1/rgFYyfwxVZqc0Iofks=; h=Message-ID:Date:MIME-Version:To:From:Subject:Content-Type; b=c8lMF8Hq7Po+pBpcWYIxDEIqgISH41XuNpwI0aST4GXDiFLSx7JlqXetKQUOFLvu0Hbs3Qred28LJpfQnGkjZFBHg6xYSzTsBwmr/zedRQ/qHYIaNP4+c8CgtmQDzsqITIcGSupfmBbSqli33nLUb2UgAFpwOC9UwxhHFwBSEiI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=web.de; spf=pass smtp.mailfrom=web.de; dkim=pass (2048-bit key) header.d=web.de header.i=l.s.r@web.de header.b=v3U9CGEg; arc=none smtp.client-ip=217.72.192.78 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=web.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=web.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=web.de header.i=l.s.r@web.de header.b="v3U9CGEg" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=web.de; s=s29768273; t=1707550981; x=1708155781; i=l.s.r@web.de; bh=zeBK73pT4F5np74w9oY/Wja1/rgFYyfwxVZqc0Iofks=; h=X-UI-Sender-Class:Date:To:From:Subject; b=v3U9CGEgtpVEme7wJe3aOs6fxlqsEp9PNyOZn6qHun8Wcsso/LuEzKC8sXtsQmiR IuosYvuX9tT8GZmg9bS8+uWgMIAPHpPGnXo7crBG8qfDcrH7/sw6xzUYRJHJZKDlP hEX7vOVzeCzFlOwbcfl0PIgC3z2tdlmTzvHI7XOH+9c351FChuEYyVzfaZggvr3Lz OyvkWzCQKCEwLPQRz5OZ/2sHobJHfLfBL6t4L2954DjYYhp3oEFouN7s2K+IczCag ZstYLOoeZOg9bT7yL5lm6C3RmPjpLjFM++5dNQY6qn2TCzLVYWqUFuImiJQJAP6bo UwK4B2Ok+UqOeLLFuQ== X-UI-Sender-Class: 814a7b36-bfc1-4dae-8640-3722d8ec6cd6 Received: from [192.168.178.29] ([79.203.17.81]) by smtp.web.de (mrweb105 [213.165.67.124]) with ESMTPSA (Nemesis) id 1MiMAO-1r2QGh2wa6-00fVbu for ; Sat, 10 Feb 2024 08:43:01 +0100 Message-ID: <954b75d0-1504-4f57-b34e-e770a4b7b3ea@web.de> Date: Sat, 10 Feb 2024 08:43:01 +0100 Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Content-Language: en-US To: Git List From: =?utf-8?q?Ren=C3=A9_Scharfe?= Subject: [PATCH] use xstrncmpz() X-Provags-ID: V03:K1:ZsWMDrnkXSy2igW+8WzqrM8l4yzYEPl8rOnBuw8zdQob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qiPyWIqvd7eOEVMq44= 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 --- 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 -- 2.43.0 diff --git a/archive-tar.c b/archive-tar.c index f2a0ed7752..8ae30125f8 100644 --- a/archive-tar.c +++ b/archive-tar.c @@ -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; diff --git a/builtin/fast-export.c b/builtin/fast-export.c index f18f0809f9..4693d18cc9 100644 --- a/builtin/fast-export.c +++ b/builtin/fast-export.c @@ -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; } diff --git a/builtin/merge.c b/builtin/merge.c index 8f819781cc..935c8a57dd 100644 --- a/builtin/merge.c +++ b/builtin/merge.c @@ -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); diff --git a/builtin/reflog.c b/builtin/reflog.c index a5a4099f61..2c3369fca5 100644 --- a/builtin/reflog.c +++ b/builtin/reflog.c @@ -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); diff --git a/contrib/coccinelle/xstrncmpz.cocci b/contrib/coccinelle/xstrncmpz.cocci new file mode 100644 index 0000000000..ccb39e2bc0 --- /dev/null +++ b/contrib/coccinelle/xstrncmpz.cocci @@ -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) +) diff --git a/convert.c b/convert.c index a8870baff3..35b25eb3cb 100644 --- a/convert.c +++ b/convert.c @@ -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); diff --git a/merge-ll.c b/merge-ll.c index 5ffb045efb..61e0ae5398 100644 --- a/merge-ll.c +++ b/merge-ll.c @@ -292,7 +292,7 @@ static int read_merge_config(const char *var, const char *value, * after seeing merge..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); diff --git a/object.c b/object.c index 2c61e4c862..e6a1c4d905 100644 --- a/object.c +++ b/object.c @@ -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) diff --git a/remote.c b/remote.c index e07b316eac..9090632e96 100644 --- a/remote.c +++ b/remote.c @@ -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); } diff --git a/userdiff.c b/userdiff.c index e399543823..2b1dab2649 100644 --- a/userdiff.c +++ b/userdiff.c @@ -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 */ }