mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* Type checking MIN with standard interface
@ 2001-09-06 19:28 Morten Welinder
  2001-09-07 22:41 ` Linus Torvalds
  0 siblings, 1 reply; 2+ messages in thread
From: Morten Welinder @ 2001-09-06 19:28 UTC (permalink / raw)
  To: linux-kernel, torvalds


All the silent-cast properties of integer types do not apply to
pointers.  Therefore...

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

cub:~> gcc -O2 -Wall min.c
min.c: In function `main':
min.c:19: warning: comparison of distinct pointer types lacks a cast


cub:~> gcc -O2 -Wall kernel-source.c 2>&1 | \
        grep "comparison of distinct pointer types lacks a cast" | \
        find-and-shoot-programmer

:-)

Morten


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

#include <stdio.h>

#define MIN(x,y)                                \
  ({ const typeof(x) _x = x;                    \
     const typeof(y) _y = y;                    \
                                                \
     (void) (&_x == &_y);                       \
                                                \
     _x < _y ? _x : _y;                         \
  })

int
main ()
{
  /* Good: */
  printf ("%d\n", MIN((signed)1, (signed)1));

  /* Bad: */
  printf ("%d\n", MIN((signed)1, (unsigned)1));

  return 0;
}

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

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

* Re: Type checking MIN with standard interface
  2001-09-06 19:28 Type checking MIN with standard interface Morten Welinder
@ 2001-09-07 22:41 ` Linus Torvalds
  0 siblings, 0 replies; 2+ messages in thread
From: Linus Torvalds @ 2001-09-07 22:41 UTC (permalink / raw)
  To: Morten Welinder; +Cc: linux-kernel


On 6 Sep 2001, Morten Welinder wrote:
>
> All the silent-cast properties of integer types do not apply to
> pointers.  Therefore...

Ok, we have a winner. Nice (and more understandable) warning message,
along with strict type-checking.

I'm happy. Will do this for 2.4.10-pre5.

		Linus


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

end of thread, other threads:[~2001-09-07 22:45 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2001-09-06 19:28 Type checking MIN with standard interface Morten Welinder
2001-09-07 22:41 ` Linus Torvalds

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®