public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] Make sure the value in abs() does not get truncated if it is greater than 2^32
@ 2009-09-03 21:12 Rolf Eike Beer
  2009-09-10 20:39 ` Andrew Morton
  2009-09-11 22:54 ` Jesper Juhl
  0 siblings, 2 replies; 4+ messages in thread
From: Rolf Eike Beer @ 2009-09-03 21:12 UTC (permalink / raw)
  To: Linux Kernel Mailing List

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

I was just digging a bit around in linux/kernel.h and stumbled over the abs() 
makro. For me it looks as it would return wrong results on 64 bit platforms 
if the input value is greater than 2^32.

Signed-off-by: Rolf Eike Beer <eike-kernel@sf-tec.de>
---
 include/linux/kernel.h |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)

diff --git a/include/linux/kernel.h b/include/linux/kernel.h
index d6320a3..1e6eb66 100644
--- a/include/linux/kernel.h
+++ b/include/linux/kernel.h
@@ -145,7 +145,7 @@ extern int _cond_resched(void);
 #define might_sleep_if(cond) do { if (cond) might_sleep(); } while (0)
 
 #define abs(x) ({				\
-		int __x = (x);			\
+		long __x = (x);			\
 		(__x < 0) ? -__x : __x;		\
 	})
 
-- 
1.6.0.2


[-- Attachment #2: This is a digitally signed message part. --]
[-- Type: application/pgp-signature, Size: 198 bytes --]

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

* Re: [PATCH] Make sure the value in abs() does not get truncated if it is greater than 2^32
  2009-09-03 21:12 [PATCH] Make sure the value in abs() does not get truncated if it is greater than 2^32 Rolf Eike Beer
@ 2009-09-10 20:39 ` Andrew Morton
  2009-09-11  5:28   ` Rolf Eike Beer
  2009-09-11 22:54 ` Jesper Juhl
  1 sibling, 1 reply; 4+ messages in thread
From: Andrew Morton @ 2009-09-10 20:39 UTC (permalink / raw)
  To: Rolf Eike Beer; +Cc: linux-kernel

On Thu, 3 Sep 2009 23:12:01 +0200
Rolf Eike Beer <eike-kernel@sf-tec.de> wrote:

> I was just digging a bit around in linux/kernel.h and stumbled over the abs() 
> makro. For me it looks as it would return wrong results on 64 bit platforms 
> if the input value is greater than 2^32.
> 

Yes, it will truncate.

> 
> diff --git a/include/linux/kernel.h b/include/linux/kernel.h
> index d6320a3..1e6eb66 100644
> --- a/include/linux/kernel.h
> +++ b/include/linux/kernel.h
> @@ -145,7 +145,7 @@ extern int _cond_resched(void);
>  #define might_sleep_if(cond) do { if (cond) might_sleep(); } while (0)
>  
>  #define abs(x) ({				\
> -		int __x = (x);			\
> +		long __x = (x);			\
>  		(__x < 0) ? -__x : __x;		\
>  	})

I wonder if that ends up producing worse code in the normal case.

We could use typeof to address that but what about

	unsigned foo = -1;
	signed bar = abs(foo);

?

That'll currently return 1 but if we use typeof it'll return
0xffffffffU which gets turned into -1.

hrm.

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

* Re: [PATCH] Make sure the value in abs() does not get truncated if it is greater than 2^32
  2009-09-10 20:39 ` Andrew Morton
@ 2009-09-11  5:28   ` Rolf Eike Beer
  0 siblings, 0 replies; 4+ messages in thread
From: Rolf Eike Beer @ 2009-09-11  5:28 UTC (permalink / raw)
  To: Andrew Morton; +Cc: linux-kernel

[-- Attachment #1: Type: Text/Plain, Size: 1480 bytes --]

Andrew Morton wrote:
> On Thu, 3 Sep 2009 23:12:01 +0200
> 
> Rolf Eike Beer <eike-kernel@sf-tec.de> wrote:
> > I was just digging a bit around in linux/kernel.h and stumbled over the
> > abs() makro. For me it looks as it would return wrong results on 64 bit
> > platforms if the input value is greater than 2^32.
> 
> Yes, it will truncate.
> 
> > diff --git a/include/linux/kernel.h b/include/linux/kernel.h
> > index d6320a3..1e6eb66 100644
> > --- a/include/linux/kernel.h
> > +++ b/include/linux/kernel.h
> > @@ -145,7 +145,7 @@ extern int _cond_resched(void);
> >  #define might_sleep_if(cond) do { if (cond) might_sleep(); } while (0)
> >
> >  #define abs(x) ({				\
> > -		int __x = (x);			\
> > +		long __x = (x);			\
> >  		(__x < 0) ? -__x : __x;		\
> >  	})
> 
> I wonder if that ends up producing worse code in the normal case.
> 
> We could use typeof to address that but what about
> 
> 	unsigned foo = -1;
> 	signed bar = abs(foo);
> 
> ?
> 
> That'll currently return 1 but if we use typeof it'll return
> 0xffffffffU which gets turned into -1.

I have not tested this, but what might happen with:

unsigned long a = 7;
long b = 9;
return abs(a - b);

I thought about using typeof but that's too much compiler magic for me to get 
right. If you can, go for it.

We could also try to check sizeof(__x) and then use either int or long to get 
the same code for the common code but behave right for big values.

Eike

[-- Attachment #2: This is a digitally signed message part. --]
[-- Type: application/pgp-signature, Size: 198 bytes --]

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

* Re: [PATCH] Make sure the value in abs() does not get truncated if it is greater than 2^32
  2009-09-03 21:12 [PATCH] Make sure the value in abs() does not get truncated if it is greater than 2^32 Rolf Eike Beer
  2009-09-10 20:39 ` Andrew Morton
@ 2009-09-11 22:54 ` Jesper Juhl
  1 sibling, 0 replies; 4+ messages in thread
From: Jesper Juhl @ 2009-09-11 22:54 UTC (permalink / raw)
  To: Rolf Eike Beer; +Cc: Linux Kernel Mailing List

On Thu, 3 Sep 2009, Rolf Eike Beer wrote:

> I was just digging a bit around in linux/kernel.h and stumbled over the abs() 
> makro. For me it looks as it would return wrong results on 64 bit platforms 
> if the input value is greater than 2^32.
> 

I think you are right.


> Signed-off-by: Rolf Eike Beer <eike-kernel@sf-tec.de>
> ---
>  include/linux/kernel.h |    2 +-
>  1 files changed, 1 insertions(+), 1 deletions(-)
> 
> diff --git a/include/linux/kernel.h b/include/linux/kernel.h
> index d6320a3..1e6eb66 100644
> --- a/include/linux/kernel.h
> +++ b/include/linux/kernel.h
> @@ -145,7 +145,7 @@ extern int _cond_resched(void);
>  #define might_sleep_if(cond) do { if (cond) might_sleep(); } while (0)
>  
>  #define abs(x) ({				\
> -		int __x = (x);			\
> +		long __x = (x);			\
>  		(__x < 0) ? -__x : __x;		\
>  	})
>  
> 

-- 
Jesper Juhl <jj@chaosbits.net>             http://www.chaosbits.net/
Plain text mails only, please      http://www.expita.com/nomime.html
Don't top-post  http://www.catb.org/~esr/jargon/html/T/top-post.html


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

end of thread, other threads:[~2009-09-11 22:54 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2009-09-03 21:12 [PATCH] Make sure the value in abs() does not get truncated if it is greater than 2^32 Rolf Eike Beer
2009-09-10 20:39 ` Andrew Morton
2009-09-11  5:28   ` Rolf Eike Beer
2009-09-11 22:54 ` Jesper Juhl

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