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

Jakub Jelinek <jakub at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |jakub at gcc dot gnu.org

--- Comment #7 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
(In reply to Sam James from comment #4)
> I'm not sure if the original is valid.
> 
> cvise got it down to:
> ```
> struct
> {
>   int d;
>   short e;
> } i;
> 
> int b;
> int *h = &b;
> 
> int
> main ()
> {
>   short f = 1;
>   short *g = &i.e;
> 
> a:
>   if (*g = 0 & ++f, *h)
>     ;
>   else
>     {
>       int c = 0;
>       if (f)
>         goto a;
>       h = &c;
>     }
> 
>   return 0;
> }
> ```
> 
> but the pointer to c is dangling. If I go back to the original, it has
> tonnes of -Wdangling-pointer.

So what?  It is not UB to assign address of a local variable to a pointer, even
to store it into some global variable.  It is UB to dereference that pointer
after the var leaves the scope (or maybe in strict reading even compare it
etc.).  While I'm not sure about the original testcase, I'm certainly not
seeing it in the reduced one.
++f shouldn't invoke UB if short is narrower than int, so this should keep
looping without h = &c; until f finally becomes 0 and then assign h = &c; and
return.

Reply via email to