public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] lib: Fix bug in int_sqrt() for 64 bit longs
@ 2006-01-06  5:13 Peter Williams
  2006-01-06  6:42 ` Eric Dumazet
  0 siblings, 1 reply; 3+ messages in thread
From: Peter Williams @ 2006-01-06  5:13 UTC (permalink / raw)
  To: Linux Kernel Mailing List

[-- Attachment #1: Type: text/plain, Size: 546 bytes --]

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.

Signed-off-by: Peter Williams <pwil3058@bigpond.com.au>

-- 
Peter Williams                                   pwil3058@bigpond.net.au

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

[-- Attachment #2: fix-int_sqrt_bug --]
[-- Type: text/plain, Size: 411 bytes --]

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
@@ -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;
 

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] lib: Fix bug in int_sqrt() for 64 bit longs
  2006-01-06  5:13 [PATCH] lib: Fix bug in int_sqrt() for 64 bit longs Peter Williams
@ 2006-01-06  6:42 ` Eric Dumazet
  2006-01-06 23:21   ` Peter Williams
  0 siblings, 1 reply; 3+ messages in thread
From: Eric Dumazet @ 2006-01-06  6:42 UTC (permalink / raw)
  To: Peter Williams; +Cc: Linux Kernel Mailing List

Peter Williams a écrit :
> 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.
> 
> Signed-off-by: Peter Williams <pwil3058@bigpond.com.au>
> 
> 
> ------------------------------------------------------------------------
> 
> 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
> @@ -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

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] lib: Fix bug in int_sqrt() for 64 bit longs
  2006-01-06  6:42 ` Eric Dumazet
@ 2006-01-06 23:21   ` Peter Williams
  0 siblings, 0 replies; 3+ messages in thread
From: Peter Williams @ 2006-01-06 23:21 UTC (permalink / raw)
  To: Eric Dumazet; +Cc: Linux Kernel Mailing List

Eric Dumazet wrote:
> Peter Williams a écrit :
> 
>> 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.
>>
>> Signed-off-by: Peter Williams <pwil3058@bigpond.com.au>
>>
>>
>> ------------------------------------------------------------------------
>>
>> 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
>> @@ -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);

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
-- 
Peter Williams                                   pwil3058@bigpond.net.au

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

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2006-01-06 23:21 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2006-01-06  5:13 [PATCH] lib: Fix bug in int_sqrt() for 64 bit longs Peter Williams
2006-01-06  6:42 ` Eric Dumazet
2006-01-06 23:21   ` Peter Williams

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox