[BUG] Nested variables

Horst von Brand vonbrand@sleipnir.valparaiso.cl
Sun May 24 00:44:00 GMT 1998


Frank DENIS -Jedi/Sector One- <j@4u.net> said:
>   I got some unexpected segmentation faults with the shadow password suite,
> when compiled with Egcs 1.0.3-release, due to a macro.
> 
>   Basically, the problem can be reproduced as follows :
>   
> #include <stdio.h>
> 
> int main(void)
> {
>    const char *yop = "Yop";
>    
>    printf("Yop 1 = [%p]\n", yop);
>      {
> 	const char *yop = yop;	
> 	printf("Yop 2 = [%p]\n", yop);
>      }
>    
>    return 0;
> }

>   With Egcs, yop1 and yop2 got different values. In fact, the nested 'yop'
> is not initialized at all:

Same with egcs-19980517 (gcc -O2, no optimization works):

	.file	"xxx.c"
	.version	"01.01"
gcc2_compiled.:
.section	.rodata
.LC0:
	.string	"Yop"
.LC1:
	.string	"Yop 1 = [%p]\n"
.LC2:
	.string	"Yop 2 = [%p]\n"
.text
	.align 4
.globl main
	.type	 main,@function
main:
	pushl %ebp
	movl %esp,%ebp
	pushl %ebx
	pushl $.LC0
	pushl $.LC1
	call printf
	pushl %ebx	! <-- never initialized
	pushl $.LC2
	call printf
	movl -4(%ebp),%ebx
	xorl %eax,%eax
	movl %ebp,%esp
	popl %ebp
	ret
.Lfe1:
	.size	 main,.Lfe1-main
	.ident	"GCC: (GNU) egcs-2.91.29 19980517 (gcc2 ss-980502 experimental)"

>   I don't know what the standards says about this, but other compilers
> initialize the nested "yop" with the value of the surrounding "yop".

They might just use the same value (.LC0) in this particular case, or
initialize the inner variable with the value of the outer one.

>   If the behavior or the result is undefined, maybe Egcs should issue a
> clearer warning than "Yop might be uninitialized". Or maybe even issue an
> error, because this has nothing to do with what the programmer may
> expect.

If it gives that warning, the compiler is clearly in error.  egcs-19980517
is silent, BTW.
-- 
Horst von Brand                             vonbrand@sleipnir.valparaiso.cl
Casilla 9G, Viña del Mar, Chile                               +56 32 672616




More information about the Gcc-bugs mailing list