Subject: bug#76559: 31.0.50; [-O3 + PGTK] Crash when 'copying as kill'/'killing word'

From: Po Lu <[email-masked]>
Date: Fri, 28 Feb 2025 08:12:32 +0800

Paul Eggert <[email-masked]> writes:

> On 2025-02-27 04:22, Pip Cet wrote:
>> (I'm not convinced there's a GCC bug there, but it doesn't matter:
>> aggressive GCC optimizations currently result in broken Emacs
>> builds).
>
> I don't see a GCC bug there. If a program casts a pointer to a
> different pointer type and dereferences the result, behavior is
> undefined (with exceptions for void * and char * that don't apply
> here). And even if we blacklist gcc -O3 -flto, who's to say some other
> compiler won't do the optimization?
>
> The violation of C rules occurs in keyboard.h, here:
>
>   INLINE void
>   kbd_buffer_store_event_hold (struct input_event *event,
> 			       struct input_event *hold_quit)
>   {
>     kbd_buffer_store_buffered_event ((union buffered_input_event *) event,
> 				     hold_quit);
>   }
>
> That cast is invalid and the resulting code need not work as one might
> expect with a circa 1978 C compiler.

The cast is valid.  It is accessing a union buffered_input_event from
the resultant pointer that is not.

"... an aggregate or union type that includes one of the aforementioned
types among its members (including, recursively, a member of a
subaggregate or contained union), or ..."