Skip to content

Commit ee344a1

Browse files
committed
numbers.h: downgrade MIN() and MAX() to unsafe versions
For compatibility with Zephyr, MIN and MAX macros are downgraded to the basic, compile-time, standard compliant and unsafe implementation. This means the code will now be the same whether it's compiled with Zephyr versus not. Fixes zephyrproject-rtos/zephyr#33567 which means it's now possible to use MIN() or MAX() in either Zephyr's BUILD_ASSERT() or in SOF's equivalent STATIC_ASSERT() or in C11's _Static_assert() This implementation is unsafe because it can evaluate its arguments twice which is obviously not desired when the argument has side-effects: https://wiki.sei.cmu.edu/confluence/display/c/PRE31-C.+Avoid+side+effects+in+arguments+to+unsafe+macros Signed-off-by: Marc Herbert <marc.herbert@intel.com>
1 parent db7475e commit ee344a1

File tree

1 file changed

+5
-11
lines changed

1 file changed

+5
-11
lines changed

src/include/sof/math/numbers.h

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,17 @@
1616
#ifdef MIN
1717
#undef MIN
1818
#endif
19-
20-
#define MIN(a, b) ({ \
21-
typeof(a) __a = (a); \
22-
typeof(b) __b = (b); \
23-
__a > __b ? __b : __a; \
24-
})
19+
/* Unsafe and portable macros for consistency with Zephyr.
20+
* See SEI CERT-C PRE31-C
21+
*/
22+
#define MIN(a, b) ((a) < (b) ? (a) : (b))
2523

2624
/* Zephyr defines this - remove local copy once Zephyr integration complete */
2725
#ifdef MAX
2826
#undef MAX
2927
#endif
28+
#define MAX(a, b) ((a) < (b) ? (b) : (a))
3029

31-
#define MAX(a, b) ({ \
32-
typeof(a) __a = (a); \
33-
typeof(b) __b = (b); \
34-
__a < __b ? __b : __a; \
35-
})
3630
#define ABS(a) ({ \
3731
typeof(a) __a = (a); \
3832
__a < 0 ? -__a : __a; \

0 commit comments

Comments
 (0)