Commit 8e695300 authored by Alexandre Duret-Lutz's avatar Alexandre Duret-Lutz

[buddy] fix an undefined behavior

* src/prime.c (BitIsSet): Do not shift signed
int by 31 places; shift unsigned int instead.
parent fcd67831
...@@ -43,7 +43,7 @@ ...@@ -43,7 +43,7 @@
#define Random(i) ( (rand() % (i)) + 1 ) #define Random(i) ( (rand() % (i)) + 1 )
#define isEven(src) (!((src) & 0x1)) #define isEven(src) (!((src) & 0x1))
#define hasFactor(src,n) ( (((src)!=(n)) && ((src)%(n) == 0)) ) #define hasFactor(src,n) ( (((src)!=(n)) && ((src)%(n) == 0)) )
#define BitIsSet(src,b) ( ((src) & (1<<(b))) != 0 ) #define BitIsSet(src,b) ( ((src) & (1U<<(b))) != 0 )
#define CHECKTIMES 20 #define CHECKTIMES 20
...@@ -115,7 +115,7 @@ static unsigned int u64_mod(UINT64 dividend, unsigned int divisor) ...@@ -115,7 +115,7 @@ static unsigned int u64_mod(UINT64 dividend, unsigned int divisor)
int i; int i;
u64_shl(&dividend, &remainder); u64_shl(&dividend, &remainder);
for (i=0 ; i<64 ; ++i) for (i=0 ; i<64 ; ++i)
{ {
if (remainder >= divisor) if (remainder >= divisor)
...@@ -145,7 +145,7 @@ static unsigned int numberOfBits(unsigned int src) ...@@ -145,7 +145,7 @@ static unsigned int numberOfBits(unsigned int src)
if (src == 0) if (src == 0)
return 0; return 0;
for (b=(sizeof(unsigned int)*8)-1 ; b>0 ; --b) for (b=(sizeof(unsigned int)*8)-1 ; b>0 ; --b)
if (BitIsSet(src,b)) if (BitIsSet(src,b))
return b+1; return b+1;
...@@ -166,10 +166,10 @@ static int isWitness(unsigned int witness, unsigned int src) ...@@ -166,10 +166,10 @@ static int isWitness(unsigned int witness, unsigned int src)
unsigned int x = d; unsigned int x = d;
d = u64_mulmod(d,d,src); d = u64_mulmod(d,d,src);
if (d == 1 && x != 1 && x != src-1) if (d == 1 && x != 1 && x != src-1)
return 1; return 1;
if (BitIsSet(src-1,i)) if (BitIsSet(src-1,i))
d = u64_mulmod(d,witness,src); d = u64_mulmod(d,witness,src);
} }
...@@ -280,12 +280,12 @@ void testMul(unsigned int a, unsigned int b) ...@@ -280,12 +280,12 @@ void testMul(unsigned int a, unsigned int b)
void testMod(unsigned int a, unsigned int b, unsigned int c) void testMod(unsigned int a, unsigned int b, unsigned int c)
{ {
UINT64 x = u64_mul(a,b); UINT64 x = u64_mul(a,b);
long long z1 = (long long)a * (long long)b; long long z1 = (long long)a * (long long)b;
long long z2 = ((long long)x.hi << 32) + (long long)x.lo; long long z2 = ((long long)x.hi << 32) + (long long)x.lo;
unsigned int m1 = z1 % c; unsigned int m1 = z1 % c;
unsigned int m2 = u64_mod(x,c); unsigned int m2 = u64_mod(x,c);
if (z1 != z2) if (z1 != z2)
printf("%d * %d = %lld,%lld\n", a, b, z1, z2); printf("%d * %d = %lld,%lld\n", a, b, z1, z2);
...@@ -311,11 +311,10 @@ int main() ...@@ -311,11 +311,10 @@ int main()
printf("ERROR"); printf("ERROR");
/*printf("\n");*/ /*printf("\n");*/
} }
return 0; return 0;
} }
#endif #endif
/* EOF */ /* EOF */
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment