Geoffrey1014/SA_Bugs

GCC --Wdiv-by-zero false negative with `0 <= (f = 0) % e.b`

Closed this issue · 2 comments

date: 2023-1-26
commit: 8c8ca873216387bc26046615c806b96f0345ff9d
args: -O0 -fanalyzer
test:

void __analyzer_eval();

union a
{
  int b;
} c()
{
  union a e;
  int f;
  e.b = 0;
  if (0 <= (f = 0) % e.b)
  {
    __analyzer_eval(0 <= (f = 0) % e.b);
  }
}

report: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=109200
fix:
original:

GCC Static Analyzer does not generate a div-by-zero warning for the 0 <= (f = 0) % e.b statement, but if it is changed to 0 <= (f = 0) % 0, analyzer generates that warning.

See it live: https://godbolt.org/z/PYoroM8hx

void __analyzer_eval();

union a
{
  int b;
} ;
void c(){
  union a e;
  int f;
  e.b = 0;
  if (0 <= (f = 0) % e.b)
  // if (0 <= (f = 0) % 0)
  {
    __analyzer_eval(0 <= (f = 0) % e.b);
  }
}

Output:

<source>: In function 'c':
<source>:14:5: warning: TRUE
   14 |     __analyzer_eval(0 <= (f = 0) % e.b);
      |     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Compiler returned: 0

duplicate of #51