git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
* [PATCH] read-cache: use size_t for {base,df}_name_compare()
@ 2023-02-05 10:36 René Scharfe
  2023-02-05 21:09 ` Ævar Arnfjörð Bjarmason
  0 siblings, 1 reply; 2+ messages in thread
From: René Scharfe @ 2023-02-05 10:36 UTC (permalink / raw)
  To: Git List; +Cc: Junio C Hamano

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

^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] read-cache: use size_t for {base,df}_name_compare()
  2023-02-05 10:36 [PATCH] read-cache: use size_t for {base,df}_name_compare() René Scharfe
@ 2023-02-05 21:09 ` Ævar Arnfjörð Bjarmason
  0 siblings, 0 replies; 2+ messages in thread
From: Ævar Arnfjörð Bjarmason @ 2023-02-05 21:09 UTC (permalink / raw)
  To: René Scharfe; +Cc: Git List, Junio C Hamano


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.

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2023-02-05 21:10 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-02-05 10:36 [PATCH] read-cache: use size_t for {base,df}_name_compare() René Scharfe
2023-02-05 21:09 ` Ævar Arnfjörð Bjarmason

Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

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 read-only IMAP folder(s) and NNTP newsgroup(s).