Anonymous avatar Anonymous committed 5bb1cda

drop length argument of has_extension

As Fredrik points out the current interface of has_extension() is
potentially confusing. Its parameters include both a nul-terminated
string and a length-limited string.

This patch drops the length argument, requiring two nul-terminated
strings; all callsites are updated. I checked that all of them indeed
provide nul-terminated strings. Filenames need to be nul-terminated
anyway if they are to be passed to open() etc. The performance penalty
of the additional strlen() is negligible compared to the system calls
which inevitably surround has_extension() calls.

Additionally, change has_extension() to use size_t inside instead of
int, as that is the exact type strlen() returns and memcmp() expects.

Signed-off-by: Rene Scharfe <>;
Signed-off-by: Junio C Hamano <>;

Comments (0)

Files changed (8)

 		entlen = strlen(de->d_name);
-		if (has_extension(de->d_name, entlen, ".exe"))
+		if (has_extension(de->d_name, ".exe"))
 			entlen -= 4;
 		if (longest < entlen)


-static inline int has_extension(const char *filename, int len, const char *ext)
+static inline int has_extension(const char *filename, const char *ext)
-	int extlen = strlen(ext);
+	size_t len = strlen(filename);
+	size_t extlen = strlen(ext);
 	return len > extlen && !memcmp(filename + len - extlen, ext, extlen);
 	if (strlen(ls->dentry_name) == 63 &&
 	    !strncmp(ls->dentry_name, "objects/pack/pack-", 18) &&
-	    has_extension(ls->dentry_name, 63, ".pack")) {
+	    has_extension(ls->dentry_name, ".pack")) {
 		get_sha1_hex(ls->dentry_name + 18, sha1);
 		setup_index(ls->repo, sha1);
 	if (!index_name) {
 		int len = strlen(pack_name);
-		if (!has_extension(pack_name, len, ".pack"))
+		if (!has_extension(pack_name, ".pack"))
 			die("packfile name '%s' does not end with '.pack'",
 		index_name_buf = xmalloc(len);
 	while ((de = readdir(dir)) != NULL) {
 		int namelen = strlen(de->d_name);
 		if (namelen != 50 ||
-		    !has_extension(de->d_name, namelen, ".pack"))
+		    !has_extension(de->d_name, ".pack"))
 		get_sha1_hex(de->d_name + 5, sha1);
 			namelen = strlen(de->d_name);
 			if (namelen > 255)
-			if (has_extension(de->d_name, namelen, ".lock"))
+			if (has_extension(de->d_name, ".lock"))
 			memcpy(path + baselen, de->d_name, namelen+1);
 			if (stat(git_path("%s", path), &st) < 0)
 		int namelen = strlen(de->d_name);
 		struct packed_git *p;
-		if (!has_extension(de->d_name, namelen, ".idx"))
+		if (!has_extension(de->d_name, ".idx"))
 		/* we have .idx.  Is it a file we can map? */
 	 * In addition to "foo.idx" we accept "foo.pack" and "foo";
 	 * normalize these forms to "foo.idx" for add_packed_git().
-	if (has_extension(arg, len, ".pack")) {
+	if (has_extension(arg, ".pack")) {
 		strcpy(arg + len - 5, ".idx");
-	} else if (!has_extension(arg, len, ".idx")) {
+	} else if (!has_extension(arg, ".idx")) {
 		if (len + 4 >= PATH_MAX)
 			return error("name too long: %s.idx", arg);
 		strcpy(arg + len, ".idx");
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.