https://gcc.gnu.org/bugzilla/show_bug.cgi?id=95385

--- Comment #3 from Haoxin Tu <haoxintu at gmail dot com> ---
(In reply to Richard Biener from comment #2)
> We likely eliminate the dead 0/0 and 0/b instructions but fail for the one in
> the loop at -O0.  As soon as the actual instruction is carried out the
> program traps and obviously further runtime messages are no longer emitted.
> 
> There's no bug here.

Hi Richard, take look at this case 

test3.cc
#include<iostream>
int main () {
    for (int i = 0; i < 1; ++i) {
        int a1 = 0;
        int aa1 = a1/0; 
    }
    int b = 0;
    int bb = 0 / b;
    0 / 0;
    std::cout << "ok" << std::endl;
    return 0;
}

$./g++ -w -fsanitize=integer-divide-by-zero test3.cc ; ./a.out 
test3.cc:5:21: runtime error: division by zero

The actual instruction is carried out as you said.

But in test4.cc
#include<iostream>
int main () {
    for (int i = 0; i < 1; ++i) {
        int b = 0;
        int bb = 0 / b; 
        0 / 0;
    }
    int b = 0;
    int bb = 0 / b;
    0 / 0;
    std::cout << "ok" << std::endl;
    return 0;
}

$$./g++ -w -fsanitize=integer-divide-by-zero test4.cc ; ./a.out 
test4.cc:5:20: runtime error: division by zero
test4.cc:6:11: runtime error: division by zero
test4.cc:9:16: runtime error: division by zero
test4.cc:10:7: runtime error: division by zero
ok

I am wondering why GCC treats test3.cc and test4.cc differently. Is there
something I didn’t understand correctly?

Reply via email to