Skip to content

Commit f2f940a

Browse files
shejialuogitster
authored andcommitted
string-list: fix sign compare warnings
In "string-list.c", there are six warnings which are emitted by "Wsign-compare". And five warnings are caused by the loop iterator type mismatch, which could be simply fixed by changing the `int` type to `size_t` type. However, for "string-list.c::add_entry" function, we compare the `index` of the `int` type with the `list->nr` of unsigned type. It seems that we could just simply convert the type of `index` from `int` to `size_t`. But actually this is a correct behavior. We would set the `index` value by checking whether `insert_at` is -1. If not, we would set `index` to be `insert_at`, otherwise we would use "get_entry_index` to find the inserted position. What if the caller passes a negative value except "-1", the compiler would convert the `index` to be a positive value which would make the `if` statement be false to avoid moving array. However, we would definitely encounter trouble when setting the inserted item. And we only call "add_entry" in "string_list_insert" function, and we simply pass "-1" for "insert_at" parameter. So, we never use this parameter to insert element in a user specified position. Let's delete this parameter. If there is any requirement later, we may use a better way to do this. And then we could safely convert the index to be `size_t` when comparing. Signed-off-by: shejialuo <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 683c54c commit f2f940a

File tree

1 file changed

+13
-17
lines changed

1 file changed

+13
-17
lines changed

string-list.c

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
#define DISABLE_SIGN_COMPARE_WARNINGS
2-
31
#include "git-compat-util.h"
42
#include "string-list.h"
53

@@ -41,16 +39,16 @@ static int get_entry_index(const struct string_list *list, const char *string,
4139
}
4240

4341
/* returns -1-index if already exists */
44-
static int add_entry(int insert_at, struct string_list *list, const char *string)
42+
static int add_entry(struct string_list *list, const char *string)
4543
{
4644
int exact_match = 0;
47-
int index = insert_at != -1 ? insert_at : get_entry_index(list, string, &exact_match);
45+
int index = get_entry_index(list, string, &exact_match);
4846

4947
if (exact_match)
5048
return -1 - index;
5149

5250
ALLOC_GROW(list->items, list->nr+1, list->alloc);
53-
if (index < list->nr)
51+
if ((size_t)index < list->nr)
5452
MOVE_ARRAY(list->items + index + 1, list->items + index,
5553
list->nr - index);
5654
list->items[index].string = list->strdup_strings ?
@@ -63,7 +61,7 @@ static int add_entry(int insert_at, struct string_list *list, const char *string
6361

6462
struct string_list_item *string_list_insert(struct string_list *list, const char *string)
6563
{
66-
int index = add_entry(-1, list, string);
64+
int index = add_entry(list, string);
6765

6866
if (index < 0)
6967
index = -1 - index;
@@ -116,7 +114,7 @@ struct string_list_item *string_list_lookup(struct string_list *list, const char
116114
void string_list_remove_duplicates(struct string_list *list, int free_util)
117115
{
118116
if (list->nr > 1) {
119-
int src, dst;
117+
size_t src, dst;
120118
compare_strings_fn cmp = list->cmp ? list->cmp : strcmp;
121119
for (src = dst = 1; src < list->nr; src++) {
122120
if (!cmp(list->items[dst - 1].string, list->items[src].string)) {
@@ -134,8 +132,8 @@ void string_list_remove_duplicates(struct string_list *list, int free_util)
134132
int for_each_string_list(struct string_list *list,
135133
string_list_each_func_t fn, void *cb_data)
136134
{
137-
int i, ret = 0;
138-
for (i = 0; i < list->nr; i++)
135+
int ret = 0;
136+
for (size_t i = 0; i < list->nr; i++)
139137
if ((ret = fn(&list->items[i], cb_data)))
140138
break;
141139
return ret;
@@ -144,8 +142,8 @@ int for_each_string_list(struct string_list *list,
144142
void filter_string_list(struct string_list *list, int free_util,
145143
string_list_each_func_t want, void *cb_data)
146144
{
147-
int src, dst = 0;
148-
for (src = 0; src < list->nr; src++) {
145+
size_t dst = 0;
146+
for (size_t src = 0; src < list->nr; src++) {
149147
if (want(&list->items[src], cb_data)) {
150148
list->items[dst++] = list->items[src];
151149
} else {
@@ -171,13 +169,12 @@ void string_list_remove_empty_items(struct string_list *list, int free_util)
171169
void string_list_clear(struct string_list *list, int free_util)
172170
{
173171
if (list->items) {
174-
int i;
175172
if (list->strdup_strings) {
176-
for (i = 0; i < list->nr; i++)
173+
for (size_t i = 0; i < list->nr; i++)
177174
free(list->items[i].string);
178175
}
179176
if (free_util) {
180-
for (i = 0; i < list->nr; i++)
177+
for (size_t i = 0; i < list->nr; i++)
181178
free(list->items[i].util);
182179
}
183180
free(list->items);
@@ -189,13 +186,12 @@ void string_list_clear(struct string_list *list, int free_util)
189186
void string_list_clear_func(struct string_list *list, string_list_clear_func_t clearfunc)
190187
{
191188
if (list->items) {
192-
int i;
193189
if (clearfunc) {
194-
for (i = 0; i < list->nr; i++)
190+
for (size_t i = 0; i < list->nr; i++)
195191
clearfunc(list->items[i].util, list->items[i].string);
196192
}
197193
if (list->strdup_strings) {
198-
for (i = 0; i < list->nr; i++)
194+
for (size_t i = 0; i < list->nr; i++)
199195
free(list->items[i].string);
200196
}
201197
free(list->items);

0 commit comments

Comments
 (0)