Fix: optional.h macro missing parentheses and guards
authorMathieu Desnoyers <mathieu.desnoyers@efficios.com>
Wed, 18 Dec 2019 15:50:00 +0000 (10:50 -0500)
committerJérémie Galarneau <jeremie.galarneau@efficios.com>
Thu, 19 Dec 2019 22:06:12 +0000 (17:06 -0500)
The are a few coding style issues with optional.h which leads to
unexpected effects when the macros are used in the caller code.

All macro parameters need to be surrounded by () (except when used near
commas, which is the C operator with least precedence).

All macros that emit code need to be surrounded by do { } while (0) so
not to emit extra ; or omit ;, which can cause subtle issues when used
with if/else statements.

Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
Change-Id: Iba6fde7c267f4d8c9ec1a89147045f0bcda3a67a
Signed-off-by: Jérémie Galarneau <jeremie.galarneau@efficios.com>
src/common/optional.h

index aee5fcc5ec911d9d17e97218632767348f827cb8..53215b0315e299695e03ec67dc49890176707cd4 100644 (file)
@@ -73,8 +73,8 @@
  */
 #define LTTNG_OPTIONAL_GET(optional)                   \
         ({                                             \
-               assert(optional.is_set);                \
-               optional.value;                         \
+               assert((optional).is_set);              \
+               (optional).value;                       \
        })
 
 /*
 #define LTTNG_OPTIONAL_INIT { .is_set = 0 }
 
 /* Set the value of an optional field. */
-#define LTTNG_OPTIONAL_SET(field_ptr, val) \
-       (field_ptr)->value = val;          \
-       (field_ptr)->is_set = 1;
+#define LTTNG_OPTIONAL_SET(field_ptr, val)     \
+       do {                                    \
+               (field_ptr)->value = (val);     \
+               (field_ptr)->is_set = 1;        \
+       } while (0)
 
 /* Put an optional field in the "unset" (NULL-ed) state. */
-#define LTTNG_OPTIONAL_UNSET(field_ptr)    \
-       (field_ptr)->is_set = 0;
+#define LTTNG_OPTIONAL_UNSET(field_ptr)                \
+       do {                                    \
+               (field_ptr)->is_set = 0;        \
+       } while (0)
 
 #endif /* LTTNG_OPTIONAL_H */
This page took 0.032902 seconds and 4 git commands to generate.