Git fork

Merge branch 'sj/string-list'

The "string-list" API function to find where a given string would
be inserted got updated so that it can use unrealistically huge
array index that would only fit in size_t but not int or ssize_t
to achieve unstated goal.

* sj/string-list:
refs: enable sign compare warnings check
string-list: change "string_list_find_insert_index" return type to "size_t"
string-list: replace negative index encoding with "exact_match" parameter
string-list: use bool instead of int for "exact_match"

+35 -36
+4 -3
add-interactive.c
··· 244 244 245 245 static ssize_t find_unique(const char *string, struct prefix_item_list *list) 246 246 { 247 - int index = string_list_find_insert_index(&list->sorted, string, 1); 247 + bool exact_match; 248 + size_t index = string_list_find_insert_index(&list->sorted, string, &exact_match); 248 249 struct string_list_item *item; 249 250 250 251 if (list->items.nr != list->sorted.nr) ··· 252 253 " vs %"PRIuMAX")", 253 254 (uintmax_t)list->items.nr, (uintmax_t)list->sorted.nr); 254 255 255 - if (index < 0) 256 - item = list->sorted.items[-1 - index].util; 256 + if (exact_match) 257 + item = list->sorted.items[index].util; 257 258 else if (index > 0 && 258 259 starts_with(list->sorted.items[index - 1].string, string)) 259 260 return -1;
+4 -6
mailmap.c
··· 1 1 #define USE_THE_REPOSITORY_VARIABLE 2 - #define DISABLE_SIGN_COMPARE_WARNINGS 3 2 4 3 #include "git-compat-util.h" 5 4 #include "environment.h" ··· 243 242 static struct string_list_item *lookup_prefix(struct string_list *map, 244 243 const char *string, size_t len) 245 244 { 246 - int i = string_list_find_insert_index(map, string, 1); 247 - if (i < 0) { 248 - /* exact match */ 249 - i = -1 - i; 245 + bool exact_match; 246 + size_t i = string_list_find_insert_index(map, string, &exact_match); 247 + if (exact_match) { 250 248 if (!string[len]) 251 249 return &map->items[i]; 252 250 /* ··· 267 265 * overlong key would be inserted, which must come after the 268 266 * real location of the key if one exists. 269 267 */ 270 - while (0 <= --i && i < map->nr) { 268 + while (i-- && i < map->nr) { 271 269 int cmp = strncasecmp(map->items[i].string, string, len); 272 270 if (cmp < 0) 273 271 /*
+4 -9
refs.c
··· 3 3 */ 4 4 5 5 #define USE_THE_REPOSITORY_VARIABLE 6 - #define DISABLE_SIGN_COMPARE_WARNINGS 7 6 8 7 #include "git-compat-util.h" 9 8 #include "advice.h" ··· 1714 1713 const struct string_list *extras, 1715 1714 const struct string_list *skip) 1716 1715 { 1717 - int pos; 1718 - 1719 1716 if (!extras) 1720 1717 return NULL; 1721 1718 ··· 1725 1722 * with dirname (remember, dirname includes the trailing 1726 1723 * slash) and is not in skip, then we have a conflict. 1727 1724 */ 1728 - for (pos = string_list_find_insert_index(extras, dirname, 0); 1725 + for (size_t pos = string_list_find_insert_index(extras, dirname, NULL); 1729 1726 pos < extras->nr; pos++) { 1730 1727 const char *extra_refname = extras->items[pos].string; 1731 1728 ··· 2414 2411 struct child_process proc = CHILD_PROCESS_INIT; 2415 2412 struct strbuf buf = STRBUF_INIT; 2416 2413 const char *hook; 2417 - int ret = 0, i; 2414 + int ret = 0; 2418 2415 2419 2416 hook = find_hook(transaction->ref_store->repo, "reference-transaction"); 2420 2417 if (!hook) ··· 2431 2428 2432 2429 sigchain_push(SIGPIPE, SIG_IGN); 2433 2430 2434 - for (i = 0; i < transaction->nr; i++) { 2431 + for (size_t i = 0; i < transaction->nr; i++) { 2435 2432 struct ref_update *update = transaction->updates[i]; 2436 2433 2437 2434 if (update->flags & REF_LOG_ONLY) ··· 2824 2821 ref_transaction_for_each_queued_update_fn cb, 2825 2822 void *cb_data) 2826 2823 { 2827 - int i; 2828 - 2829 - for (i = 0; i < transaction->nr; i++) { 2824 + for (size_t i = 0; i < transaction->nr; i++) { 2830 2825 struct ref_update *update = transaction->updates[i]; 2831 2826 2832 2827 cb(update->refname,
+14 -15
string-list.c
··· 16 16 /* if there is no exact match, point to the index where the entry could be 17 17 * inserted */ 18 18 static size_t get_entry_index(const struct string_list *list, const char *string, 19 - int *exact_match) 19 + bool *exact_match) 20 20 { 21 21 size_t left = 0, right = list->nr; 22 22 compare_strings_fn cmp = list->cmp ? list->cmp : strcmp; ··· 29 29 else if (compare > 0) 30 30 left = middle + 1; 31 31 else { 32 - *exact_match = 1; 32 + if (exact_match) 33 + *exact_match = true; 33 34 return middle; 34 35 } 35 36 } 36 37 37 - *exact_match = 0; 38 + if (exact_match) 39 + *exact_match = false; 38 40 return right; 39 41 } 40 42 41 43 static size_t add_entry(struct string_list *list, const char *string) 42 44 { 43 - int exact_match = 0; 45 + bool exact_match; 44 46 size_t index = get_entry_index(list, string, &exact_match); 45 47 46 48 if (exact_match) ··· 68 70 void string_list_remove(struct string_list *list, const char *string, 69 71 int free_util) 70 72 { 71 - int exact_match; 73 + bool exact_match; 72 74 int i = get_entry_index(list, string, &exact_match); 73 75 74 76 if (exact_match) { ··· 82 84 } 83 85 } 84 86 85 - int string_list_has_string(const struct string_list *list, const char *string) 87 + bool string_list_has_string(const struct string_list *list, const char *string) 86 88 { 87 - int exact_match; 89 + bool exact_match; 88 90 get_entry_index(list, string, &exact_match); 89 91 return exact_match; 90 92 } 91 93 92 - int string_list_find_insert_index(const struct string_list *list, const char *string, 93 - int negative_existing_index) 94 + size_t string_list_find_insert_index(const struct string_list *list, const char *string, 95 + bool *exact_match) 94 96 { 95 - int exact_match; 96 - int index = get_entry_index(list, string, &exact_match); 97 - if (exact_match) 98 - index = -1 - (negative_existing_index ? index : 0); 99 - return index; 97 + return get_entry_index(list, string, exact_match); 100 98 } 101 99 102 100 struct string_list_item *string_list_lookup(struct string_list *list, const char *string) 103 101 { 104 - int exact_match, i = get_entry_index(list, string, &exact_match); 102 + bool exact_match; 103 + size_t i = get_entry_index(list, string, &exact_match); 105 104 if (!exact_match) 106 105 return NULL; 107 106 return list->items + i;
+9 -3
string-list.h
··· 172 172 /* Use these functions only on sorted lists: */ 173 173 174 174 /** Determine if the string_list has a given string or not. */ 175 - int string_list_has_string(const struct string_list *list, const char *string); 176 - int string_list_find_insert_index(const struct string_list *list, const char *string, 177 - int negative_existing_index); 175 + bool string_list_has_string(const struct string_list *list, const char *string); 176 + 177 + /** 178 + * Find the index at which a new element should be inserted into the 179 + * string_list to maintain sorted order. If exact_match is not NULL, 180 + * it will be set to true if the string already exists in the list. 181 + */ 182 + size_t string_list_find_insert_index(const struct string_list *list, const char *string, 183 + bool *exact_match); 178 184 179 185 /** 180 186 * Insert a new element to the string_list. The returned pointer can