]> rtime.felk.cvut.cz Git - ulut.git/commitdiff
Be more explicit in overflow arithmetic compare to avoid possible undefined behavior.
authorPavel Pisa <pisa@cmp.felk.cvut.cz>
Sat, 21 May 2011 23:50:22 +0000 (01:50 +0200)
committerPavel Pisa <pisa@cmp.felk.cvut.cz>
Sat, 21 May 2011 23:50:22 +0000 (01:50 +0200)
The C standard declares modulo arithmetic behavior only for unsigned
types.

Signed-off-by: Pavel Pisa <pisa@cmp.felk.cvut.cz>
ulut/ul_utdefs.h

index c13cf63439b30351425c79c40bd76ac6312282b7..0b2e4d7ef97ac002b42b67b2aff1583826b69c03 100644 (file)
@@ -41,24 +41,28 @@ extern "C" {
 #ifndef ul_cyclic_gt
 #define ul_cyclic_gt(x,y) \
        ((sizeof(x)>=sizeof(long long))&&(sizeof(y)>=sizeof(long long))? \
-               (long long)((long long)(x)-(long long)(y))>0: \
+               (long long)((unsigned long long)(x)-(unsigned long long)(y))>0: \
         (sizeof(x)>=sizeof(long))&&(sizeof(y)>=sizeof(long))? \
-               (long)((long)(x)-(long)(y))>0: /* x,y casts to suppress warnings only*/ \
-        (sizeof(x)>=sizeof(int))&&(sizeof(y)>=sizeof(int))?(int)((x)-(y))>0: \
-        (sizeof(x)>=sizeof(short))&&(sizeof(y)>=sizeof(short))?(short)((x)-(y))>0: \
-        (signed char)((x)-(y))>0 \
+               (long)((unsigned long)(x)-(unsigned long)(y))>0: \
+        (sizeof(x)>=sizeof(int))&&(sizeof(y)>=sizeof(int))? \
+               (int)((unsigned int)(x)-(unsigned int)(y))>0: \
+        (sizeof(x)>=sizeof(short))&&(sizeof(y)>=sizeof(short))? \
+               (short)((unsigned short)(x)-(unsigned short)(y))>0: \
+        (signed char)((unsigned char)(x)-(unsigned char)(y))>0 \
        )
 #endif /*ul_cyclic_gt*/
 
 #ifndef ul_cyclic_ge
 #define ul_cyclic_ge(x,y) \
        ((sizeof(x)>=sizeof(long long))&&(sizeof(y)>=sizeof(long long))? \
-               (long long)((long long)(x)-(long long)(y))>=0: \
+               (long long)((unsigned long long)(x)-(unsigned long long)(y))>=0: \
         (sizeof(x)>=sizeof(long))&&(sizeof(y)>=sizeof(long))? \
-               (long)((long)(x)-(long)(y))>=0: /* x,y casts to suppress warnings only*/ \
-        (sizeof(x)>=sizeof(int))&&(sizeof(y)>=sizeof(int))?(int)((x)-(y))>=0: \
-        (sizeof(x)>=sizeof(short))&&(sizeof(y)>=sizeof(short))?(short)((x)-(y))>=0: \
-        (signed char)((x)-(y))>=0 \
+               (long)((unsigned long)(x)-(unsigned long)(y))>=0: \
+        (sizeof(x)>=sizeof(int))&&(sizeof(y)>=sizeof(int))? \
+               (int)((unsigned int)(x)-(unsigned int)(y))>=0: \
+        (sizeof(x)>=sizeof(short))&&(sizeof(y)>=sizeof(short))? \
+               (short)((unsigned short)(x)-(unsigned short)(y))>=0: \
+        (signed char)((unsigned char)(x)-(unsigned char)(y))>=0 \
        )
 #endif /*ul_cyclic_ge*/