These 2 functions: cache.h:896: foreach_alt_odb transport.h:170: refs_from_alternate_cb were unnecessarily coupled; the callback used by: refs_from_alternate_cb was being passed to the higher-level: foreach_alt_odb which necessitated some casting ugliness that has actually been invoking undefined behavior[0]. This commit decouples this relationship, resulting in a simpler (albeit slightly more verbose) usage. As a bonus, the undefined behavior is automatically resolved. References ---------- [0] Variables of type pointer to void were being used to pass around values of type pointer to function; see: [RFC 1/2] Portability: Convert strictly between function pointers Message-ID: <3c6b883f-8860-4da2-b328-d912019a4145-mfwitten@xxxxxxxxx> Signed-off-by: Michael Witten <mfwitten@xxxxxxxxx> --- builtin/fetch-pack.c | 7 ++++++- builtin/receive-pack.c | 7 ++++++- cache.h | 4 ++-- sha1_file.c | 4 ++-- transport.c | 3 +-- transport.h | 4 ++-- 6 files changed, 19 insertions(+), 10 deletions(-) diff --git a/cache.h b/cache.h index 9f06d21..71cfcef 100644 --- a/cache.h +++ b/cache.h @@ -892,8 +892,8 @@ extern struct alternate_object_database { } *alt_odb_list; extern void prepare_alt_odb(void); extern void add_to_alternates_file(const char *reference); -typedef int alt_odb_fn(struct alternate_object_database *, void *); +typedef int (*alt_odb_fn)(struct alternate_object_database *); -extern void foreach_alt_odb(alt_odb_fn, void*); +extern void foreach_alt_odb(alt_odb_fn); struct pack_window { struct pack_window *next; diff --git a/sha1_file.c b/sha1_file.c index df0edba..e55a496 100644 --- a/sha1_file.c +++ b/sha1_file.c @@ -388,13 +388,13 @@ void add_to_alternates_file(const char *reference) link_alt_odb_entries(alt, alt + strlen(alt), '\n', NULL, 0); } -void foreach_alt_odb(alt_odb_fn fn, void *cb) +void foreach_alt_odb(alt_odb_fn fn) { struct alternate_object_database *ent; prepare_alt_odb(); for (ent = alt_odb_list; ent; ent = ent->next) - if (fn(ent, cb)) + if (fn(ent)) return; } diff --git a/transport.h b/transport.h index efb1968..72a692f 100644 --- a/transport.h +++ b/transport.h @@ -166,7 +166,7 @@ int transport_refs_pushed(struct ref *ref); void transport_print_push_status(const char *dest, struct ref *refs, int verbose, int porcelain, int *nonfastforward); -typedef void alternate_ref_fn(const struct ref *, void *); +typedef void (*alternate_ref_fn)(const struct ref *, void *); -extern int refs_from_alternate_cb(struct alternate_object_database *e, void *cb); +extern int refs_from_alternate_cb(struct alternate_object_database *, alternate_ref_fn); #endif diff --git a/transport.c b/transport.c index a02f79a..c61723f 100644 --- a/transport.c +++ b/transport.c @@ -1190,14 +1190,13 @@ literal_copy: return xstrdup(url); } -int refs_from_alternate_cb(struct alternate_object_database *e, void *cb) +int refs_from_alternate_cb(struct alternate_object_database *e, alternate_ref_fn ref_fn) { char *other; size_t len; struct remote *remote; struct transport *transport; const struct ref *extra; - alternate_ref_fn *ref_fn = cb; e->name[-1] = '\0'; other = xstrdup(real_path(e->base)); diff --git a/builtin/fetch-pack.c b/builtin/fetch-pack.c index 85aff02..62ebdb4 100644 --- a/builtin/fetch-pack.c +++ b/builtin/fetch-pack.c @@ -224,9 +224,14 @@ static void insert_one_alternate_ref(const struct ref *ref, void *unused) rev_list_insert_ref(NULL, ref->old_sha1, 0, NULL); } +static int alt_odb_callback(struct alternate_object_database *d) +{ + return refs_from_alternate_cb(d,insert_one_alternate_ref); +} + static void insert_alternate_refs(void) { - foreach_alt_odb(refs_from_alternate_cb, insert_one_alternate_ref); + foreach_alt_odb(alt_odb_callback); } #define INITIAL_FLUSH 16 diff --git a/builtin/receive-pack.c b/builtin/receive-pack.c index 27050e7..8ef6301 100644 --- a/builtin/receive-pack.c +++ b/builtin/receive-pack.c @@ -736,9 +736,14 @@ static void add_one_alternate_ref(const struct ref *ref, void *unused) add_extra_ref(".have", ref->old_sha1, 0); } +static int alt_odb_callback(struct alternate_object_database *d) +{ + return refs_from_alternate_cb(d,add_one_alternate_ref); +} + static void add_alternate_refs(void) { - foreach_alt_odb(refs_from_alternate_cb, add_one_alternate_ref); + foreach_alt_odb(alt_odb_callback); } int cmd_receive_pack(int argc, const char **argv, const char *prefix) -- 1.7.4.18.g68fe8 -- To unsubscribe from this list: send the line "unsubscribe git" in the body of a message to majordomo@xxxxxxxxxxxxxxx More majordomo info at http://vger.kernel.org/majordomo-info.html