Commit 4a084a9e authored by Rusty Russell's avatar Rusty Russell

cast: fix cast of void * when we don't have GCC features.

I thought using sizeof() comparison to compare the types was clever,
but it doesn't work on void pointers, as sizeof(void) is illegal.
parent ef54ea44
......@@ -122,11 +122,8 @@
#else
#define cast_sign_compatible(type, expr) \
(sizeof(*(type)0) == 1 && sizeof(*(expr)) == 1)
#define cast_const_compat1(expr, type) \
(sizeof(*(expr)) == sizeof(*(type)0))
#define cast_const_compat2(expr, type) \
(sizeof(**(expr)) == sizeof(**(type)0))
#define cast_const_compat3(expr, type) \
(sizeof(***(expr)) == sizeof(***(type)0))
#define cast_const_compat1(expr, type) (1)
#define cast_const_compat2(expr, type) (1)
#define cast_const_compat3(expr, type) (1)
#endif
#endif /* CCAN_CAST_H */
#include <ccan/cast/cast.h>
#include <stdlib.h>
/* Note: this *isn't* sizeof(char) on all platforms. */
struct char_struct {
char c;
};
int main(int argc, char *argv[])
{
char *uc;
const
#ifdef FAIL
struct char_struct
#else
char
#endif
*p = NULL;
uc = cast_const(char *, p);
return 0;
}
#ifdef FAIL
#if !HAVE_TYPEOF||!HAVE_BUILTIN_CHOOSE_EXPR||!HAVE_BUILTIN_TYPES_COMPATIBLE_P
#error "Unfortunately we don't fail if cast_const can only use size"
#endif
#endif
#include <ccan/cast/cast.h>
#include <stdlib.h>
/* Note: this *isn't* sizeof(char) on all platforms. */
struct char_struct {
char c;
};
int main(int argc, char *argv[])
{
char *uc;
const
const
#ifdef FAIL
int
struct char_struct
#else
char
#endif
......@@ -15,3 +20,9 @@ int main(int argc, char *argv[])
uc = cast_const(char *, p);
return 0;
}
#ifdef FAIL
#if !HAVE_TYPEOF||!HAVE_BUILTIN_CHOOSE_EXPR||!HAVE_BUILTIN_TYPES_COMPATIBLE_P
#error "Unfortunately we don't fail if cast_const can only use size"
#endif
#endif
#include <ccan/cast/cast.h>
#include <stdlib.h>
/* Note: this *isn't* sizeof(char) on all platforms. */
struct char_struct {
char c;
};
int main(int argc, char *argv[])
{
char **uc;
const
#ifdef FAIL
struct char_struct
#else
char
#endif
**p = NULL;
uc = cast_const2(char **, p);
return 0;
}
#ifdef FAIL
#if !HAVE_TYPEOF||!HAVE_BUILTIN_CHOOSE_EXPR||!HAVE_BUILTIN_TYPES_COMPATIBLE_P
#error "Unfortunately we don't fail if cast_const can only use size"
#endif
#endif
#include <ccan/cast/cast.h>
#include <stdlib.h>
/* Note: this *isn't* sizeof(char) on all platforms. */
struct char_struct {
char c;
};
int main(int argc, char *argv[])
{
char **uc;
const
const
#ifdef FAIL
int
struct char_struct
#else
char
#endif
......@@ -15,3 +20,9 @@ int main(int argc, char *argv[])
uc = cast_const2(char **, p);
return 0;
}
#ifdef FAIL
#if !HAVE_TYPEOF||!HAVE_BUILTIN_CHOOSE_EXPR||!HAVE_BUILTIN_TYPES_COMPATIBLE_P
#error "Unfortunately we don't fail if cast_const can only use size"
#endif
#endif
#include <ccan/cast/cast.h>
#include <stdlib.h>
/* Note: this *isn't* sizeof(char) on all platforms. */
struct char_struct {
char c;
};
int main(int argc, char *argv[])
{
char ***uc;
const
#ifdef FAIL
struct char_struct
#else
char
#endif
***p = NULL;
uc = cast_const3(char ***, p);
return 0;
}
#ifdef FAIL
#if !HAVE_TYPEOF||!HAVE_BUILTIN_CHOOSE_EXPR||!HAVE_BUILTIN_TYPES_COMPATIBLE_P
#error "Unfortunately we don't fail if cast_const can only use size"
#endif
#endif
#include <ccan/cast/cast.h>
#include <stdlib.h>
/* Note: this *isn't* sizeof(char) on all platforms. */
struct char_struct {
char c;
};
int main(int argc, char *argv[])
{
char ***uc;
const
const
#ifdef FAIL
int
struct char_struct
#else
char
#endif
......@@ -15,3 +20,9 @@ int main(int argc, char *argv[])
uc = cast_const3(char ***, p);
return 0;
}
#ifdef FAIL
#if !HAVE_TYPEOF||!HAVE_BUILTIN_CHOOSE_EXPR||!HAVE_BUILTIN_TYPES_COMPATIBLE_P
#error "Unfortunately we don't fail if cast_const can only use size"
#endif
#endif
......@@ -13,3 +13,9 @@ int main(int argc, char *argv[])
uc = cast_signed(unsigned char *, p);
return 0;
}
#ifdef FAIL
#if !HAVE_TYPEOF||!HAVE_BUILTIN_CHOOSE_EXPR||!HAVE_BUILTIN_TYPES_COMPATIBLE_P
#error "Unfortunately we don't fail if cast_const can only use size"
#endif
#endif
#include <ccan/cast/cast.h>
static void *remove_void(const void *p)
{
return cast_const(void *, p);
}
int main(void)
{
void *p = remove_void("foo");
return !p;
}
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