From b52d3e2ad36a82103b970bce3676deb0bb33257a Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Mon, 4 Feb 2019 09:51:34 +0100 Subject: [PATCH] shared: add NM_CONST_MAX() macro There is: 1) glib's MAX() macro, which evaluates arguments multiple times, but yields a constant expression, if the arguments are constant. 2) NM's NM_MAX() macro, which evaluates arguments exactly once, but never yields a constant expression. 3) systemd's MAX() which is like NM_MAX(). Now, it's sensible to use char buf[MAX (A_CONSTANT, ANOTHER_CONSTANT)]; and this works with glib's variant (1). However, when we include systemd headers, 1) gets redefined to 3), and above no longer works. That is because we we don't allow VLA and systemd's macro gives not a constant expression. Add NM_CONST_MAX() macro which is like systemd's CONST_MAX(). It can only operate on constant arguments. --- shared/nm-utils/nm-macros-internal.h | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/shared/nm-utils/nm-macros-internal.h b/shared/nm-utils/nm-macros-internal.h index 134d08ef79..6907ef1518 100644 --- a/shared/nm-utils/nm-macros-internal.h +++ b/shared/nm-utils/nm-macros-internal.h @@ -1371,6 +1371,14 @@ nm_strcmp_p (gconstpointer a, gconstpointer b) : _b); \ }) +/* evaluates to (void) if _A or _B are not constant or of different types */ +#define NM_CONST_MAX(_A, _B) \ + (__builtin_choose_expr (( __builtin_constant_p (_A) \ + && __builtin_constant_p (_B) \ + && __builtin_types_compatible_p (typeof (_A), typeof (_B))), \ + ((_A) > (_B)) ? (_A) : (_B), \ + ((void) 0))) + /*****************************************************************************/ static inline guint