Re: [IDEA+RFC] Possible solution for min()/max() war

Peter T. Breuer (ptb@it.uc3m.es)
Fri, 31 Aug 2001 02:57:13 +0200 (MET DST)


"A month of sundays ago Alex Bligh - linux-kernel wrote:"
> I believe your patch is correct & better than the
> 3 arg perversion, (because the diff to 3 args
> can only be worse than the diff to strict
> casting); carry on plugging it.

Can't say that I have an agenda to carry. It just is a fact that
a 3-arg min/max sticks in the craw! And after seeing this discussion
fly past for a week I begin to understand what Linus' aim might be
(and what the technical problem might be, but I don't think that's
as important). And anyone wth access to gcc's info page can see
that gcc extensions are powerful enough to do the extra checking
that Linus wants ... even without working out the details.

Out of the same mild interest, I've counted the following defn's of min
in the kernel headers (2.4.8):

include/linux/amigaffs.h:#define MIN(a, b) ({ \
include/linux/cyclomx.h:#define min(a,b) (((a)<(b))?(a):(b))
include/linux/cycx_drv.h:#define MIN(a,b) ((a) < (b) ? (a) : (b))
include/linux/efs_fs.h:#define MIN(a, b) (((a) < (b)) ? (a) : (b))
include/linux/if_tun.h:#define MIN(a,b) ( (a)<(b) ? (a):(b) )
include/linux/isdn.h:#define MIN(a,b) ((a<b)?a:b)
include/linux/isicom.h:#define MIN(a, b) ( (a) < (b) ? (a) : (b) )
include/linux/lvm.h:#define min(a,b) (((a)<(b))?(a):(b))
include/linux/wanpipe.h:#define min(a,b) (((a)<(b))?(a):(b))

so apparently there wasn't a central definition before? Incidentally,
most of these are vulnerable to double evaluation.

I've modified these (used the macro with asm(."err");) and recompiled ...

..no messages from bzImage.

Groove. Make modules hits paydirt:

gcc -D__KERNEL__ -I/usr/local/src/linux-2.4.8/include -Wall
-Wstrict-prototypes -Wno-trigraphs -O2 -fno-strict-aliasing
-fno-common -pipe -mpreferred-stack-boundary=2 -march=i686 -DMODULE
-c -o tun.o tun.c
{standard input}: Assembler messages:
{standard input}:633: Error: Unknown pseudo-op:
`.error_with_min_or_max'
make[2]: *** [tun.o] Error 1

That's line 270:

int len = count ...
...
len = MIN(skb->len, len);
copy_to_user(ptr, skb->data, len);

Well, I assume skb->len is unsigned. But len can be negative at times
.. it causes an error return. But this assignment is in the non-error
path where len >= 0, so it is safe to do unsigned comparisons
and replace this with

if (len > skb->len) // len >= 0 guaranteed
len = skb->len;

There are other errors. I'll leave them to you ...

Peter
-
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/