This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Bug in gcc 2.7.2.1


I'm seeing a problem with gcc 2.7.2.1. When passing an alloca()d
buffer to a function whose results are passed to another function,
the alloc stack pointer adjustment happens after trailing arguments
have been pushed onto the stack. This opens a hole in the stack
that well-meaning code falls into. The work-around is to do the alloca
outside the function.  I'll download gcc 2.95 tonight and repeat this.

Thanks,
 Phil

---------

#include <stdio.h>
#include <stdlib.h>

int foo(const char *tag, char *p1, int z1, char *p2, int z2);
char *goo(char *);

char *
goo (char *inp)
{
    return inp;
}

int
foo (const char *tag, char *p1, int z1, char *p2, int z2)
{
    printf("%s: %p %x %p %x\n", tag, p1, z1, p2, z2);

    if (p1 == NULL || p2 == NULL)
	printf("%s failed ptr %p %p\n", tag, p1, p2);

    if (z1 != 0 || z2 != 0)
	printf("%s failed zero %x %x\n", tag, z1, z2);
    return 0;
}

int
main (int argc __unused, char **argv __unused)
{
    char b1[10], b2[20];

    foo("static", b1, 0, b2, 0);
    foo("straight", alloca(10), 0, alloca(20), 0);
    foo("goo", goo(alloca(10)), 0, goo(alloca(20)), 0);

    return 0;
}

------

Generates:

static: 0xefbfd664 0 0xefbfd650 0
straight: 0xefbfd630 0 0xefbfd63c 0
goo: 0xefbfd5f0 15f8 0x169f efbfd670
goo failed zero 15f8 efbfd670

The assembly for the last call looks like:

        pushl $0
        leal -32(%ebp),%eax
        pushl %eax
        pushl $0
        leal -12(%ebp),%eax
        pushl %eax
        call _foo
        .stabd 68,0,23
        addl $16,%esp
        addl $-20,%esp
# Opens a hole in the stack
        movl %esp,%eax
        addl $-12,%esp
# Opens another hole in the stack
        movl %esp,%edx
        pushl $0
        pushl %eax
        pushl $0
        pushl %edx
        call _foo

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]