Skip to contentSkip to conversation

lib: Fix bug in int_sqrt() for 64 bit longs

PATCH In review Peter Williams 2 participants 3 messages Jan 6, 2006 Library code

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 lib: Fix bug in int_sqrt() for 64 bit longs

The implementation of int_sqrt() assumes that longs have 32 bits. On systems that have 64 bit longs this will result in gross errors when the argument to the function is greater than 2^32 - 1 on such systems. I doubt whether any such use is currently made of int_sqrt() but the attached patch fixes the problem anyway.

Signature · 4 lines
Peter Williams                                   [email protected]

"Learning, n. The kind of ignorance distinguishing the studious."
 -- Ambrose Bierce
lib/int_sqrt.c +1 -1
Index: GIT-warnings/lib/int_sqrt.c
===================================================================
@@ -15,7 +15,7 @@ unsigned long int_sqrt(unsigned long x)
 	op = x;
 	res = 0;
 
-	one = 1 << 30;
+	one = 1 << (BITS_PER_LONG - 2);
 	while (one > op)
 		one >>= 2;
 
#1 lore raw
Quoted 12 lines · Peter Williams

The implementation of int_sqrt() assumes that longs have 32 bits. On systems that have 64 bit longs this will result in gross errors when the argument to the function is greater than 2^32 - 1 on such systems. I doubt whether any such use is currently made of int_sqrt() but the attached patch fixes the problem anyway.

------------------------------------------------------------------------

Index: GIT-warnings/lib/int_sqrt.c
===================================================================

lib/int_sqrt.c unsigned long int_sqrt(unsigned long x) · line 21

Quoted 8 lines
lib/int_sqrt.c +1 -1
@@ -15,7 +15,7 @@ unsigned long int_sqrt(unsigned long x)
 	op = x;
 	res = 0;
 
-	one = 1 << 30;
+	one = 1 << (BITS_PER_LONG - 2);
 	while (one > op)
 		one >>= 2;
 

Are you sure it works ?

I would have writen :

one = 1L << (BITS_PER_LONG - 2);

Eric

#2 lore raw
Quoted 27 lines · Eric Dumazet
Quoted 13 lines · Peter Williams

The implementation of int_sqrt() assumes that longs have 32 bits. On systems that have 64 bit longs this will result in gross errors when the argument to the function is greater than 2^32 - 1 on such systems. I doubt whether any such use is currently made of int_sqrt() but the attached patch fixes the problem anyway.

------------------------------------------------------------------------

Index: GIT-warnings/lib/int_sqrt.c
===================================================================
--- GIT-warnings.orig/lib/int_sqrt.c 2005-10-25 13:55:22.000000000
+1000
+++ GIT-warnings/lib/int_sqrt.c 2006-01-06 14:29:19.000000000 +1100

lib/int_sqrt.c unsigned long int_sqrt(unsigned long x) · line 21

Quoted 8 lines
lib/int_sqrt.c +1 -1
@@ -15,7 +15,7 @@ unsigned long int_sqrt(unsigned long x)
     op = x;
     res = 0;
 
-    one = 1 << 30;
+    one = 1 << (BITS_PER_LONG - 2);
     while (one > op)
         one >>= 2;
 

Are you sure it works ?

Quoted 3 lines

I would have writen :

one = 1L << (BITS_PER_LONG - 2);

I think you're right as just using 1 would make the expression an integer variable and the compiler would complain about shifting too far unless integers were the same size as longs. (I don't have a 64 bit system to confirm that on which is why it snuck by me. :-()

Thanks
Peter

Signature · 4 lines
Peter Williams                                   [email protected]

"Learning, n. The kind of ignorance distinguishing the studious."
 -- Ambrose Bierce