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

            Bug ID: 95385
           Summary: GCC stop detect UBs after a divide by zero in for
                    statement
           Product: gcc
           Version: 11.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: haoxintu at gmail dot com
  Target Milestone: ---

This code test1.cc

#include<iostream>
int main () {
    int a = 1;
    for (int i = 0; i < 10; ++i) {
        a /= i; // Error: division by zero on the first iteration
    }

    int b = 0;
    int bb = 0 / b;
    0 / 0;
    std::cout << "ok" << std::endl;
    return 0;
}

$./g++ -w -fsanitize=integer-divide-by-zero test1.cc ; ./a.out 
test.cc:5:11: runtime error: division by zero
Floating point exception (core dumped)

GCC only detects one runtime error in for statement but leaves out detecting
the statements following ones.

In test2.cc

#include<iostream>
int main () {
    /*
    int a = 1;
    for (int i = 0; i < 10; ++i) {
        a /= i; // Error: division by zero on the first iteration
    }
    */

    int b = 0;
    int bb = 0 / b;
    0 / 0;
    std::cout << "ok" << std::endl;
    return 0;
}
$./g++ -w -fsanitize=integer-divide-by-zero test2.cc ; ./a.out 
test.cc:11:16: runtime error: division by zero
test.cc:12:7: runtime error: division by zero
ok

Should the main function return from "return 0" rather than exit directly from
the for statement?

Reply via email to