git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "René Scharfe" <l.s.r@web.de>
To: Git List <git@vger.kernel.org>
Cc: Junio C Hamano <gitster@pobox.com>
Subject: [PATCH] read-cache: use size_t for {base,df}_name_compare()
Date: Sun, 5 Feb 2023 11:36:28 +0100	[thread overview]
Message-ID: <d272fd03-a005-f6ad-3f00-6b1a970a519f@web.de> (raw)

Support names of any length in base_name_compare() and df_name_compare()
by using size_t for their length parameters.  They pass the length on to
memcmp(3), which also takes it as a size_t.

Signed-off-by: René Scharfe <l.s.r@web.de>
---
Formatted with --function-context for easier review.

 cache.h      |  6 ++++--
 read-cache.c | 13 +++++++------
 2 files changed, 11 insertions(+), 8 deletions(-)

diff --git a/cache.h b/cache.h
index 4bf14e0bd9..52d017335e 100644
--- a/cache.h
+++ b/cache.h
@@ -1623,8 +1623,10 @@ int repo_interpret_branch_name(struct repository *r,

 int validate_headref(const char *ref);

-int base_name_compare(const char *name1, int len1, int mode1, const char *name2, int len2, int mode2);
-int df_name_compare(const char *name1, int len1, int mode1, const char *name2, int len2, int mode2);
+int base_name_compare(const char *name1, size_t len1, int mode1,
+		      const char *name2, size_t len2, int mode2);
+int df_name_compare(const char *name1, size_t len1, int mode1,
+		    const char *name2, size_t len2, int mode2);
 int name_compare(const char *name1, size_t len1, const char *name2, size_t len2);
 int cache_name_stage_compare(const char *name1, int len1, int stage1, const char *name2, int len2, int stage2);

diff --git a/read-cache.c b/read-cache.c
index 7bd12afb38..35e5657877 100644
--- a/read-cache.c
+++ b/read-cache.c
@@ -488,56 +488,57 @@ int ie_modified(struct index_state *istate,
 	return 0;
 }

-int base_name_compare(const char *name1, int len1, int mode1,
-		      const char *name2, int len2, int mode2)
+int base_name_compare(const char *name1, size_t len1, int mode1,
+		      const char *name2, size_t len2, int mode2)
 {
 	unsigned char c1, c2;
-	int len = len1 < len2 ? len1 : len2;
+	size_t len = len1 < len2 ? len1 : len2;
 	int cmp;

 	cmp = memcmp(name1, name2, len);
 	if (cmp)
 		return cmp;
 	c1 = name1[len];
 	c2 = name2[len];
 	if (!c1 && S_ISDIR(mode1))
 		c1 = '/';
 	if (!c2 && S_ISDIR(mode2))
 		c2 = '/';
 	return (c1 < c2) ? -1 : (c1 > c2) ? 1 : 0;
 }

 /*
  * df_name_compare() is identical to base_name_compare(), except it
  * compares conflicting directory/file entries as equal. Note that
  * while a directory name compares as equal to a regular file, they
  * then individually compare _differently_ to a filename that has
  * a dot after the basename (because '\0' < '.' < '/').
  *
  * This is used by routines that want to traverse the git namespace
  * but then handle conflicting entries together when possible.
  */
-int df_name_compare(const char *name1, int len1, int mode1,
-		    const char *name2, int len2, int mode2)
+int df_name_compare(const char *name1, size_t len1, int mode1,
+		    const char *name2, size_t len2, int mode2)
 {
-	int len = len1 < len2 ? len1 : len2, cmp;
 	unsigned char c1, c2;
+	size_t len = len1 < len2 ? len1 : len2;
+	int cmp;

 	cmp = memcmp(name1, name2, len);
 	if (cmp)
 		return cmp;
 	/* Directories and files compare equal (same length, same name) */
 	if (len1 == len2)
 		return 0;
 	c1 = name1[len];
 	if (!c1 && S_ISDIR(mode1))
 		c1 = '/';
 	c2 = name2[len];
 	if (!c2 && S_ISDIR(mode2))
 		c2 = '/';
 	if (c1 == '/' && !c2)
 		return 0;
 	if (c2 == '/' && !c1)
 		return 0;
 	return c1 - c2;
 }
--
2.39.1

             reply	other threads:[~2023-02-05 10:36 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-02-05 10:36 René Scharfe [this message]
2023-02-05 21:09 ` [PATCH] read-cache: use size_t for {base,df}_name_compare() Ævar Arnfjörð Bjarmason

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=d272fd03-a005-f6ad-3f00-6b1a970a519f@web.de \
    --to=l.s.r@web.de \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).