This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: [PATCH] C undefined behavior fix
- From: "Petr Vandrovec" <VANDROVE at vc dot cvut dot cz>
- To: Paul Koning <pkoning at equallogic dot com>
- Cc: trini at kernel dot crashing dot org, velco at fadata dot bg, linux-kernel at vger dot kernel dot org, gcc at gcc dot gnu dot org, linuxppc-dev at lists dot linuxppc dot org
- Date: Wed, 2 Jan 2002 18:40:24 +0100
- Subject: Re: [PATCH] C undefined behavior fix
- Organization: CC CTU Prague
On 2 Jan 02 at 11:45, Paul Koning wrote:
>
> It might be interesting for the compiler to warn about this coding
> error (since it presumably can detect it). But "fixing" the behavior
> of undefined code seems like a strange thing to do.
It is even worse (gcc 2.95.4 20011223 (Debian prerelease), i386).
Test code:
#include <string.h>
char* dst;
void main(void) {
strcpy(dst, "test"+CONSTANT);
}
# gcc -O2 -S test.c -DCONSTANT=10
test.c: In function `main':
test.c:4: warning: offset outside bounds of constant string
...
and compiler generated correct code (call to strcpy with "test"+10).
But:
# gcc -O2 -S test.c -DCONSTANT=0x80000000
test.c: In function `main':
test.c:4: warning: offset outside bounds of constant string
gcc: Internal compiler error: program cc1 got fatal signal 11
(and for CONSTANT < 5 it of course generated correct code to fill
dst with string contents; and yes, I know that code will sigsegv on
run because of dst is not initialized - but it should die at runtime,
not at compile time).
So we should definitely change RELOC(), or sooner or later gcc will
die on such code :-(
Debian's gcc 3.0.3-1 generates:
0 <= CONSTANT <= 4: fills dst directly with constant
5 <= CONSTANT <= 0x7FFFFFFF: emit warnings + use strcpy()
0x80000000U <= CONSTANT <= 0xFFFFFFFFU: use strcpy() silently
... and it does not die.
Best regards,
Petr Vandrovec
vandrove@vc.cvut.cz