From 775ec6f91b31d3be87c17b910acef4cd23aaa030 Mon Sep 17 00:00:00 2001 From: Patrick Pelissier Date: Sat, 1 Oct 2022 11:37:54 +0200 Subject: [PATCH] Make M_ASSERT_INDEX more robust in case it is called with a signed integer. The trick was using x-x to represent 0 so that the compiler won't complain with the comparaison x >= 0 when it is called with unsigned number. --- m-core.h | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/m-core.h b/m-core.h index 74a72775..f14fbc7c 100644 --- a/m-core.h +++ b/m-core.h @@ -359,10 +359,14 @@ M_BEGIN_PROTECTED_CODE * The index is supposed to be unsigned. * It is only used to valid user input, not an intermediary calculus. * NOTE: Can be overiden by user if it needs to keep access under control - * even on release mode */ + * even on release mode + * NOTE: (index)-(index) is used to represent 0, but to avoid spurious + * warning by the compiler on "comparaison is always true" for unsigned + * numbers (It is properly optimized in 0). + * */ #ifndef M_ASSERT_INDEX #define M_ASSERT_INDEX(index, max) do { \ - M_ASSERT((index) < (max)); \ + M_ASSERT((index) >= ((index)-(index)) && (index) < (max)); \ } while (0) #endif