Skip to content

Commit cf0fc3e

Browse files
committed
Merge branch 'sj/string-list' into next
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"
2 parents a81dc9f + 22e7bc8 commit cf0fc3e

File tree

5 files changed

+35
-36
lines changed

5 files changed

+35
-36
lines changed

add-interactive.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -244,16 +244,17 @@ static void find_unique_prefixes(struct prefix_item_list *list)
244244

245245
static ssize_t find_unique(const char *string, struct prefix_item_list *list)
246246
{
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);
248249
struct string_list_item *item;
249250

250251
if (list->items.nr != list->sorted.nr)
251252
BUG("prefix_item_list in inconsistent state (%"PRIuMAX
252253
" vs %"PRIuMAX")",
253254
(uintmax_t)list->items.nr, (uintmax_t)list->sorted.nr);
254255

255-
if (index < 0)
256-
item = list->sorted.items[-1 - index].util;
256+
if (exact_match)
257+
item = list->sorted.items[index].util;
257258
else if (index > 0 &&
258259
starts_with(list->sorted.items[index - 1].string, string))
259260
return -1;

mailmap.c

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#define USE_THE_REPOSITORY_VARIABLE
2-
#define DISABLE_SIGN_COMPARE_WARNINGS
32

43
#include "git-compat-util.h"
54
#include "environment.h"
@@ -243,10 +242,9 @@ void clear_mailmap(struct string_list *map)
243242
static struct string_list_item *lookup_prefix(struct string_list *map,
244243
const char *string, size_t len)
245244
{
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) {
250248
if (!string[len])
251249
return &map->items[i];
252250
/*
@@ -267,7 +265,7 @@ static struct string_list_item *lookup_prefix(struct string_list *map,
267265
* overlong key would be inserted, which must come after the
268266
* real location of the key if one exists.
269267
*/
270-
while (0 <= --i && i < map->nr) {
268+
while (i-- && i < map->nr) {
271269
int cmp = strncasecmp(map->items[i].string, string, len);
272270
if (cmp < 0)
273271
/*

refs.c

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
*/
44

55
#define USE_THE_REPOSITORY_VARIABLE
6-
#define DISABLE_SIGN_COMPARE_WARNINGS
76

87
#include "git-compat-util.h"
98
#include "advice.h"
@@ -1714,8 +1713,6 @@ const char *find_descendant_ref(const char *dirname,
17141713
const struct string_list *extras,
17151714
const struct string_list *skip)
17161715
{
1717-
int pos;
1718-
17191716
if (!extras)
17201717
return NULL;
17211718

@@ -1725,7 +1722,7 @@ const char *find_descendant_ref(const char *dirname,
17251722
* with dirname (remember, dirname includes the trailing
17261723
* slash) and is not in skip, then we have a conflict.
17271724
*/
1728-
for (pos = string_list_find_insert_index(extras, dirname, 0);
1725+
for (size_t pos = string_list_find_insert_index(extras, dirname, NULL);
17291726
pos < extras->nr; pos++) {
17301727
const char *extra_refname = extras->items[pos].string;
17311728

@@ -2414,7 +2411,7 @@ static int run_transaction_hook(struct ref_transaction *transaction,
24142411
struct child_process proc = CHILD_PROCESS_INIT;
24152412
struct strbuf buf = STRBUF_INIT;
24162413
const char *hook;
2417-
int ret = 0, i;
2414+
int ret = 0;
24182415

24192416
hook = find_hook(transaction->ref_store->repo, "reference-transaction");
24202417
if (!hook)
@@ -2431,7 +2428,7 @@ static int run_transaction_hook(struct ref_transaction *transaction,
24312428

24322429
sigchain_push(SIGPIPE, SIG_IGN);
24332430

2434-
for (i = 0; i < transaction->nr; i++) {
2431+
for (size_t i = 0; i < transaction->nr; i++) {
24352432
struct ref_update *update = transaction->updates[i];
24362433

24372434
if (update->flags & REF_LOG_ONLY)
@@ -2824,9 +2821,7 @@ void ref_transaction_for_each_queued_update(struct ref_transaction *transaction,
28242821
ref_transaction_for_each_queued_update_fn cb,
28252822
void *cb_data)
28262823
{
2827-
int i;
2828-
2829-
for (i = 0; i < transaction->nr; i++) {
2824+
for (size_t i = 0; i < transaction->nr; i++) {
28302825
struct ref_update *update = transaction->updates[i];
28312826

28322827
cb(update->refname,

string-list.c

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ void string_list_init_dup(struct string_list *list)
1616
/* if there is no exact match, point to the index where the entry could be
1717
* inserted */
1818
static size_t get_entry_index(const struct string_list *list, const char *string,
19-
int *exact_match)
19+
bool *exact_match)
2020
{
2121
size_t left = 0, right = list->nr;
2222
compare_strings_fn cmp = list->cmp ? list->cmp : strcmp;
@@ -29,18 +29,20 @@ static size_t get_entry_index(const struct string_list *list, const char *string
2929
else if (compare > 0)
3030
left = middle + 1;
3131
else {
32-
*exact_match = 1;
32+
if (exact_match)
33+
*exact_match = true;
3334
return middle;
3435
}
3536
}
3637

37-
*exact_match = 0;
38+
if (exact_match)
39+
*exact_match = false;
3840
return right;
3941
}
4042

4143
static size_t add_entry(struct string_list *list, const char *string)
4244
{
43-
int exact_match = 0;
45+
bool exact_match;
4446
size_t index = get_entry_index(list, string, &exact_match);
4547

4648
if (exact_match)
@@ -68,7 +70,7 @@ struct string_list_item *string_list_insert(struct string_list *list, const char
6870
void string_list_remove(struct string_list *list, const char *string,
6971
int free_util)
7072
{
71-
int exact_match;
73+
bool exact_match;
7274
int i = get_entry_index(list, string, &exact_match);
7375

7476
if (exact_match) {
@@ -82,26 +84,23 @@ void string_list_remove(struct string_list *list, const char *string,
8284
}
8385
}
8486

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)
8688
{
87-
int exact_match;
89+
bool exact_match;
8890
get_entry_index(list, string, &exact_match);
8991
return exact_match;
9092
}
9193

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)
9496
{
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);
10098
}
10199

102100
struct string_list_item *string_list_lookup(struct string_list *list, const char *string)
103101
{
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);
105104
if (!exact_match)
106105
return NULL;
107106
return list->items + i;

string-list.h

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -172,9 +172,15 @@ void string_list_remove_empty_items(struct string_list *list, int free_util);
172172
/* Use these functions only on sorted lists: */
173173

174174
/** 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);
178184

179185
/**
180186
* Insert a new element to the string_list. The returned pointer can

0 commit comments

Comments
 (0)