From 73c02ec46e9c7e175a61f25522c191cf7e04feb6 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Sat, 7 Jan 2017 14:49:53 +1030 Subject: [PATCH] str: make sure STR_MAX_CHARS gives a constant expression. Signed-off-by: Rusty Russell --- ccan/str/str.h | 2 +- ccan/str/test/compile_ok-STR_MAX_CHARS-static.c | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) create mode 100644 ccan/str/test/compile_ok-STR_MAX_CHARS-static.c diff --git a/ccan/str/str.h b/ccan/str/str.h index 85491bc7..d919b84d 100644 --- a/ccan/str/str.h +++ b/ccan/str/str.h @@ -94,7 +94,7 @@ size_t strcount(const char *haystack, const char *needle); #if HAVE_TYPEOF /* Only a simple type can have 0 assigned, so test that. */ #define STR_MAX_CHARS_TCHECK_(type_or_expr) \ - ({ typeof(type_or_expr) x = 0; (void)x; 0; }) + (sizeof(({ typeof(type_or_expr) x = 0; x; }))*0) #else #define STR_MAX_CHARS_TCHECK_(type_or_expr) 0 #endif diff --git a/ccan/str/test/compile_ok-STR_MAX_CHARS-static.c b/ccan/str/test/compile_ok-STR_MAX_CHARS-static.c new file mode 100644 index 00000000..bc6aff7a --- /dev/null +++ b/ccan/str/test/compile_ok-STR_MAX_CHARS-static.c @@ -0,0 +1,8 @@ +#include + +int main(void) +{ + static char str[STR_MAX_CHARS(int)]; + + return str[0] ? 0 : 1; +} -- 2.39.2