Commit 894dd68a authored by Rusty Russell's avatar Rusty Russell

typesafe_cb: fix promotable types being incorrectly accepted by cast_if_type.

cast_if_type() should not try to degrade the expression using 1?(test):0,
as that promotes bool to int, as well as degrading functions to function
pointers: it should be done by the callers.

In particular, this fixes sparse_bsearch.
parent 5282308f
#include <ccan/typesafe_cb/typesafe_cb.h>
#include <stdbool.h>
static void _set_some_value(void *val)
{
}
#define set_some_value(expr) \
_set_some_value(cast_if_type(void *, (expr), (expr), int))
int main(int argc, char *argv[])
{
#ifdef FAIL
bool x = 0;
#if !HAVE_TYPEOF||!HAVE_BUILTIN_CHOOSE_EXPR||!HAVE_BUILTIN_TYPES_COMPATIBLE_P
#error "Unfortunately we don't fail if cast_if_type is a noop."
#endif
#else
int x = 0;
#endif
set_some_value(x);
return 0;
}
#include <ccan/typesafe_cb/typesafe_cb.h>
#include <stdlib.h>
/* const args in callbacks should be OK. */
static void _register_callback(void (*cb)(void *arg), void *arg)
{
}
#define register_callback(cb, arg) \
_register_callback(typesafe_cb(void, (cb), (arg)), (arg))
static void _register_callback_pre(void (*cb)(int x, void *arg), void *arg)
{
}
#define register_callback_pre(cb, arg) \
_register_callback_pre(typesafe_cb_preargs(void, (cb), (arg), int), (arg))
static void _register_callback_post(void (*cb)(void *arg, int x), void *arg)
{
}
#define register_callback_post(cb, arg) \
_register_callback_post(typesafe_cb_postargs(void, (cb), (arg), int), (arg))
struct undefined;
static void my_callback(const struct undefined *undef)
{
}
static void my_callback_pre(int x, struct undefined *undef)
{
}
static void my_callback_post(struct undefined *undef, int x)
{
}
int main(int argc, char *argv[])
{
struct undefined *handle = NULL;
void (*cb)(const struct undefined *undef) = my_callback;
void (*pre)(int x, struct undefined *undef) = my_callback_pre;
void (*post)(struct undefined *undef, int x) = my_callback_post;
register_callback(cb, handle);
register_callback_pre(pre, handle);
register_callback_post(post, handle);
return 0;
}
......@@ -12,9 +12,8 @@
*
* This macro is used to create functions which allow multiple types.
* The result of this macro is used somewhere that a @desttype type is
* expected: if @expr was of type @oktype, it will be cast to
* @desttype type. As a result, if @expr is any type other than
* @oktype or @desttype, a compiler warning will be issued.
* expected: if @test is exactly of type @oktype, then @expr will be
* cast to @desttype type, otherwise left alone.
*
* This macro can be used in static initializers.
*
......@@ -31,7 +30,7 @@
* _set_some_value(cast_if_type(void *, (e), (e), unsigned long))
*/
#define cast_if_type(desttype, expr, test, oktype) \
__builtin_choose_expr(__builtin_types_compatible_p(typeof(1?(test):0), oktype), \
__builtin_choose_expr(__builtin_types_compatible_p(typeof(test), oktype), \
(desttype)(expr), (expr))
#else
#define cast_if_type(desttype, expr, test, oktype) ((desttype)(expr))
......@@ -105,10 +104,8 @@ __builtin_choose_expr(__builtin_types_compatible_p(typeof(1?(test):0), oktype),
* #define register_callback(fn, arg) \
* _register_callback(typesafe_cb_const(void, (fn), (arg)), (arg))
*/
#define typesafe_cb_const(rtype, fn, arg) \
sizeof((fn)((const void *)0)), \
cast_if_type(rtype (*)(const void *), \
(fn), (fn)(arg), rtype (*)(typeof(arg)))
#define typesafe_cb_const(rtype, fn, arg) \
cast_if_type(rtype (*)(const void *), (fn), (fn)(arg), rtype)
/**
* typesafe_cb_preargs - cast a callback function if it matches the arg
......@@ -126,8 +123,9 @@ __builtin_choose_expr(__builtin_types_compatible_p(typeof(1?(test):0), oktype),
* (arg))
*/
#define typesafe_cb_preargs(rtype, fn, arg, ...) \
cast_if_type(rtype (*)(__VA_ARGS__, void *), (fn), (fn), \
cast_if_type(rtype (*)(__VA_ARGS__, void *), (fn), &*(fn), \
rtype (*)(__VA_ARGS__, typeof(arg)))
/**
* typesafe_cb_postargs - cast a callback function if it matches the arg
* @rtype: the return type of the callback function
......@@ -144,8 +142,9 @@ __builtin_choose_expr(__builtin_types_compatible_p(typeof(1?(test):0), oktype),
* (arg))
*/
#define typesafe_cb_postargs(rtype, fn, arg, ...) \
cast_if_type(rtype (*)(void *, __VA_ARGS__), (fn), (fn), \
cast_if_type(rtype (*)(void *, __VA_ARGS__), (fn), &*(fn), \
rtype (*)(typeof(arg), __VA_ARGS__))
/**
* typesafe_cb_cmp - cast a compare function if it matches the arg
* @rtype: the return type of the callback function
......@@ -168,7 +167,7 @@ __builtin_choose_expr(__builtin_types_compatible_p(typeof(1?(test):0), oktype),
* typesafe_cb_cmp(int, (cmpfn), (base)), (arg))
*/
#define typesafe_cb_cmp(rtype, cmpfn, arg) \
cast_if_type(rtype (*)(const void *, const void *), (cmpfn), \
cast_if_type(rtype (*)(const void *, const void *), (cmpfn), &*(cmpfn), \
rtype (*)(const typeof(*arg)*, const typeof(*arg)*))
#endif /* CCAN_CAST_IF_TYPE_H */
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment