aboutsummaryrefslogtreecommitdiff
path: root/clang/test/Analysis/div-zero.cpp
blob: 51ea25e828a18bc3a482668259729946d895fb48 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// RUN: %clang_analyze_cc1 -analyzer-checker=core.DivideZero -verify %s

int fooPR10616 (int qX ) {
  int a, c, d;

  d = (qX-1);
  while ( d != 0 ) {
    d = c - (c/d) * d;
  }

  return (a % (qX-1)); // expected-warning {{Division by zero}}

}

namespace GH148875 {
struct A {
  int x;
  A(int v) : x(v) {}
};

struct B {
  int x;
  B() : x(0) {}
};

struct C {
  int x, y;
  C(int a, int b) : x(a), y(b) {}
};

struct D {
  int x;
};

struct E {
  D d;
  E(int a) : d{a} {}
};

struct F {
  int x;
};

int t1() {
  A a{42};
  return 1 / (a.x - 42); // expected-warning {{Division by zero}}
}

int t2() {
  B b{};
  return 1 / b.x; // expected-warning {{Division by zero}}
}

int t3() {
  C c1{1, -1};
  return 1 / (c1.x + c1.y); // expected-warning {{Division by zero}}
}

int t4() {
  C c2{0, 0};
  return 1 / (c2.x + c2.y); // expected-warning {{Division by zero}}
}

int t5() {
  E e{32};
  return 1 / (e.d.x - 32); // expected-warning {{Division by zero}}
}

int t6() {
  F f{32};
  return 1 / (f.x - 32); // expected-warning {{Division by zero}}
}
}