From mboxrd@z Thu Jan 1 00:00:00 1970 From: Carlo Wood To: egcs-bugs@cygnus.com Cc: pommnitz@darmstadt.gmd.de, egcs@cygnus.com (egcs@cygnus.com) Subject: Re: Further observations regarding alloca on i586-pc-linux-gnu Date: Sat, 22 Aug 1998 11:00:00 -0000 Message-id: <199808221724.TAA14230@jolan.ppro> References: X-SW-Source: 1998-08/msg00757.html | Joerg Pommnitz writes: | | > While the following code dies with a segmentation violation | | > #include | > #include | > class xx { public: char *xy (char *c = alloca (18)) { | > strcpy (c, "Hello World!"); return c; } }; | > int main () { xx x; cout << x.xy () << endl; } | | > Is this a bug? I think yes, but I'm not sure whether case #1 | > is supposed to work. | | IMO, it should work on x86, as it does on sparc and alpha. The | problem is that the stack pointer is moved after pushing default | arguments onto the stack. | | Unfortunately, I don't have the (time required to develop) skills | needed to fix it, so I'll leave this for someone else. | | Anyway, next time you report a problem, please clearly state on which | platform you have encountered it, and which compiler options you have | used. It took me some time to find out the problem would only occur | on x86 without optimization! After a little bit of experimenting, I found what is happening: cout << alloca (20) << '\n'; becomes: ??? pushl $10 # put '\n' on the stack addl $-20,%esp # allocate 20 bytes on the stack movl %esp,%eax pushl %eax # write the result of alloca() to cout pushl $cout .LCFI2: call __ls__7ostreamPCv addl $8,%esp # correct last two pushes. movl %eax,%eax # put return value `cout' on the stack: pushl %eax call __ls__7ostreamc # Try to write '\n' (fails, stack pointer wrong). addl $8,%esp # "correct" wrongly stack for push $10 and last push. Correct would be: addl $-20,%esp # allocate 20 bytes on the stack movl %esp,%eax !!! pushl $10 # put '\n' on the stack pushl %eax # write the result of alloca() to cout ..etc In other words: This is a bug. -- Carlo Wood