Welcome to mirror list, hosted at ThFree Co, Russian Federation.

git.kernel.org/pub/scm/git/git.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJeff King <peff@peff.net>2023-08-20 02:55:30 +0300
committerJunio C Hamano <gitster@pobox.com>2023-08-20 07:17:53 +0300
commitbeaa1d952b90523a167a5d3f24e0a8ce56a4afcd (patch)
treebe6d9eb76a827b2d0a129ecb1cf45793b0c5a51a /range-diff.c
parent2bbeddee5d36dfa3fab16ccd0b78dcdcf66c3a99 (diff)
hashmap: use expected signatures for comparison functions
We prefer for callback functions to match the signature with which they'll be called, rather than casting them to the correct type when assigning function pointers. Even though casting often works in the real world, it is a violation of the standard. We did a mass conversion in 939af16eac (hashmap_cmp_fn takes hashmap_entry params, 2019-10-06), but have grown a few new cases since then. Because of the cast, the compiler does not complain. However, as of clang-18, UBSan will catch these at run-time, and the case in range-diff.c triggers when running t3206. After seeing that one, I scanned the results of: git grep '_fn)[^(]' '*.c' | grep -v typedef and found a similar case in compat/terminal.c (which presumably isn't called in the test suite, since it doesn't trigger UBSan). There might be other cases lurking if the cast is done using a typedef that doesn't end in "_fn", but loosening it finds too many false positives. I also looked for: git grep ' = ([a-z_]*) *[a-z]' '*.c' to find assignments that cast, but nothing looked like a function. The resulting code is unfortunately a little longer, but the bonus of using container_of() is that we are no longer restricted to the hashmap_entry being at the start of the struct. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'range-diff.c')
-rw-r--r--range-diff.c11
1 files changed, 7 insertions, 4 deletions
diff --git a/range-diff.c b/range-diff.c
index 2e86063491..ca5493984a 100644
--- a/range-diff.c
+++ b/range-diff.c
@@ -230,16 +230,19 @@ cleanup:
}
static int patch_util_cmp(const void *cmp_data UNUSED,
- const struct patch_util *a,
- const struct patch_util *b,
- const char *keydata)
+ const struct hashmap_entry *ha,
+ const struct hashmap_entry *hb,
+ const void *keydata)
{
+ const struct patch_util
+ *a = container_of(ha, const struct patch_util, e),
+ *b = container_of(hb, const struct patch_util, e);
return strcmp(a->diff, keydata ? keydata : b->diff);
}
static void find_exact_matches(struct string_list *a, struct string_list *b)
{
- struct hashmap map = HASHMAP_INIT((hashmap_cmp_fn)patch_util_cmp, NULL);
+ struct hashmap map = HASHMAP_INIT(patch_util_cmp, NULL);
int i;
/* First, add the patches of a to a hash map */