4 ms·
Do compilers emit warnings here (do they usually do that when there is a known UB?) Here it's clear that "NeverCalled" is never called so it's clear that Do is
by alkonaut 9y ago
Do compilers emit warnings here (do they usually do that when there is a known UB?)
Here it's clear that "NeverCalled" is never called so it's clear that Do is never assigned, so the Call can't produce anything useful.
EDIT: Ugh missed the lack of "static" on the NeverCalled. Never mind.
Is this too hard to decide with certainty in C because of how you could do some pointer gymnastics and just fiddle with the field without the compiler realizing, so in order not to spit out false positives, compilers just have to assume the author knows what he's doing and allow the suspected UB without warning?
I'm just used to higher level "safe" languages (Rust, C#, etc), where lack of assignment is usually determined with certainty.
- kbeckmann 9y agoWith clang you can enable run time UB checks using the -fsanitize=undefined flag[0]. But in this case, the sanitizer doesn't detect undefined behavior. I tried to build the example (and changing the system to an echo) using "clang a.cpp -o a -Os -std=c++11 -lstdc++ -fsanitize=undefined", but the sanitizer didn't detect the UB. [0] https://clang.llvm.org/docs/UndefinedBehaviorSanitizer.html https://clang.llvm.org/docs/UndefinedBehaviorSanitizer.html
- _pmf_ 9y ago> but the sanitizer didn't detect the UB. Isn't this because the sanitizer works with the instrumented "optimized" code, which does no longer invoke UB? Or is the clang toolchain intelligent enough to not perform UB dependent optimizations when the sanitizer is specified?
- umanwizard 9y agoWhat would it even mean for compiled, machine language code to invoke UB or not? UB is mostly a C concept, not a lower-level one. The semantics of assembly language programs are well-defined much more often (the only counterexample I can think of is stuff involving using threading, memory barriers, etc. incorrect)
- batat 9y agoI can't build it with fsanitize=undefined at all with clang 3.4: > clang -Os -std=c++11 -Wall -o boom -fsanitize=undefined boom.cpp /tmp/boom-428713.o:(.rodata._ZTIFvvE[_ZTIFvvE]+0x0): undefined reference to `vtable for __cxxabiv1::__function_type_info' /tmp/boom-428713.o:(.rodata._ZTIFivE[_ZTIFivE]+0x0): undefined reference to `vtable for __cxxabiv1::__function_type_info' clang: error: linker command failed with exit code 1 (use -v to see invocation)
- red75prime 9y agoOutput of clang with -Weverything 11 : <source>:11:6: warning: no previous prototype for function 'NeverCalled' [-Wmissing-prototypes] void NeverCalled() { ^ 1 warning generated.
- rossy 9y agoThat's mostly a cosmetic warning. If you add a prototype for NeverCalled() at the top of the file, it silences the warning, but Clang still emits the same code.
- _pmf_ 9y ago> If you add a prototype for NeverCalled() at the top of the file, it silences the warning Or you could write correct C and use (void) instead of () as argument list.
- rossy 9y agoThis example is C++, not C. Clang is complaining because it's good practice to declare a prototype for a non-static function (in a header file) before using or declaring the function itself, so it can't accidentally be used with the wrong parameter list in a different compilation unit. It will produce this warning if you have () or (void) as the parameter list.
- deleted 9y ago[deleted]
- culturedsystems 9y ago"Here it's clear that "NeverCalled" is never called so it's clear that Do is never assigned." I don't think that is clear. If the given file is linked with another file that initializes a static variable with a function that calls NeverCalled, the initialization will be done before main is called, and so NeverCalled will be called. The compiler can't tell from just analysing the one file shown here whether or not NeverCalled is called.
- alkonaut 9y ago> I don't think that is clear Right. I missed that the uncalled function was public. So the issue is not clear until you link I suppose, so only detected "globally" whether the function is called. is it fair to say that that kind of global analysis is rare in the compiler+linker toolchain world for C, and usually left to static analyzers instead? > The compiler can't tell from just analysing the one file shown here whether or not NeverCalled is called. Right, and making it static you get the -Wunused-function. Great. This warning is about the NeverCalled function being unused though, and not about Do being unassigned before it's called. Curious, is there any way to get a warning for this? This is UB, correct? And the assignment is now known not to happen? typedef int (*Function)(); static Function Do; void main() { Do(); }
- tom_mellior 9y ago> another file that initializes a static variable with a function that calls NeverCalled How would that work in actual code? Static initialization must be done with constant expressions. C++ has constexpr functions, but they cannot call NeverCalled unless it is also marked constexpr, can they?