mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] kernel.h: Verify that arguments to swap() are the same type
@ 2009-07-16  1:34 Joe Perches
  2009-07-16  3:19 ` Amerigo Wang
  2009-07-23 21:47 ` Andrew Morton
  0 siblings, 2 replies; 5+ messages in thread
From: Joe Perches @ 2009-07-16  1:34 UTC (permalink / raw)
  To: LKML

Signed-off-by: Joe Perches <joe@perches.com>

diff --git a/include/linux/kernel.h b/include/linux/kernel.h
index d6320a3..72878a5 100644
--- a/include/linux/kernel.h
+++ b/include/linux/kernel.h
@@ -637,8 +637,13 @@ static inline void ftrace_dump(void) { }
 /*
  * swap - swap value of @a and @b
  */
-#define swap(a, b) \
-	do { typeof(a) __tmp = (a); (a) = (b); (b) = __tmp; } while (0)
+#define swap(a, b)				\
+do {						\
+	typeof(a) __tmp = (a);			\
+	BUILD_BUG_ON(!__same_type(__tmp, (b)));	\
+	(a) = (b);				\
+	(b) = __tmp;				\
+} while (0)
 
 /**
  * container_of - cast a member of a structure out to the containing structure



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

* Re: [PATCH] kernel.h: Verify that arguments to swap() are the same type
  2009-07-16  1:34 [PATCH] kernel.h: Verify that arguments to swap() are the same type Joe Perches
@ 2009-07-16  3:19 ` Amerigo Wang
  2009-07-16  5:09   ` Joe Perches
  2009-07-23 21:47 ` Andrew Morton
  1 sibling, 1 reply; 5+ messages in thread
From: Amerigo Wang @ 2009-07-16  3:19 UTC (permalink / raw)
  To: Joe Perches; +Cc: LKML

On Wed, Jul 15, 2009 at 06:34:21PM -0700, Joe Perches wrote:
>Signed-off-by: Joe Perches <joe@perches.com>
>

Hmm, sounds reasonable.

Does this catch any actual wrong usages of swap()?

Acked-by: WANG Cong <xiyou.wangcong@gmail.com>


>diff --git a/include/linux/kernel.h b/include/linux/kernel.h
>index d6320a3..72878a5 100644
>--- a/include/linux/kernel.h
>+++ b/include/linux/kernel.h
>@@ -637,8 +637,13 @@ static inline void ftrace_dump(void) { }
> /*
>  * swap - swap value of @a and @b
>  */
>-#define swap(a, b) \
>-	do { typeof(a) __tmp = (a); (a) = (b); (b) = __tmp; } while (0)
>+#define swap(a, b)				\
>+do {						\
>+	typeof(a) __tmp = (a);			\
>+	BUILD_BUG_ON(!__same_type(__tmp, (b)));	\
>+	(a) = (b);				\
>+	(b) = __tmp;				\
>+} while (0)
> 
> /**
>  * container_of - cast a member of a structure out to the containing structure
>
>
>--
>To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
>the body of a message to majordomo@vger.kernel.org
>More majordomo info at  http://vger.kernel.org/majordomo-info.html
>Please read the FAQ at  http://www.tux.org/lkml/

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

* Re: [PATCH] kernel.h: Verify that arguments to swap() are the same type
  2009-07-16  3:19 ` Amerigo Wang
@ 2009-07-16  5:09   ` Joe Perches
  0 siblings, 0 replies; 5+ messages in thread
From: Joe Perches @ 2009-07-16  5:09 UTC (permalink / raw)
  To: Amerigo Wang; +Cc: LKML

On Thu, 2009-07-16 at 11:19 +0800, Amerigo Wang wrote:
> On Wed, Jul 15, 2009 at 06:34:21PM -0700, Joe Perches wrote:
> >Signed-off-by: Joe Perches <joe@perches.com>
> Does this catch any actual wrong usages of swap()?

No.


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

* Re: [PATCH] kernel.h: Verify that arguments to swap() are the same type
  2009-07-16  1:34 [PATCH] kernel.h: Verify that arguments to swap() are the same type Joe Perches
  2009-07-16  3:19 ` Amerigo Wang
@ 2009-07-23 21:47 ` Andrew Morton
  2009-07-23 22:25   ` Joe Perches
  1 sibling, 1 reply; 5+ messages in thread
From: Andrew Morton @ 2009-07-23 21:47 UTC (permalink / raw)
  To: Joe Perches; +Cc: linux-kernel

On Wed, 15 Jul 2009 18:34:21 -0700
Joe Perches <joe@perches.com> wrote:

> Signed-off-by: Joe Perches <joe@perches.com>
> 
> diff --git a/include/linux/kernel.h b/include/linux/kernel.h
> index d6320a3..72878a5 100644
> --- a/include/linux/kernel.h
> +++ b/include/linux/kernel.h
> @@ -637,8 +637,13 @@ static inline void ftrace_dump(void) { }
>  /*
>   * swap - swap value of @a and @b
>   */
> -#define swap(a, b) \
> -	do { typeof(a) __tmp = (a); (a) = (b); (b) = __tmp; } while (0)
> +#define swap(a, b)				\
> +do {						\
> +	typeof(a) __tmp = (a);			\
> +	BUILD_BUG_ON(!__same_type(__tmp, (b)));	\
> +	(a) = (b);				\
> +	(b) = __tmp;				\
> +} while (0)
>  

I wonder if we can do

	typecheck(a, typeof(b));


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

* Re: [PATCH] kernel.h: Verify that arguments to swap() are the same type
  2009-07-23 21:47 ` Andrew Morton
@ 2009-07-23 22:25   ` Joe Perches
  0 siblings, 0 replies; 5+ messages in thread
From: Joe Perches @ 2009-07-23 22:25 UTC (permalink / raw)
  To: Andrew Morton; +Cc: linux-kernel

On Thu, 2009-07-23 at 14:47 -0700, Andrew Morton wrote:
> On Wed, 15 Jul 2009 18:34:21 -0700
> Joe Perches <joe@perches.com> wrote:
> 
> > Signed-off-by: Joe Perches <joe@perches.com>
> > 
> > diff --git a/include/linux/kernel.h b/include/linux/kernel.h
> > index d6320a3..72878a5 100644
> > --- a/include/linux/kernel.h
> > +++ b/include/linux/kernel.h
> > @@ -637,8 +637,13 @@ static inline void ftrace_dump(void) { }
> >  /*
> >   * swap - swap value of @a and @b
> >   */
> > -#define swap(a, b) \
> > -	do { typeof(a) __tmp = (a); (a) = (b); (b) = __tmp; } while (0)
> > +#define swap(a, b)				\
> > +do {						\
> > +	typeof(a) __tmp = (a);			\
> > +	BUILD_BUG_ON(!__same_type(__tmp, (b)));	\
> > +	(a) = (b);				\
> > +	(b) = __tmp;				\
> > +} while (0)
> >  
> 
> I wonder if we can do
> 
> 	typecheck(a, typeof(b));d

Yes.  It generates a warning rather than a build bug.
Maybe that's better.

$ cat -n typeck.c
     1	#define typecheck(type, x)			\
     2	  ({	type __dummy;				\
     3	    typeof(x) __dummy2;				\
     4	    (void)(&__dummy == &__dummy2);		\
     5	    1;						\
     6	  })
     7	
     8	#define swap(a, b)				\
     9	  do { typeof(a) __tmp;				\
    10	    typecheck(typeof(a), (b));			\
    11	    __tmp = (a);				\
    12	    (a) = (b);					\
    13	    (b) = __tmp;				\
    14	  } while (0)
    15	
    16	int main(int argc, char** argv)
    17	{
    18	  int a,b;
    19	  long j,k;
    20	  
    21	  swap(a,b);
    22	  swap(j,k);
    23	  swap(a,j);
    24	}
$ gcc typeck.c
typeck.c: In function ‘main’:
typeck.c:23: warning: comparison of distinct pointer types lacks a cast



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

end of thread, other threads:[~2009-07-23 22:25 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-07-16  1:34 [PATCH] kernel.h: Verify that arguments to swap() are the same type Joe Perches
2009-07-16  3:19 ` Amerigo Wang
2009-07-16  5:09   ` Joe Perches
2009-07-23 21:47 ` Andrew Morton
2009-07-23 22:25   ` Joe Perches

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

all inboxes | Powered by JetHome®