[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <Pine.LNX.4.58.0702161553050.2481@be1.lrz>
Date: Fri, 16 Feb 2007 18:40:02 +0100 (CET)
From: Bodo Eggert <7eggert@....de>
To: Sergei Organov <osv@...ad.com>
cc: 7eggert@....de, Linus Torvalds <torvalds@...ux-foundation.org>,
"J.A. MagallÃÃón"
<jamagallon@....com>, Jan Engelhardt <jengelh@...ux01.gwdg.de>,
Jeff Garzik <jeff@...zik.org>,
Linux Kernel Mailing List <linux-kernel@...r.kernel.org>,
Andrew Morton <akpm@...ux-foundation.org>
Subject: Re: somebody dropped a (warning) bomb
On Fri, 16 Feb 2007, Sergei Organov wrote:
> Bodo Eggert <7eggert@....de> writes:
> > Sergei Organov <osv@...ad.com> wrote:
> >> Linus Torvalds <torvalds@...ux-foundation.org> writes:
> > If you don't code for a specific compiler with specific settings, there is
> > no implementation defining the signedness of char, and each part of the code
> > using char* will be wrong unless it handles both cases correctly.
>
> The problem here is that due to historical reasons, there could be code
> out there that abuses "char" for "signed char" (not sure about "unsigned
> char"). Old code and old habits are rather persistent.
There could be code using trigraphs ... and gcc has an option for that.
If this code uses signed chars, using it on unsigned-char-archs is broken
and should be warned about, but the compiler will not warn about this
because this code will not use "signed char" and therefore it's bug-to-bug
syntax compatible, waiting for a semantic breakdown.
I'll say it again: Either the code using unspecified chars is correct, or
it isn't. If it's correct, neither using with signed nor with unsigned
chars is a bug and you should not warn at all, and if it's not correct,
you should always warn. Instead, gcc warns on "code compiles for $arch".
> > Therefore it's either always wrong to call your char* function with char*,
> > unsigned char* _and_ signed char unless you can guarantee not to overflow any
> > of them, or it's always correct to call char* functions with any kind
> > of these.
>
> How are you sure those who wrote foo(char*) agrees with your opinion or
> even understands all the involved issues?
Let's asume we have this piece of buggy code. We compile it on an unsigned
char architecture. No warning. *BOOM*
Let's asume there is correct code, and we use it as designed:
Warning: Wrong arch
Warning: Wrong arch
Warning: Wrong arch
Warning: real issue
Warning: Wrong arch
Warning: Wrong arch
Warning: Wrong arch
Warning: Wrong arch
<scroll off screen/>
Warning: Wrong arch
Warning: Wrong arch
Warning: Wrong arch
Warning: Wrong arch
Warning: Wrong arch
You don't see "real issue". *BOOM*
What can you do about this warning? Let's asume we cast everywhere:
struct foo * p;
printf(strlen(char*)p); *BOOM*
Let's asume we disable this warning:
int f(unsigned short x)
{
if (!x)
return 0;
return (int) x + f(x-1);
}
f(-1); *BOOM*
Therefore unless you program for one arch with one set of compiler flags,
this warning is useless, and I did not see much code explicitely designed
to be non-portable.
Warning on wrong signedness is good, but if you can't enable it on
portable code, it's useless.
--
Funny quotes:
39. Ever wonder about those people who spend $2.00 apiece on those little
bottles of Evian water? Try spelling Evian backwards: NAIVE
-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@...r.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/
Powered by blists - more mailing lists