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 <dev+git@drbeat.li> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Beat Bolli committed
Jul 9, 2018 at 21:25 UTC
b6d3f5a960c2b5dfe3d8d84e7f4c26e03d721d10
1 file changed
+14
-4
string-list.c
+14
-4
@@ -224,18 +224,28 @@ struct string_list_item *string_list_append(struct string_list *list,
224
list->strdup_strings ? xstrdup(string) : (char *)string);
225
}
226
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
+
236
static int cmp_items(const void *a, const void *b, void *ctx)
237
{
229
- compare_strings_fn cmp = ctx;
238
+ struct string_list_sort_ctx *sort_ctx = ctx;
239
const struct string_list_item *one = a;
240
const struct string_list_item *two = b;
232
- return cmp(one->string, two->string);
241
+ return sort_ctx->cmp(one->string, two->string);
242
}
243
244
void string_list_sort(struct string_list *list)
245
{
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);
249
}
250
251
struct string_list_item *unsorted_string_list_lookup(struct string_list *list,