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

[PATCH 1/9] Add has_extension()

From
Rene Scharfe <rene.scharfe@lsrfire.ath.cx>
Date
Aug 10, 2006, 15:02 UTC
Message-ID
<11552221581171-git-send-email-rene.scharfe@lsrfire.ath.cx>
In-Reply-To
<11552221582769-git-send-email-rene.scharfe@lsrfire.ath.cx>

The little helper has_extension() documents through its name what we are trying to do and makes sure we don't forget the underrun check.

Signed-off-by: Rene Scharfe <rene.scharfe@lsrfire.ath.cx>
---
 git-compat-util.h |    6 ++++++
 help.c            |    2 +-
 http-fetch.c      |    2 +-
 index-pack.c      |    2 +-
 local-fetch.c     |    2 +-
 refs.c            |    2 +-
 sha1_file.c       |    2 +-
 verify-pack.c     |    2 +-
 8 files changed, 13 insertions(+), 7 deletions(-)
diff --git a/git-compat-util.h b/git-compat-util.h
index 3bcf5b1..dd92093 100644
--- a/git-compat-util.h
+++ b/git-compat-util.h
@@ -139,6 +139,12 @@ static inline ssize_t xwrite(int fd, con
 	}
 }
 
+static inline int has_extension(const char *filename, int len, const char *ext)
+{
+	int extlen = strlen(ext);
+	return len > extlen && !memcmp(filename + len - extlen, ext, extlen);
+}
+
 /* Sane ctype - no locale, and works with signed chars */
 #undef isspace
 #undef isdigit
diff --git a/help.c b/help.c
index fb731cc..7a7f775 100644
--- a/help.c
+++ b/help.c
@@ -140,7 +140,7 @@ static void list_commands(const char *ex
 			continue;
 
 		entlen = strlen(de->d_name);
-		if (4 < entlen && !strcmp(de->d_name + entlen - 4, ".exe"))
+		if (has_extension(de->d_name, entlen, ".exe"))
 			entlen -= 4;
 
 		if (longest < entlen)
diff --git a/http-fetch.c b/http-fetch.c
index 36af3e5..6ea39f0 100644
--- a/http-fetch.c
+++ b/http-fetch.c
@@ -870,7 +870,7 @@ static void process_ls_pack(struct remot
 
 	if (strlen(ls->dentry_name) == 63 &&
 	    !strncmp(ls->dentry_name, "objects/pack/pack-", 18) &&
-	    !strncmp(ls->dentry_name+58, ".pack", 5)) {
+	    has_extension(ls->dentry_name, 63, ".pack")) {
 		get_sha1_hex(ls->dentry_name + 18, sha1);
 		setup_index(ls->repo, sha1);
 	}
diff --git a/index-pack.c b/index-pack.c
index b39953d..a91e39e 100644
--- a/index-pack.c
+++ b/index-pack.c
@@ -447,7 +447,7 @@ int main(int argc, char **argv)
 		usage(index_pack_usage);
 	if (!index_name) {
 		int len = strlen(pack_name);
-		if (len < 5 || strcmp(pack_name + len - 5, ".pack"))
+		if (!has_extension(pack_name, len, ".pack"))
 			die("packfile name '%s' does not end with '.pack'",
 			    pack_name);
 		index_name_buf = xmalloc(len);
diff --git a/local-fetch.c b/local-fetch.c
index 4bf86fb..59af57c 100644
--- a/local-fetch.c
+++ b/local-fetch.c
@@ -44,7 +44,7 @@ static int setup_indices(void)
 	while ((de = readdir(dir)) != NULL) {
 		int namelen = strlen(de->d_name);
 		if (namelen != 50 || 
-		    strcmp(de->d_name + namelen - 5, ".pack"))
+		    !has_extension(de->d_name, namelen, ".pack"))
 			continue;
 		get_sha1_hex(de->d_name + 5, sha1);
 		setup_index(sha1);
diff --git a/refs.c b/refs.c
index 02850b6..b01835f 100644
--- a/refs.c
+++ b/refs.c
@@ -147,7 +147,7 @@ static int do_for_each_ref(const char *b
 			namelen = strlen(de->d_name);
 			if (namelen > 255)
 				continue;
-			if (namelen>5 && !strcmp(de->d_name+namelen-5,".lock"))
+			if (has_extension(de->d_name, namelen, ".lock"))
 				continue;
 			memcpy(path + baselen, de->d_name, namelen+1);
 			if (stat(git_path("%s", path), &st) < 0)
diff --git a/sha1_file.c b/sha1_file.c
index 3d7a7d4..f0a4a7e 100644
--- a/sha1_file.c
+++ b/sha1_file.c
@@ -608,7 +608,7 @@ static void prepare_packed_git_one(char 
 		int namelen = strlen(de->d_name);
 		struct packed_git *p;
 
-		if (strcmp(de->d_name + namelen - 4, ".idx"))
+		if (!has_extension(de->d_name, namelen, ".idx"))
 			continue;
 
 		/* we have .idx.  Is it a file we can map? */
diff --git a/verify-pack.c b/verify-pack.c
index c99db9d..ef00204 100644
--- a/verify-pack.c
+++ b/verify-pack.c
@@ -10,7 +10,7 @@ static int verify_one_pack(char *arg, in
 		/* Should name foo.idx, but foo.pack may be named;
 		 * convert it to foo.idx
 		 */
-		if (!strcmp(arg + len - 5, ".pack")) {
+		if (has_extension(arg, len, ".pack")) {
 			strcpy(arg + len - 5, ".idx");
 			len--;
 		}
-- 
1.4.2.rc2.g822a
Previous: Rene ScharfeNext: Johannes Schindelin
Message 2 of 18 in “git-verify-pack: clean up and make builtin”
  1. 0/9 git-verify-pack: clean up and make builtinRene Scharfe, Aug 10, 2006
  2. 1/9 Add has_extension()Rene Scharfe, Aug 10, 2006
  3. Johannes SchindelinAug 10, 2006
  4. Rene ScharfeAug 10, 2006
  5. Fredrik KuivinenAug 10, 2006
  6. Rene ScharfeAug 10, 2006
  7. Fredrik KuivinenAug 10, 2006
  8. 2/9 git-verify-pack: show usage when no pack was specifiedRene Scharfe, Aug 10, 2006
  9. 3/9 git-verify-pack: more careful path handlingRene Scharfe, Aug 10, 2006
  10. 4/9 git-verify-pack: insist on .idx extensionRene Scharfe, Aug 10, 2006
  11. 5/9 git-verify-pack: get rid of while loopRene Scharfe, Aug 10, 2006
  12. 6/9 git-verify-pack: free pack after use and a cleanupRene Scharfe, Aug 10, 2006
  13. 7/9 git-verify-pack: buffer overrun paranoiaRene Scharfe, Aug 10, 2006
  14. 8/9 git-verify-pack: no need to count errorsRene Scharfe, Aug 10, 2006
  15. Johannes SchindelinAug 10, 2006
  16. Rene ScharfeAug 10, 2006
  17. 9/9 git-verify-pack: make builtinRene Scharfe, Aug 10, 2006
  18. Johannes SchindelinAug 10, 2006

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.