summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorThomas Haller <thaller@redhat.com>2017-12-13 08:17:07 +0100
committerThomas Haller <thaller@redhat.com>2017-12-13 11:12:07 +0100
commit6febe75da76517a69f073fb50abffc5c2c7d58cd (patch)
tree1e06f27bcf450296a2353dc5dab6975c81d333b2
parentnetworkd: don't try to configure IPv6 proxy NDP if IPv6 is not available (#7613) (diff)
downloadsystemd-6febe75da76517a69f073fb50abffc5c2c7d58cd.tar.xz
systemd-6febe75da76517a69f073fb50abffc5c2c7d58cd.zip
basic/macros: add STRLEN() to get length of string literal as constant expression
While the compiler likely optimizes strlen(x) for string literals, it is not a constant expression. Hence, char buffer[strlen("OPTION_000") + 1]; declares a variable-length array. STRLEN() can be used instead when a constant espression is needed. It's not entirely identical to strlen(), as STRLEN("a\0") counts 2. Also, it only works with string literals and the macro enforces that the argument is a literal.
-rw-r--r--src/basic/macro.h8
1 files changed, 8 insertions, 0 deletions
diff --git a/src/basic/macro.h b/src/basic/macro.h
index 78679083e8..02d22de833 100644
--- a/src/basic/macro.h
+++ b/src/basic/macro.h
@@ -144,6 +144,14 @@ static inline unsigned long ALIGN_POWER2(unsigned long u) {
!__builtin_types_compatible_p(typeof(x), typeof(&*(x))), \
sizeof(x)/sizeof((x)[0]), \
(void)0))
+
+/*
+ * STRLEN - return the length of a string literal, minus the trailing NUL byte.
+ * Contrary to strlen(), this is a constant expression.
+ * @x: a string literal.
+ */
+#define STRLEN(x) (sizeof(""x"") - 1)
+
/*
* container_of - cast a member of a structure out to the containing structure
* @ptr: the pointer to the member.