str: make sure STR_MAX_CHARS gives a constant expression.
authorRusty Russell <rusty@rustcorp.com.au>
Sat, 7 Jan 2017 04:19:53 +0000 (14:49 +1030)
committerRusty Russell <rusty@rustcorp.com.au>
Sat, 7 Jan 2017 04:19:53 +0000 (14:49 +1030)
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
ccan/str/str.h
ccan/str/test/compile_ok-STR_MAX_CHARS-static.c [new file with mode: 0644]

index 85491bc7e33e2f2d6d7dd87fe606ec3e26c00d30..d919b84d49be3cf3a7c7435ffd9822fc17632363 100644 (file)
@@ -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 (file)
index 0000000..bc6aff7
--- /dev/null
@@ -0,0 +1,8 @@
+#include <ccan/str/str.h>
+
+int main(void)
+{
+       static char str[STR_MAX_CHARS(int)];
+
+       return str[0] ? 0 : 1;
+}