Skip to content

Commit

Permalink
string-list.c: avoid conversion from void * to function pointer
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
bbolli authored and gitster committed Jul 9, 2018
1 parent 9ad3635 commit b6d3f5a
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions string-list.c
Original file line number Diff line number Diff line change
Expand Up @@ -224,18 +224,28 @@ struct string_list_item *string_list_append(struct string_list *list,
list->strdup_strings ? xstrdup(string) : (char *)string);
}

/*
* Encapsulate the compare function pointer because ISO C99 forbids
* casting from void * to a function pointer and vice versa.
*/
struct string_list_sort_ctx
{
compare_strings_fn cmp;
};

static int cmp_items(const void *a, const void *b, void *ctx)
{
compare_strings_fn cmp = ctx;
struct string_list_sort_ctx *sort_ctx = ctx;
const struct string_list_item *one = a;
const struct string_list_item *two = b;
return cmp(one->string, two->string);
return sort_ctx->cmp(one->string, two->string);
}

void string_list_sort(struct string_list *list)
{
QSORT_S(list->items, list->nr, cmp_items,
list->cmp ? list->cmp : strcmp);
struct string_list_sort_ctx sort_ctx = {list->cmp ? list->cmp : strcmp};

QSORT_S(list->items, list->nr, cmp_items, &sort_ctx);
}

struct string_list_item *unsorted_string_list_lookup(struct string_list *list,
Expand Down

0 comments on commit b6d3f5a

Please sign in to comment.