Browse Source

str: make sure STR_MAX_CHARS gives a constant expression.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
Rusty Russell 9 years ago
parent
commit
73c02ec46e
2 changed files with 9 additions and 1 deletions
  1. 1 1
      ccan/str/str.h
  2. 8 0
      ccan/str/test/compile_ok-STR_MAX_CHARS-static.c

+ 1 - 1
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

+ 8 - 0
ccan/str/test/compile_ok-STR_MAX_CHARS-static.c

@@ -0,0 +1,8 @@
+#include <ccan/str/str.h>
+
+int main(void)
+{
+	static char str[STR_MAX_CHARS(int)];
+
+	return str[0] ? 0 : 1;
+}