git/list[1] front-page[2] threads[3] people[4] search[5] about
 

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

From
René Scharfe <l.s.r@web.de>
Date
Feb 5, 2023, 10:36 UTC
Message-ID
<d272fd03-a005-f6ad-3f00-6b1a970a519f@web.de>

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
Next: Ævar Arnfjörð Bjarmason
Message 1 of 2 in “read-cache: use size_t for {base,df}_name_compare()”
  1. read-cache: use size_t for {base,df}_name_compare()René Scharfe, Feb 5, 2023
  2. Ævar Arnfjörð BjarmasonFeb 5, 2023

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.