Dmitry Sokolov
Dmitry Sokolov

Reputation: 3190

PVS-Studio: warning V609 is generated even if denominator is checked

#define b1 0x01
#define b2 0x02
#define b3 0x04
#define b1 0x08

int flag = get_flag();
int x1 = ((flag & b1) ? 1 : 0) + ((flag & b2) ? 1 : 0);
int x2 = ((flag & b3) ? 1 : 0) + ((flag & b4) ? 1 : 0);
int x = max(x1, x2);
if (x < 1)
    throw "fail";
int y = 1 / x;        // <<< V609: Divide by zero. Denominator range [0..2]

y will not be evaluated if x < 1, but V609 is generated. Why?

Upvotes: 0

Views: 52

Answers (1)

AndreyKarpov
AndreyKarpov

Reputation: 999

Thank you for the code fragment. This is a false positive indeed. We'll investigate the reasons why it appears and will try to fix this flaw.

As for now, we suggest using one of the means of false positive suppression or use suppression base.

Upvotes: 1

Related Questions