@matheusbsbs / matheus-bagliano / commits / 5bb1cda5f7
Fork 0

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 <rene.scharfe@lsrfire.ath.cx> Signed-off-by: Junio C Hamano <junkio@cox.net>

Rene Scharfe committed Aug 11, 2006 at 14:01 UTC 5bb1cda5f73988963e7470f3cd75a380751f6d99
8 files changed +11 -10
builtin-help.c
+1 -1
@@ -140,7 +140,7 @@ static void list_commands(const char *exec_path, const char *pattern)
140 continue;
141
142 entlen = strlen(de->d_name);
143 - if (has_extension(de->d_name, entlen, ".exe"))
143 + if (has_extension(de->d_name, ".exe"))
144 entlen -= 4;
145
146 if (longest < entlen)
git-compat-util.h
+3 -2
@@ -139,9 +139,10 @@ static inline ssize_t xwrite(int fd, const void *buf, size_t len)
139 }
140 }
141
142 -static inline int has_extension(const char *filename, int len, const char *ext)
142 +static inline int has_extension(const char *filename, const char *ext)
143 {
144 - int extlen = strlen(ext);
144 + size_t len = strlen(filename);
145 + size_t extlen = strlen(ext);
146 return len > extlen && !memcmp(filename + len - extlen, ext, extlen);
147 }
148
http-fetch.c
+1 -1
@@ -870,7 +870,7 @@ static void process_ls_pack(struct remote_ls_ctx *ls)
870
871 if (strlen(ls->dentry_name) == 63 &&
872 !strncmp(ls->dentry_name, "objects/pack/pack-", 18) &&
873 - has_extension(ls->dentry_name, 63, ".pack")) {
873 + has_extension(ls->dentry_name, ".pack")) {
874 get_sha1_hex(ls->dentry_name + 18, sha1);
875 setup_index(ls->repo, sha1);
876 }
index-pack.c
+1 -1
@@ -447,7 +447,7 @@ int main(int argc, char **argv)
447 usage(index_pack_usage);
448 if (!index_name) {
449 int len = strlen(pack_name);
450 - if (!has_extension(pack_name, len, ".pack"))
450 + if (!has_extension(pack_name, ".pack"))
451 die("packfile name '%s' does not end with '.pack'",
452 pack_name);
453 index_name_buf = xmalloc(len);
local-fetch.c
+1 -1
@@ -44,7 +44,7 @@ static int setup_indices(void)
44 while ((de = readdir(dir)) != NULL) {
45 int namelen = strlen(de->d_name);
46 if (namelen != 50 ||
47 - !has_extension(de->d_name, namelen, ".pack"))
47 + !has_extension(de->d_name, ".pack"))
48 continue;
49 get_sha1_hex(de->d_name + 5, sha1);
50 setup_index(sha1);
refs.c
+1 -1
@@ -147,7 +147,7 @@ static int do_for_each_ref(const char *base, int (*fn)(const char *path, const u
147 namelen = strlen(de->d_name);
148 if (namelen > 255)
149 continue;
150 - if (has_extension(de->d_name, namelen, ".lock"))
150 + if (has_extension(de->d_name, ".lock"))
151 continue;
152 memcpy(path + baselen, de->d_name, namelen+1);
153 if (stat(git_path("%s", path), &st) < 0)
sha1_file.c
+1 -1
@@ -590,7 +590,7 @@ static void prepare_packed_git_one(char *objdir, int local)
590 int namelen = strlen(de->d_name);
591 struct packed_git *p;
592
593 - if (!has_extension(de->d_name, namelen, ".idx"))
593 + if (!has_extension(de->d_name, ".idx"))
594 continue;
595
596 /* we have .idx. Is it a file we can map? */
verify-pack.c
+2 -2
@@ -16,10 +16,10 @@ static int verify_one_pack(const char *path, int verbose)
16 * In addition to "foo.idx" we accept "foo.pack" and "foo";
17 * normalize these forms to "foo.idx" for add_packed_git().
18 */
19 - if (has_extension(arg, len, ".pack")) {
19 + if (has_extension(arg, ".pack")) {
20 strcpy(arg + len - 5, ".idx");
21 len--;
22 - } else if (!has_extension(arg, len, ".idx")) {
22 + } else if (!has_extension(arg, ".idx")) {
23 if (len + 4 >= PATH_MAX)
24 return error("name too long: %s.idx", arg);
25 strcpy(arg + len, ".idx");