diff mbox series

read-cache: use size_t for {base,df}_name_compare()

Message ID d272fd03-a005-f6ad-3f00-6b1a970a519f@web.de (mailing list archive)
State New, archived
Headers show
Series read-cache: use size_t for {base,df}_name_compare() | expand

Commit Message

René Scharfe Feb. 5, 2023, 10:36 a.m. UTC
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(-)

--
2.39.1

Comments

Ævar Arnfjörð Bjarmason Feb. 5, 2023, 9:09 p.m. UTC | #1
On Sun, Feb 05 2023, René Scharfe wrote:

> 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.

This looks good, and it's nice to see changes that slowly address the
greater topic of our outstanding -Werror=sign-compare issues.
diff mbox series

Patch

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;
 }