[Bug c/91031] wrong code generated when using compound literal

makhaloff at gmail dot com gcc-bugzilla@gcc.gnu.org
Thu May 7 06:37:06 GMT 2020


https://gcc.gnu.org/bugzilla/show_bug.cgi?id=91031

--- Comment #5 from Alexey Makhalov <makhaloff at gmail dot com> ---
(In reply to Andrew Pinski from comment #1)
> In previous versions of gcc, the compound literal was put in the function
> level scope rather than in the current scope. Which is why it worked
> previously.  But the code was undefined.  This was added to the changes page
> too.

Hi Andrew, thanks for the update.

There is an inconsistency which is really worried me.

1) The behavior of GCC is different (from user point of view). -O0 allocates
anonymous variable per function, but -01 and higher allocate it per scope?

2) this sample will allocate anonymous (char *)"test" per function scope with
any optimization
--------------------
#include <string.h>

int testme(char *j) {
        if (!j)
                j = (char *)"test";

        return strlen(j) == 4;
}

int main(void) {
        return testme(0) == 0;
}
----------------------
3) Why GCC does not provide any warning/errors in that case?

4) Even if anonymous variable put only in current scope (with optimization), I
still see space for it was allocated in function frame.
I can give you bigger example where array of pointers was allocated on stack in
prologue, but was not initialized.

Can you point to the commit which introduced this change, please?

PS: went to read C standard for anonymous variables.


More information about the Gcc-bugs mailing list