Email or username:

Password:

Forgot your password?
42 comments
Arsen :gentoo:

@mx00s @dev iirc it might be the case that infinite loops that aren't broken out of aren't UB in C but are in C++. I'd check the spec but I still haven't left the confines of my bed yet.

(EDIT: it is specifically loops w/o side effects that are UB in C++, and I'm still pretty sure while (1); is not UB in C)

The first case is quite simple, the infinite loop with no effects gets optimized into nothing, and the function trailer is not reachable since there (was)is an infinite loop before it, so the code happily continues along and across the function entrypoint for unreachable, acting as if it was just called, which results in the print being executed.

@mx00s @dev iirc it might be the case that infinite loops that aren't broken out of aren't UB in C but are in C++. I'd check the spec but I still haven't left the confines of my bed yet.

(EDIT: it is specifically loops w/o side effects that are UB in C++, and I'm still pretty sure while (1); is not UB in C)

Per Vognsen

@dev The situation is so bad that AFAIK even though LLVM has improved in this area, if you want to compile a language (unlike C++ or C prior to C11) that has well-defined infinite loops on top of LLVM, you pretty much still have to use a dummy asm block to prevent this kind of insanity.

Thomas Dickerson

@dev does the compiler say anything with -Wextra?

Thomas Dickerson

@dev oh I see you have a type error that for some reason the compiler is ignoring. Try -Werror=return-type ?

Thomas Dickerson

@dev Oh - I guess that’s not firing because the *compiler* looks at the control flow and says “you don’t actually reach the closing brace” and then something is happening lower down in LLVM to delete the loop?

Dev

@elfprince13 working theory is the since the ub loop is optimized out, somehow it keeps going beyond main

Brian Campbell

@dev @elfprince13 Yeah, you can take a look in Compiler Explorer and see that that is indeed the case. main is just empty, so it falls through to unreachable.

godbolt.org/z/nPzWzbvb7

Thomas Dickerson

@dev if you don’t write your infinite loops as the Collatz Conjecture that’s on you ;) but it doesn’t seem like the compiler should be allowed to treat the loop as infinite for the purposes of control flow analysis to suppress a warning about missing “return 0”, but then decide the behavior is undefined and can be finite.

Greg Parker

@elfprince13 @dev As of C99 and C++ it is optional to have a return statement in main(). If control reaches the end of main() with no explicit return, it returns 0.

JJ :blobblackcat:

@elfprince13 @dev ah, but is the collatz conjecture good enough when c compilers are smart enough to disprove fermat's last theorem?

blog.regehr.org/archives/140

JJ :blobblackcat:

@elfprince13 @dev oh i see you've seen this already

nevertheless, a shining testament to the field of computer science. those mathematicians can't even disprove fermat's last theorem, smh

Jonathan Yu

@dev If main never calls unreachable, how does that code get executed? Is it something weird like falling through from main without a return, and then running the following code? Hmm

I saw some of the chatter on Twitter from JF but don't know enough about C++ semantics

Charlotte 🦝 θΔ
@jawnsy @dev basically in c++ infinite loops that do nothing are undefined behavior and LLVM assumes undefined behavior cannot occur, and just stops emitting code for main after the prolog.

This includes the function return so it literally just falls through the next code, and that happens to be unreachable()
Nicolas van Kempen

@dev Endless loops with no side effects is undefined behavior, Clang decided to be funny about it.

Dan Cross

@dev GCC's over in a corner pissed off that they're not doing this yet.

Dev

@zombierustpunk oh it is just fish shell and I've replaced cat with bat, which does the code highlighting

Kevin López/KDDLB

@dev I want to know what variant of cat shows files with syntax highlighting and line numbers... :o

Aliciaverse

@dev Undefined behaviour and the missing diagnostics are a absolutely awful.

Go Up