array-struct.c revision be1fe1eb12a1cb91c8e3a9fcc2db4dfe989def6c
1// RUN: clang -analyze -checker-simple -analyzer-store=basic -verify %s &&
2// RUN: clang -analyze -checker-cfref -analyzer-store=basic -verify %s &&
3// RUN: clang -analyze -checker-cfref -analyzer-store=region -verify %s
4
5struct s {
6  int data;
7  int data_array[10];
8};
9
10typedef struct {
11  int data;
12} STYPE;
13
14void g1(struct s* p);
15
16// Array to pointer conversion. Array in the struct field.
17void f(void) {
18  int a[10];
19  int (*p)[10];
20  p = &a;
21  (*p)[3] = 1;
22
23  struct s d;
24  struct s *q;
25  q = &d;
26  q->data = 3;
27  d.data_array[9] = 17;
28}
29
30// StringLiteral in lvalue context and pointer to array type.
31// p: ElementRegion, q: StringRegion
32void f2() {
33  char *p = "/usr/local";
34  char (*q)[4];
35  q = &"abc";
36}
37
38// Typedef'ed struct definition.
39void f3() {
40  STYPE s;
41}
42
43// Initialize array with InitExprList.
44void f4() {
45  int a[] = { 1, 2, 3};
46  int b[3] = { 1, 2 };
47  struct s c[] = {{1,{1}}};
48}
49
50// Struct variable in lvalue context.
51// Assign UnknownVal to the whole struct.
52void f5() {
53  struct s data;
54  g1(&data);
55}
56
57// AllocaRegion test.
58void f6() {
59  char *p;
60  p = __builtin_alloca(10);
61  p[1] = 'a';
62}
63
64struct s2;
65
66void g2(struct s2 *p);
67
68// Incomplete struct pointer used as function argument.
69void f7() {
70  struct s2 *p = __builtin_alloca(10);
71  g2(p);
72}
73
74// sizeof() is unsigned while -1 is signed in array index.
75void f8() {
76  int a[10];
77  a[sizeof(a)/sizeof(int) - 1] = 1; // no-warning
78}
79
80// Initialization of struct array elements.
81void f9() {
82  struct s a[10];
83}
84
85// Initializing array with string literal.
86void f10() {
87  char a1[4] = "abc";
88  char a3[6] = "abc";
89}
90
91// Retrieve the default value of element/field region.
92void f11() {
93  struct s a;
94  g(&a);
95  if (a.data == 0) // no-warning
96    a.data = 1;
97}
98