array-struct.c revision a7ac9444b4b82de868fac9710a56807898a90b02
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} 48 49// Struct variable in lvalue context. 50// Assign UnknownVal to the whole struct. 51void f5() { 52 struct s data; 53 g1(&data); 54} 55 56// AllocaRegion test. 57void f6() { 58 char *p; 59 p = __builtin_alloca(10); 60 p[1] = 'a'; 61} 62 63struct s2; 64 65void g2(struct s2 *p); 66 67// Incomplete struct pointer used as function argument. 68void f7() { 69 struct s2 *p = __builtin_alloca(10); 70 g2(p); 71} 72 73// sizeof() is unsigned while -1 is signed in array index. 74void f8() { 75 int a[10]; 76 a[sizeof(a)/sizeof(int) - 1] = 1; // no-warning 77} 78 79// Initialization of struct array elements. 80void f9() { 81 struct s a[10]; 82} 83 84// Initializing array with string literal. 85void f10() { 86 char a1[4] = "abc"; 87 char a3[6] = "abc"; 88} 89