vla.c revision 176edba5311f6eff0cad2631449885ddf4fbc9ea
1// RUN: %clang_cc1 -analyze -analyzer-checker=core -verify %s
2
3// Zero-sized VLAs.
4void check_zero_sized_VLA(int x) {
5  if (x)
6    return;
7
8  int vla[x]; // expected-warning{{Declared variable-length array (VLA) has zero size}}
9}
10
11void check_uninit_sized_VLA() {
12  int x;
13  int vla[x]; // expected-warning{{Declared variable-length array (VLA) uses a garbage value as its size}}
14}
15
16// Negative VLAs.
17static void vla_allocate_signed(int x) {
18  int vla[x]; // expected-warning{{Declared variable-length array (VLA) has negative size}}
19}
20
21static void vla_allocate_unsigned(unsigned int x) {
22  int vla[x]; // no-warning
23}
24
25void check_negative_sized_VLA_1() {
26  vla_allocate_signed(-1);
27}
28
29void check_negative_sized_VLA_2() {
30  vla_allocate_unsigned(-1);
31}
32
33void check_negative_sized_VLA_3() {
34  int x = -1;
35  int vla[x]; // expected-warning{{Declared variable-length array (VLA) has negative size}}
36}
37
38void check_negative_sized_VLA_4() {
39  unsigned int x = -1;
40  int vla[x]; // no-warning
41}
42
43void check_negative_sized_VLA_5() {
44  signed char x = -1;
45  int vla[x]; // expected-warning{{Declared variable-length array (VLA) has negative size}}
46}
47
48void check_negative_sized_VLA_6() {
49  unsigned char x = -1;
50  int vla[x]; // no-warning
51}
52
53void check_negative_sized_VLA_7() {
54  signed char x = -1;
55  int vla[x + 2]; // no-warning
56}
57
58void check_negative_sized_VLA_8() {
59  signed char x = 1;
60  int vla[x - 2]; // expected-warning{{Declared variable-length array (VLA) has negative size}}
61}
62
63void check_negative_sized_VLA_9() {
64  int x = 1;
65  int vla[x]; // no-warning
66}
67
68static void check_negative_sized_VLA_10_sub(int x)
69{
70  int vla[x]; // expected-warning{{Declared variable-length array (VLA) has negative size}}
71}
72
73void check_negative_sized_VLA_10(int x) {
74  if (x < 0)
75    check_negative_sized_VLA_10_sub(x);
76}
77
78static void check_negative_sized_VLA_11_sub(int x)
79{
80  int vla[x]; // no-warning
81}
82
83void check_negative_sized_VLA_11(int x) {
84  if (x > 0)
85    check_negative_sized_VLA_11_sub(x);
86}
87