]> git.ipfire.org Git - thirdparty/systemd.git/commit
basic/macros: add STRLEN() to get length of string literal as constant expression
authorThomas Haller <thaller@redhat.com>
Wed, 13 Dec 2017 07:17:07 +0000 (08:17 +0100)
committerThomas Haller <thaller@redhat.com>
Wed, 13 Dec 2017 10:12:07 +0000 (11:12 +0100)
commit6febe75da76517a69f073fb50abffc5c2c7d58cd
tree1e06f27bcf450296a2353dc5dab6975c81d333b2
parent18a121f9b462e2241c4a590f0a47f5351cd47e0f
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.
src/basic/macro.h