Closed chadbramwell closed 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
.
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.
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?
Fair enough. I'll close it.
fib.c has undefined behavior (UB) because it does not have a final return value