{"thread":{"id":"64106","subject":"[PATCH 3/4] string-list: change \"string_list_find_insert_index\" return type to \"size_t\"","startedAt":"2025-09-07T16:40:44Z","lastAt":"2025-09-07T16:40:44Z","messageCount":1,"participants":["shejialuo"],"isPatch":true,"patchVersion":1,"patchTotal":4},"messages":[{"id":"525715","messageId":"aL21iR390y0kwRWV@ArchLinux","threadId":"64106","inReplyTo":null,"subject":"[PATCH 3/4] string-list: change \"string_list_find_insert_index\" return type to \"size_t\"","fromName":"shejialuo","fromEmail":"shejialuo@gmail.com","sentAt":"2025-09-07T16:40:41Z","receivedAt":"2025-09-07T16:40:44Z","isPatch":true,"sender":{"key":"shejialuo@gmail.com","avatar":"https://avatars.githubusercontent.com/u/56911263?v=4"},"body":"As \"string_list_find_insert_index\" is a simple wrapper of\n\"get_entry_index\", we could simply change its return type to \"size_t\".\n\nUpdate all callers to use size_t variables for storing the return value.\nThe tricky fix is the loop condition in \"mailmap.c\" to properly handle\n\"size_t\" underflow by changing from `0 <= --i` to `i-- > 0`.\n\nRemove \"DISABLE_SIGN_COMPARE_WARNINGS\" from \"mailmap.c\" as it's no\nlonger needed with the proper unsigned types.\n\nSigned-off-by: shejialuo <shejialuo@gmail.com>\n---\n add-interactive.c | 2 +-\n mailmap.c         | 5 ++---\n refs.c            | 4 +---\n string-list.c     | 4 ++--\n string-list.h     | 4 ++--\n 5 files changed, 8 insertions(+), 11 deletions(-)\n\ndiff --git a/add-interactive.c b/add-interactive.c\nindex 9a42b3b38b..2005f56b69 100644\n--- a/add-interactive.c\n+++ b/add-interactive.c\n@@ -222,7 +222,7 @@ static void find_unique_prefixes(struct prefix_item_list *list)\n static ssize_t find_unique(const char *string, struct prefix_item_list *list)\n {\n \tint exact_match;\n-\tint index = string_list_find_insert_index(&list->sorted, string, &exact_match);\n+\tsize_t index = string_list_find_insert_index(&list->sorted, string, &exact_match);\n \tstruct string_list_item *item;\n \n \tif (list->items.nr != list->sorted.nr)\ndiff --git a/mailmap.c b/mailmap.c\nindex 253517cdf6..0168342650 100644\n--- a/mailmap.c\n+++ b/mailmap.c\n@@ -1,5 +1,4 @@\n #define USE_THE_REPOSITORY_VARIABLE\n-#define DISABLE_SIGN_COMPARE_WARNINGS\n \n #include \"git-compat-util.h\"\n #include \"environment.h\"\n@@ -244,7 +243,7 @@ static struct string_list_item *lookup_prefix(struct string_list *map,\n \t\t\t\t\t      const char *string, size_t len)\n {\n \tint exact_match;\n-\tint i = string_list_find_insert_index(map, string, &exact_match);\n+\tsize_t i = string_list_find_insert_index(map, string, &exact_match);\n \tif (exact_match) {\n \t\tif (!string[len])\n \t\t\treturn &map->items[i];\n@@ -266,7 +265,7 @@ static struct string_list_item *lookup_prefix(struct string_list *map,\n \t * overlong key would be inserted, which must come after the\n \t * real location of the key if one exists.\n \t */\n-\twhile (0 <= --i && i < map->nr) {\n+\twhile (i-- > 0 && i < map->nr) {\n \t\tint cmp = strncasecmp(map->items[i].string, string, len);\n \t\tif (cmp < 0)\n \t\t\t/*\ndiff --git a/refs.c b/refs.c\nindex f1ff5bf846..a8f06b9a0a 100644\n--- a/refs.c\n+++ b/refs.c\n@@ -1688,8 +1688,6 @@ const char *find_descendant_ref(const char *dirname,\n \t\t\t\tconst struct string_list *extras,\n \t\t\t\tconst struct string_list *skip)\n {\n-\tint pos;\n-\n \tif (!extras)\n \t\treturn NULL;\n \n@@ -1699,7 +1697,7 @@ const char *find_descendant_ref(const char *dirname,\n \t * with dirname (remember, dirname includes the trailing\n \t * slash) and is not in skip, then we have a conflict.\n \t */\n-\tfor (pos = string_list_find_insert_index(extras, dirname, NULL);\n+\tfor (size_t pos = string_list_find_insert_index(extras, dirname, NULL);\n \t     pos < extras->nr; pos++) {\n \t\tconst char *extra_refname = extras->items[pos].string;\n \ndiff --git a/string-list.c b/string-list.c\nindex 224bc182ff..e69923cd88 100644\n--- a/string-list.c\n+++ b/string-list.c\n@@ -91,8 +91,8 @@ int string_list_has_string(const struct string_list *list, const char *string)\n \treturn exact_match;\n }\n \n-int string_list_find_insert_index(const struct string_list *list, const char *string,\n-\t\t\t\t  int *exact_match)\n+size_t string_list_find_insert_index(const struct string_list *list, const char *string,\n+\t\t\t\t     int *exact_match)\n {\n \treturn get_entry_index(list, string, exact_match);\n }\ndiff --git a/string-list.h b/string-list.h\nindex 03c7009472..f6be2bd5c7 100644\n--- a/string-list.h\n+++ b/string-list.h\n@@ -173,8 +173,8 @@ void string_list_remove_empty_items(struct string_list *list, int free_util);\n \n /** Determine if the string_list has a given string or not. */\n int string_list_has_string(const struct string_list *list, const char *string);\n-int string_list_find_insert_index(const struct string_list *list, const char *string,\n-\t\t\t\t  int *exact_match);\n+size_t string_list_find_insert_index(const struct string_list *list, const char *string,\n+\t\t\t\t     int *exact_match);\n \n /**\n  * Insert a new element to the string_list. The returned pointer can\n-- \n2.51.0\n\n"}]}