Skip to content

Commit b6d3f5a

Browse files
bbolligitster
authored andcommitted
string-list.c: avoid conversion from void * to function pointer
ISO C forbids the conversion of void pointers to function pointers. Introduce a context struct that encapsulates the function pointer. Signed-off-by: Beat Bolli <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 9ad3635 commit b6d3f5a

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

string-list.c

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -224,18 +224,28 @@ struct string_list_item *string_list_append(struct string_list *list,
224224
list->strdup_strings ? xstrdup(string) : (char *)string);
225225
}
226226

227+
/*
228+
* Encapsulate the compare function pointer because ISO C99 forbids
229+
* casting from void * to a function pointer and vice versa.
230+
*/
231+
struct string_list_sort_ctx
232+
{
233+
compare_strings_fn cmp;
234+
};
235+
227236
static int cmp_items(const void *a, const void *b, void *ctx)
228237
{
229-
compare_strings_fn cmp = ctx;
238+
struct string_list_sort_ctx *sort_ctx = ctx;
230239
const struct string_list_item *one = a;
231240
const struct string_list_item *two = b;
232-
return cmp(one->string, two->string);
241+
return sort_ctx->cmp(one->string, two->string);
233242
}
234243

235244
void string_list_sort(struct string_list *list)
236245
{
237-
QSORT_S(list->items, list->nr, cmp_items,
238-
list->cmp ? list->cmp : strcmp);
246+
struct string_list_sort_ctx sort_ctx = {list->cmp ? list->cmp : strcmp};
247+
248+
QSORT_S(list->items, list->nr, cmp_items, &sort_ctx);
239249
}
240250

241251
struct string_list_item *unsorted_string_list_lookup(struct string_list *list,

0 commit comments

Comments
 (0)