1
1
mirror of https://github.com/systemd/systemd-stable.git synced 2025-03-13 12:58:20 +03:00

sort-util: make bsearch_safe() actually typesafe, by returning the right type

(cherry picked from commit 423e2400afbecd0254515209642089ddbf787249)
(cherry picked from commit c8b2999ae3b32bc7eaab71ca3a1ebf660461d57e)
This commit is contained in:
Lennart Poettering 2023-11-01 14:37:05 +01:00 committed by Luca Boccassi
parent f912ef6dc1
commit 2b0492f4a4
3 changed files with 9 additions and 4 deletions

View File

@ -18,7 +18,7 @@ void *xbsearch_r(const void *key, const void *base, size_t nmemb, size_t size,
({ \
const typeof((b)[0]) *_k = k; \
int (*_func_)(const typeof((b)[0])*, const typeof((b)[0])*, typeof(userdata)) = func; \
xbsearch_r((const void*) _k, (b), (n), sizeof((b)[0]), (comparison_userdata_fn_t) _func_, userdata); \
(typeof((b)[0])*) xbsearch_r((const void*) _k, (b), (n), sizeof((b)[0]), (comparison_userdata_fn_t) _func_, userdata); \
})
/**
@ -38,7 +38,7 @@ static inline void* bsearch_safe(const void *key, const void *base,
({ \
const typeof((b)[0]) *_k = k; \
int (*_func_)(const typeof((b)[0])*, const typeof((b)[0])*) = func; \
bsearch_safe((const void*) _k, (b), (n), sizeof((b)[0]), (comparison_fn_t) _func_); \
(typeof((b)[0])*) bsearch_safe((const void*) _k, (b), (n), sizeof((b)[0]), (comparison_fn_t) _func_); \
})
/**

View File

@ -511,7 +511,12 @@ Instance* resource_find_instance(Resource *rr, const char *version) {
.metadata.version = (char*) version,
}, *k = &key;
return typesafe_bsearch(&k, rr->instances, rr->n_instances, instance_cmp);
Instance **found;
found = typesafe_bsearch(&k, rr->instances, rr->n_instances, instance_cmp);
if (!found)
return NULL;
return *found;
}
int resource_resolve_path(

View File

@ -817,7 +817,7 @@ static int feature_to_string_compare_func(const FeatureToString *a, const Featur
}
static void print_feature(Feature feature, const char *prefix) {
FeatureToString *found, in = {
const FeatureToString *found, in = {
.feature = feature,
};