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.
This commit is contained in:
Patrick Pelissier committed 2022-10-01 11:38:11 +02:00
1 parent 95bc7063a0
commit 775ec6f91b
1 file changed
+6 -2
+6 -2
View File
@@ -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