-
Notifications
You must be signed in to change notification settings - Fork 11.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Clang stop detecting UBs after a divide by zero #45469
Comments
This means your program crashed... which is what naturally happens on x86 when a |
Hi, Eli, what value should be returned is not my concern. look this case #include<iostream>
int main () {
0 / 0;
0 / 0;
std::cout << "ok" << std::endl;
return 0;
} in clang-trunk $clang++-trunk -fsanitize=integer-divide-by-zero test3.cc ; ./a.out
test3.cc:3:7: runtime error: division by zero
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior example.cpp:3:7 in
test3.cc:4:7: runtime error: division by zero
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior example.cpp:4:7 in
ok I mean |
Do you mean it doesn't matter to the program? Or that you personally don't care what value the compiler chooses? If you run |
In this case the dead code (to perform the division) was never emitted - so the program printed out the diagnostic, but continued. In your first case, the code isn't dead - the code is used and actually performs the division, and produces a floating point signal/exception. That's just what the program does (that's the behavior of the undefined behavior the program invokes). All UBSan did was transform the way that failure was printed. If you run the program without UBSan, you get this: $ clang++ -w div.cpp && ./a.out
Floating point exception
$ UBSan can't make the program continue beyond that point - there's no value that could be produced by the division that would allow the program to continue in any defined way. (that's what Eli was getting at - what value should 0/0 produce to put into the variable to let the program continue to use?) |
I think I get your point. The reason is that GCC treats "0/0" produce 0 but "1/0" as an exception, and Clang treats all of them as an exception. I am so sorry I have no idea about what "0/0" should be produced, but I hold the view that Clang and GCC should produce consistent results. |
Take a look at this case in float-divide-by-zero
#include<iostream>
int main () {
float a = 1/0.0;
std::cout << a << std::endl;
return 0;
} $clang++-trunk -w -fsanitize=float-divide-by-zero test4.cc ; ./a.out
test4.cc:3:16: runtime error: division by zero
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior example.cpp:3:16 in
inf I think the results in integer-divide-by-zero might be treated similarly to float-divide-by-zero, just print "inf" to "0/0" rather than trigger a dead signal to stop following statements. |
Sorry, by a mistake, print "nan" to "0/0" and "inf" to "1/0". |
Nature of undefined behavior - it can vary between compilers. "inf" can't be printed, because "inf" doesn't fit in an integer. Floating point values have representations of infinity, integers do not. |
Extended Description
This code
test1.cc
Clang only detects one runtime error in for statement but leaves out detecting the statements following ones.
In
test2.cc
Should the main function return from
return 0
rather than exit directly from the for statement?I also file a bug report in GCC, they confirmed this limitation immediately.
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=95385
The text was updated successfully, but these errors were encountered: