britown

Creative-Type Impersonator

🌸请别在工作死🌸


I sometimes like working on never-to-be-finished video game projects


Right now I'm making a game called Chronicles.


Wanna make a game? Here is a list of great C++ libraries to use.


I maintain a Letterboxd in much the way that I assume people maintain bonsai trees.


This is Owen:
Owen
And this is Molly:
Molly
Furthermore, this is Max:
Molly

srxl
@srxl

Given the following C++ (not C!) input file:

#include <stdio.h>

int main() {
    while (1);
}

void oopsies() {
    printf("tee hee fucky wucky :3");
}

Compiling this with clang 13 or newer, with -O1 or higher, results in the output:

> ./compiled
tee hee fucky wucky :3

Godbolt reveals the following output:

main:                                   # @main
oopsies():                            # @oopsies()
        push    rax
        mov     edi, offset .L.str
        xor     eax, eax
        call    printf
        pop     rax
        ret
.L.str:
        .asciz  "tee hee fucky wucky :3"

which is uhhhhh Yeah Thats Not Right

what i think is happening:

  • clang sees infinite loop and goes "ah cool, i don't need to insert a ret here since this wont terminate
  • clang also decides to optimize out the infinite loop for Some Fucking Reason
  • as a result, we're left with a completely empty main with no ret

is there like some undefined behaviour Spooky Activity going on here? why is it Do That? and why only for c++ and not c, where it correctly keeps the infinite loop?


natatorialremnants
@natatorialremnants
This page's posts are visible only to users who are logged in.

You must log in to comment.

in reply to @Qyriad's post:

honestly like yeah whatever "UB can do whatever it wants" but this imho just feels like a compiler bug plain and simple. If the loop is getting removed, the ret should not ALSO be removed. That's ridiculous behavior, and surely it was not intended to do that.