https://gcc.gnu.org/bugzilla/show_bug.cgi?id=104884
Bug ID: 104884 Summary: functions miss their 'ret' instruction (and fall through) in certain cases with '-O3' under x86-84 Product: gcc Version: 11.2.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c Assignee: unassigned at gcc dot gnu.org Reporter: zamfofex at twdb dot moe Target Milestone: --- Created attachment 52613 --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=52613&action=edit code in C function is wrongly executed It seems as if certain loops that rely on undefined behavior cause generated functions to be allowed to fall through (i.e. they don’t include an appropriate ‘ret’ instruction as necessary) with ‘-O3’, at least under x86‐64. Even though they do make use of undefined behavior, letting the code execution fall through to subsequent code in the executable seems potentially really dangerous to me, unless I’m missing something (which is not impossible). In the attached ‘main.c’ file, the code in the function ‘bar’ should never be executed, yet it somehow is. If the loop from the function ‘foo’ is placed within an exported function, the generated function body is empty, and upon calling, the execution falls through and likely causes a segfault.