array-struct.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. // RUN: %clang_cc1 -analyze -analyzer-checker=core,alpha.core.CastToStruct -analyzer-store=region -analyzer-constraints=range -verify %s
  2. struct s {
  3. int data;
  4. int data_array[10];
  5. };
  6. typedef struct {
  7. int data;
  8. } STYPE;
  9. void g(char *p);
  10. void g1(struct s* p);
  11. // Array to pointer conversion. Array in the struct field.
  12. void f(void) {
  13. int a[10];
  14. int (*p)[10];
  15. p = &a;
  16. (*p)[3] = 1;
  17. struct s d;
  18. struct s *q;
  19. q = &d;
  20. q->data = 3;
  21. d.data_array[9] = 17;
  22. }
  23. // StringLiteral in lvalue context and pointer to array type.
  24. // p: ElementRegion, q: StringRegion
  25. void f2() {
  26. char *p = "/usr/local";
  27. char (*q)[4];
  28. q = &"abc";
  29. }
  30. // Typedef'ed struct definition.
  31. void f3() {
  32. STYPE s;
  33. }
  34. // Initialize array with InitExprList.
  35. void f4() {
  36. int a[] = { 1, 2, 3};
  37. int b[3] = { 1, 2 };
  38. struct s c[] = {{1,{1}}};
  39. }
  40. // Struct variable in lvalue context.
  41. // Assign UnknownVal to the whole struct.
  42. void f5() {
  43. struct s data;
  44. g1(&data);
  45. }
  46. // AllocaRegion test.
  47. void f6() {
  48. char *p;
  49. p = __builtin_alloca(10);
  50. g(p);
  51. char c = *p;
  52. p[1] = 'a';
  53. // Test if RegionStore::EvalBinOp converts the alloca region to element
  54. // region.
  55. p += 2;
  56. }
  57. struct s2;
  58. void g2(struct s2 *p);
  59. // Incomplete struct pointer used as function argument.
  60. void f7() {
  61. struct s2 *p = __builtin_alloca(10);
  62. g2(p);
  63. }
  64. // sizeof() is unsigned while -1 is signed in array index.
  65. void f8() {
  66. int a[10];
  67. a[sizeof(a)/sizeof(int) - 1] = 1; // no-warning
  68. }
  69. // Initialization of struct array elements.
  70. void f9() {
  71. struct s a[10];
  72. }
  73. // Initializing array with string literal.
  74. void f10() {
  75. char a1[4] = "abc";
  76. char a3[6] = "abc";
  77. }
  78. // Retrieve the default value of element/field region.
  79. void f11() {
  80. struct s a;
  81. g1(&a);
  82. if (a.data == 0) // no-warning
  83. a.data = 1;
  84. }
  85. // Convert unsigned offset to signed when creating ElementRegion from
  86. // SymbolicRegion.
  87. void f12(int *list) {
  88. unsigned i = 0;
  89. list[i] = 1;
  90. }
  91. struct s1 {
  92. struct s2 {
  93. int d;
  94. } e;
  95. };
  96. // The binding of a.e.d should not be removed. Test recursive subregion map
  97. // building: a->e, e->d. Only then 'a' could be added to live region roots.
  98. void f13(double timeout) {
  99. struct s1 a;
  100. a.e.d = (int) timeout;
  101. if (a.e.d == 10)
  102. a.e.d = 4;
  103. }
  104. struct s3 {
  105. int a[2];
  106. };
  107. static struct s3 opt;
  108. // Test if the embedded array is retrieved correctly.
  109. void f14() {
  110. struct s3 my_opt = opt;
  111. }
  112. void bar(int*);
  113. struct s3 gets3() {
  114. struct s3 s;
  115. return s;
  116. }
  117. void accessArrayFieldNoCrash() {
  118. bar(gets3().a);
  119. bar((gets3().a));
  120. bar(((gets3().a)));
  121. }
  122. // Test if the array is correctly invalidated.
  123. void f15() {
  124. int a[10];
  125. bar(a);
  126. if (a[1]) // no-warning
  127. (void)1;
  128. }
  129. struct s3 p[1];
  130. // Code from postgresql.
  131. // Current cast logic of region store mistakenly leaves the final result region
  132. // an ElementRegion of type 'char'. Then load a nonloc::SymbolVal from it and
  133. // assigns to 'a'.
  134. void f16(struct s3 *p) {
  135. struct s3 a = *((struct s3*) ((char*) &p[0])); // expected-warning{{Casting a non-structure type to a structure type and accessing a field can lead to memory access errors or data corruption}}
  136. }
  137. void inv(struct s1 *);
  138. // Invalidate the struct field.
  139. void f17() {
  140. struct s1 t;
  141. int x;
  142. inv(&t);
  143. if (t.e.d)
  144. x = 1;
  145. }
  146. void read(char*);
  147. void f18() {
  148. char *q;
  149. char *p = (char *) __builtin_alloca(10);
  150. read(p);
  151. q = p;
  152. q++;
  153. if (*q) { // no-warning
  154. }
  155. }
  156. // [PR13927] offsetof replacement macro flagged as "dereference of a null pointer"
  157. int offset_of_data_array(void)
  158. {
  159. return ((char *)&(((struct s*)0)->data_array)) - ((char *)0); // no-warning
  160. }
  161. int testPointerArithmeticOnVoid(void *bytes) {
  162. int p = 0;
  163. if (&bytes[0] == &bytes[1])
  164. return 6/p; // no-warning
  165. return 0;
  166. }
  167. int testRValueArraySubscriptExpr(void *bytes) {
  168. int *p = (int*)&bytes[0];
  169. *p = 0;
  170. if (*(int*)&bytes[0] == 0)
  171. return 0;
  172. return 5/(*p); // no-warning
  173. }