{"thread":{"id":"5208","subject":"[PATCH] drop length argument of has_extension","startedAt":"2006-08-11T12:01:45Z","lastAt":"2006-08-11T12:01:45Z","messageCount":1,"participants":["Rene Scharfe"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"25129","messageId":"20060811120145.GA24982@lsrfire.ath.cx","threadId":"5208","inReplyTo":null,"subject":"[PATCH] drop length argument of has_extension","fromName":"Rene Scharfe","fromEmail":"rene.scharfe@lsrfire.ath.cx","sentAt":"2006-08-11T12:01:45Z","receivedAt":"2006-08-11T12:01:45Z","isPatch":true,"sender":{"key":"l.s.r@web.de","avatar":"https://avatars.githubusercontent.com/u/26122331?v=4"},"body":"As Fredrik points out the current interface of has_extension() is\npotentially confusing.  Its parameters include both a nul-terminated\nstring and a length-limited string.\n\nThis patch drops the length argument, requiring two nul-terminated\nstrings; all callsites are updated.  I checked that all of them indeed\nprovide nul-terminated strings.  Filenames need to be nul-terminated\nanyway if they are to be passed to open() etc.  The performance penalty\nof the additional strlen() is negligible compared to the system calls\nwhich inevitably surround has_extension() calls.\n\nAdditionally, change has_extension() to use size_t inside instead of\nint, as that is the exact type strlen() returns and memcmp() expects.\n\nThis patch applies to the current next branch (9d8df19).\n\nSigned-off-by: Rene Scharfe <rene.scharfe@lsrfire.ath.cx>\n---\n\n builtin-verify-pack.c |    4 ++--\n git-compat-util.h     |    5 +++--\n help.c                |    2 +-\n http-fetch.c          |    2 +-\n index-pack.c          |    2 +-\n local-fetch.c         |    2 +-\n refs.c                |    2 +-\n sha1_file.c           |    2 +-\n 8 files changed, 11 insertions(+), 10 deletions(-)\n\ndiff --git a/builtin-verify-pack.c b/builtin-verify-pack.c\nindex d700761..7d39d9b 100644\n--- a/builtin-verify-pack.c\n+++ b/builtin-verify-pack.c\n@@ -17,10 +17,10 @@ static int verify_one_pack(const char *p\n \t * In addition to \"foo.idx\" we accept \"foo.pack\" and \"foo\";\n \t * normalize these forms to \"foo.idx\" for add_packed_git().\n \t */\n-\tif (has_extension(arg, len, \".pack\")) {\n+\tif (has_extension(arg, \".pack\")) {\n \t\tstrcpy(arg + len - 5, \".idx\");\n \t\tlen--;\n-\t} else if (!has_extension(arg, len, \".idx\")) {\n+\t} else if (!has_extension(arg, \".idx\")) {\n \t\tif (len + 4 >= PATH_MAX)\n \t\t\treturn error(\"name too long: %s.idx\", arg);\n \t\tstrcpy(arg + len, \".idx\");\ndiff --git a/git-compat-util.h b/git-compat-util.h\nindex dd92093..b2e1895 100644\n--- a/git-compat-util.h\n+++ b/git-compat-util.h\n@@ -139,9 +139,10 @@ static inline ssize_t xwrite(int fd, con\n \t}\n }\n \n-static inline int has_extension(const char *filename, int len, const char *ext)\n+static inline int has_extension(const char *filename, const char *ext)\n {\n-\tint extlen = strlen(ext);\n+\tsize_t len = strlen(filename);\n+\tsize_t extlen = strlen(ext);\n \treturn len > extlen && !memcmp(filename + len - extlen, ext, extlen);\n }\n \ndiff --git a/help.c b/help.c\nindex 7a7f775..6484cb9 100644\n--- a/help.c\n+++ b/help.c\n@@ -140,7 +140,7 @@ static void list_commands(const char *ex\n \t\t\tcontinue;\n \n \t\tentlen = strlen(de->d_name);\n-\t\tif (has_extension(de->d_name, entlen, \".exe\"))\n+\t\tif (has_extension(de->d_name, \".exe\"))\n \t\t\tentlen -= 4;\n \n \t\tif (longest < entlen)\ndiff --git a/http-fetch.c b/http-fetch.c\nindex 6ea39f0..de5fc44 100644\n--- a/http-fetch.c\n+++ b/http-fetch.c\n@@ -870,7 +870,7 @@ static void process_ls_pack(struct remot\n \n \tif (strlen(ls->dentry_name) == 63 &&\n \t    !strncmp(ls->dentry_name, \"objects/pack/pack-\", 18) &&\n-\t    has_extension(ls->dentry_name, 63, \".pack\")) {\n+\t    has_extension(ls->dentry_name, \".pack\")) {\n \t\tget_sha1_hex(ls->dentry_name + 18, sha1);\n \t\tsetup_index(ls->repo, sha1);\n \t}\ndiff --git a/index-pack.c b/index-pack.c\nindex a91e39e..b20659c 100644\n--- a/index-pack.c\n+++ b/index-pack.c\n@@ -447,7 +447,7 @@ int main(int argc, char **argv)\n \t\tusage(index_pack_usage);\n \tif (!index_name) {\n \t\tint len = strlen(pack_name);\n-\t\tif (!has_extension(pack_name, len, \".pack\"))\n+\t\tif (!has_extension(pack_name, \".pack\"))\n \t\t\tdie(\"packfile name '%s' does not end with '.pack'\",\n \t\t\t    pack_name);\n \t\tindex_name_buf = xmalloc(len);\ndiff --git a/local-fetch.c b/local-fetch.c\nindex b6ec170..7d01845 100644\n--- a/local-fetch.c\n+++ b/local-fetch.c\n@@ -44,7 +44,7 @@ static int setup_indices(void)\n \twhile ((de = readdir(dir)) != NULL) {\n \t\tint namelen = strlen(de->d_name);\n \t\tif (namelen != 50 ||\n-\t\t    !has_extension(de->d_name, namelen, \".pack\"))\n+\t\t    !has_extension(de->d_name, \".pack\"))\n \t\t\tcontinue;\n \t\tget_sha1_hex(de->d_name + 5, sha1);\n \t\tsetup_index(sha1);\ndiff --git a/refs.c b/refs.c\nindex b01835f..28a9394 100644\n--- a/refs.c\n+++ b/refs.c\n@@ -147,7 +147,7 @@ static int do_for_each_ref(const char *b\n \t\t\tnamelen = strlen(de->d_name);\n \t\t\tif (namelen > 255)\n \t\t\t\tcontinue;\n-\t\t\tif (has_extension(de->d_name, namelen, \".lock\"))\n+\t\t\tif (has_extension(de->d_name, \".lock\"))\n \t\t\t\tcontinue;\n \t\t\tmemcpy(path + baselen, de->d_name, namelen+1);\n \t\t\tif (stat(git_path(\"%s\", path), &st) < 0)\ndiff --git a/sha1_file.c b/sha1_file.c\nindex f0a4a7e..ed52d71 100644\n--- a/sha1_file.c\n+++ b/sha1_file.c\n@@ -608,7 +608,7 @@ static void prepare_packed_git_one(char \n \t\tint namelen = strlen(de->d_name);\n \t\tstruct packed_git *p;\n \n-\t\tif (!has_extension(de->d_name, namelen, \".idx\"))\n+\t\tif (!has_extension(de->d_name, \".idx\"))\n \t\t\tcontinue;\n \n \t\t/* we have .idx.  Is it a file we can map? */\n"}]}