nlsandler / write_a_c_compiler

Test suite to help you write your own C compiler
https://norasandler.com/2017/11/29/Write-a-Compiler.html
MIT License
855 stars 97 forks source link

stage_9/valid/fib.c UB #13

Closed chadbramwell closed 4 years ago

chadbramwell commented 4 years ago

fib.c has undefined behavior (UB) because it does not have a final return value

kalj commented 4 years ago

Why would having no final return value produce UB? It looks completely fine to me. All paths through fib end with a return statement.

chadbramwell commented 4 years ago

Logically, yes it looks correct. I thought the standard said that all potential paths must return a value even if logically it shouldn't be possible. I mentioned this because I got a compiler warning but I can't remember which compiler off the top of my head.

kalj commented 4 years ago

I thought the standard said that all potential paths must return a value...

Technically, even that is not quite true. Nora talks about that in the Part 5 post, and apparently it is not UB to not return anything as long as the caller doesn't use it. But it definitely sounds like a stupid thing to do, and all sensible compilers do give a warning (I tried clang and gcc (actually MSVC does give an error, but who's surprised, it was always the odd one with a loose relationship with the standard...)).

Anyways, in our case the fib function cannot possibly reach its final brace, so we don't have the problem at all. There are an if and an else, both of which return. No?

chadbramwell commented 4 years ago

Fair enough. I'll close it.