From: Mathieu Desnoyers Date: Wed, 18 Dec 2019 15:50:00 +0000 (-0500) Subject: Fix: optional.h macro missing parentheses and guards X-Git-Tag: v2.11.1~21 X-Git-Url: https://git.lttng.org./?a=commitdiff_plain;h=3268167bc3429f7eeab975961f02b7de0ecf22fe;p=lttng-tools.git Fix: optional.h macro missing parentheses and guards 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 Change-Id: Iba6fde7c267f4d8c9ec1a89147045f0bcda3a67a Signed-off-by: Jérémie Galarneau --- diff --git a/src/common/optional.h b/src/common/optional.h index aee5fcc5e..53215b031 100644 --- a/src/common/optional.h +++ b/src/common/optional.h @@ -73,8 +73,8 @@ */ #define LTTNG_OPTIONAL_GET(optional) \ ({ \ - assert(optional.is_set); \ - optional.value; \ + assert((optional).is_set); \ + (optional).value; \ }) /* @@ -86,12 +86,16 @@ #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 */