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