38 ms·
C, C++, x86/x64 assembly: The case of forgotten return
- lalaithion 8y agoWhat does clang do?
- petermcneeley 8y agoclang "<source>:19:1: warning: control reaches end of non-void function [-Wreturn-type]" Given that no return is undefined behavior (6.6.3 c++ standard) this probably should just be treated as an error. (it would be nice if a manifested return was value/default init but it is not) The reason why these are not errors in the compiler is for backwards compatibility.
- chartreusek 8y agoSince the article is talking about C/C++ In C reaching the end of a non-void function is not undefined behaviour though. It's equivalent to ending with a return; However attempting to use the return value of that function is undefined behaviour. It'd be fine to have that warning be an error when compiling for C++, but not for C. C89 3.6.6.4 (http://port70.net/~nsz/c/c89/c89-draft.html#3.6.6.4 http://port70.net/~nsz/c/c89/c89-draft.html#3.6.6.4): "If a return statement without an expression is executed, and the value of the function call is used by the caller, the behavior is undefined. Reaching the } that terminates a function is equivalent to executing a return statement without an expression."
- maxlybbert 8y agoI’m sure that rule comes from the time when “void” didn’t exist, so functions were defined to return int, but nobody ever bothered to actually return anything.
- chartreusek 8y agoIt's definitely there to retain some backwards compatibility with existing K&R C programs. Though it does still somewhat exist in the C11 standards. From 6.9.1 - "If the } that terminates a function is reached, and the value of the function call is used by the caller, the behavior is undefined" Though 6.8.6.4 also says "A return statement without an expression shall only appear in a function whose return type is void" So it seems like a non-void function hitting the end of the block without a return statement is allowed (provided the value isn't used). But having a "return;" in that function would not be in C11.
- shaklee3 8y agoClassic example is printf. It returns an error, but nobody checks it.
- wmu 8y ago"-Wall -Wextra -Werror" are you allies :)
- twodayslate 8y ago-Weverything if using clang
- spc476 8y agoIt's too noisy just by itself. First time I tried it, it complained about padding added to structures. When I packed them, it complained about packed structures.
- MaulingMonkey 8y agoManually pad them, and it'll complain about unused fields ;) I've actually had some pieces of code where it was super useful to enable that padding warning as an error. Some complicated C++ types that needed to have identical memory layouts across compilers yet often weren't because of some really nasty shenanigans... -Weverything is for people who really want to explicitly opt out of every warning they don't like.
- ndesaulniers 8y agoNot recommended. Particularly there are warnings for C++ now that are "mutually unsatisfy-able."
- 8y ago
- zygotic12 8y ago'If you don't eat yer meat, you can't have any pudding.' No one mentions the change in attitude towards programming. If it wasn't correct YOU were not good enough.
- akkartik 8y agoMy mind is blown that g++ (7.3.0) doesn't warn on this.
- saagarjha 8y agoIt seems like GCC 8 warns me by default, so someone must have realized that this was stupid: $ pbpaste | g++-8 -x c++ - <stdin>: In function 'color* create_color(int, int, int)': <stdin>:19:1: warning: no return statement in function returning non-void [-Wreturn-type]
- gumby 8y agoThat is the correct behavior for c. But you mentioned g++: not sure when this became invalid in c++ but for me g++ -std=17 warns by default (though we compile everything with maximum possible warnings already)
- kazinator 8y agoFrom an ISO C conformance viewpoint, it is neither "correct" nor "incorrect" to fail to generate a diagnostic that is not required by ISO C. In the ISO C++ language, I vaguely seem to recall ("I could have sworn that") that failing to return a value from a function is a diagnosable semantic rule violation. And that it has been for a very long time. So I fully sympathize with the "mind is blown" comment in grandparent. Hey, g++ -pedantic catches some important semicolon trespasses, though: noreturn.cc:19:2: warning: extra ‘;’ [-pedantic] noreturn.cc:25:2: warning: extra ‘;’ [-pedantic] :)
- emilfihlman 8y ago>And this is also yet another demonstration, how C/C++ places return value into EAX/RAX register. No, it's a compiler implementation detail, not a C/++ specification.
- ajross 8y agoIt's specified by the relevant ABI standard, FWIW.
- deleted 8y ago[deleted]
- deleted 8y ago[deleted]
- marssaxman 8y agoIt's a little bit awkward to watch a new generation rediscover things which used to be common knowledge. It's a good thing, though; this sort of thing shouldn't be as common a practice as it once was.
- Iv 8y agoI am intrigued as to why the compiler feels it is allowed to simplify a function returning nothing as a no-op. After all this function allocates memory and stores things in there. This is a side effect that the optimization suppresses.
- marcoperaza 8y agoThe allocation’s address is only ever stored into a local variable. The compiler can therefore prove it’s not used elsewhere (short of e.g. scanning the whole heap for it)
- plorkyeran 8y agoThe compiler assumes that you have given it a valid C program, and optimizes accordingly. A valid C program is not allowed to fall off the end of a non-void function without returning a value if the return value is used. This means that if the optimizer sees a function which will always do this, it "knows" that you never call it (as if you did, your program would be invalid). Since the function is never called, there's no point in emitting any code for the body of the function. It still needs to emit a small stub, as you could legally take the address of it. As it so happens your program does call this function. This makes your program not a valid C program, and so the standard says nothing about how it should behave.
- deleted 8y ago[deleted]
- jchw 8y agoThe C attitude is simple. If your code invokes undefined behavior, the compiler is allowed to do whatever it wants. Not returning a value from a function that is not void is definitely a footgun. The C language itself does not prevent you from invoking these footguns. Why, I don't know for sure; I wasn't around for the genesis of C. I would guess that if C were designed today, most of the undefined behavior that can be detected statically would just be errors. Of course, compilers are allowed to optimize assuming your code does not utilize undefined behavior. This is reasonable in my opinion. If the compiler were not allowed to optimize in ways that broke undefined behavior, it would be nearly impossible to write meaningful compiler optimizations. Most compiler optimizations change the semantics of the execution even though they keep the semantics of the program itself identical according to the rules of the C language. Where would you draw the line? If I monkey patch a function that my compiler decided to inline, would I have reason to complain? In the face of C seeing your completely unused side-effects, it knows there is no possible way your program could ever actually touch those side-effects in the bounds of defined behavior. Therefore, it is free to throw them away. This may seem unreasonable, but in my opinion it's nearly the only reasonable choice. To me the real solution is something like -Wall -Werror. For most statically detectable invocations of undefined behavior, this will save you, throwing an error in this case because "not all codepaths return a value." Then, you can sleep easy without sacrificing decent dead-code elimination.
- denkmoon 8y agoThis gives me nightmares. I could spend weeks debugging this and not think of that.
- tom_mellior 8y agoYou could spend two seconds turning on -Wall instead ;-)
- teddyh 8y ago> Non-optimizing GCC 5.4 silently compiles this with no warnings GCC 5.4 is more than two years old; the current version is 8.2¹. 1. https://www.gnu.org/software/gcc/releases.html https://www.gnu.org/software/gcc/releases.html
- pard68 8y agoBut when you are an enterprise too afraid to upgrade your version of RHEL then GCC 5.4 <= may be what you have. I know a company that paid a considerable amount of money to am outside team to get the current release of GCC running on their no longer supported version of RHEL.