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

2020-05-07 Thread pinskia at gcc dot gnu.org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=91031

--- Comment #6 from Andrew Pinski  ---
(In reply to Alexey Makhalov from comment #5)
> (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?

Because it is an optimization to be able to reuse the stack space.  Undefined
code at runtime is just that undefined.

> 
> 2) this sample will allocate anonymous (char *)"test" per function scope
> with any optimization

NO this example does not use compound literals so it does not have an anonymous
variable in play.  In fact in below testcase, "test" is allocated in a static
read only region.
I think you want:
j = (char[]){"test"};

See PR 89113 for an example.

> 
> #include 
> 
> 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?

Because escape analysis is "hard".  This can be found at runtime though with
-fsanitize=address (which enables -fsanitize-address-use-after-scope).

> 
> 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.

It could have been shared with a different anonymous variable.


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

r259641

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

2020-05-07 Thread makhaloff at gmail dot com
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=91031

--- Comment #5 from Alexey Makhalov  ---
(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 

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.

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

2020-05-06 Thread pinskia at gcc dot gnu.org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=91031

Andrew Pinski  changed:

   What|Removed |Added

 CC||makhaloff at gmail dot com

--- Comment #4 from Andrew Pinski  ---
*** Bug 94979 has been marked as a duplicate of this bug. ***

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

2019-06-28 Thread jakub at gcc dot gnu.org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=91031

Jakub Jelinek  changed:

   What|Removed |Added

 CC||jakub at gcc dot gnu.org

--- Comment #3 from Jakub Jelinek  ---
See https://gcc.gnu.org/gcc-9/porting_to.html#complit

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

2019-06-28 Thread david at pgmasters dot net
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=91031

David  changed:

   What|Removed |Added

 CC||david at pgmasters dot net

--- Comment #2 from David  ---
We figured out the issue, but were trying to figure out why it worked on any
version of gcc.  Thanks for the explanation.

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

2019-06-28 Thread pinskia at gcc dot gnu.org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=91031

Andrew Pinski  changed:

   What|Removed |Added

 Status|UNCONFIRMED |RESOLVED
 Resolution|--- |INVALID

--- Comment #1 from Andrew Pinski  ---
The compound literal is consider an anonymous variable in the current scope. 
So in your case you take the address of it and then the scope ends but you
access it afterwards.

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.