From ae9b49ecb1b31e9116cbb02e96d707a12eae736e Mon Sep 17 00:00:00 2001 From: borysp Date: Thu, 31 Dec 2020 00:38:40 +0100 Subject: [PATCH] Remove parentheses from assert argument evaluation This helps with catching errors like `assert(x = y)`. --- LibOS/shim/src/shim_context-x86_64.c | 2 +- Pal/include/lib/assert.h | 16 +++++++++------- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/LibOS/shim/src/shim_context-x86_64.c b/LibOS/shim/src/shim_context-x86_64.c index 66297a9c..de366e60 100644 --- a/LibOS/shim/src/shim_context-x86_64.c +++ b/LibOS/shim/src/shim_context-x86_64.c @@ -118,7 +118,7 @@ void shim_xstate_restore(const void* xstate_extended) { assert(fpx_sw->magic1 == SHIM_FP_XSTATE_MAGIC1); assert(fpx_sw->extended_size == g_shim_xsave_size + SHIM_FP_XSTATE_MAGIC2_SIZE); assert(fpx_sw->xfeatures == g_shim_xsave_features); - assert(fpx_sw->xstate_size = g_shim_xsave_size); + assert(fpx_sw->xstate_size == g_shim_xsave_size); assert(*((__typeof__(SHIM_FP_XSTATE_MAGIC2)*)bytes_after_xstate) == SHIM_FP_XSTATE_MAGIC2); __UNUSED(bytes_after_xstate); diff --git a/Pal/include/lib/assert.h b/Pal/include/lib/assert.h index 0c7db5da..917a40d9 100644 --- a/Pal/include/lib/assert.h +++ b/Pal/include/lib/assert.h @@ -21,13 +21,15 @@ noreturn void __abort(void); * build system. */ #ifdef DEBUG -#define assert(expr) \ - ({ \ - (!(expr)) ? ({ \ - warn("assert failed " __FILE__ ":%d %s\n", __LINE__, #expr); \ - __abort(); \ - }) \ - : (void)0; \ +/* This `if` is weird intentionally - not to have parentheses around `expr` to catch `assert(x = y)` + * errors. */ +#define assert(expr) \ + ({ \ + if (expr) {} else { \ + warn("assert failed " __FILE__ ":%d %s\n", __LINE__, #expr); \ + __abort(); \ + } \ + (void)0; \ }) #else #define assert(expr) ((void)0)