array-struct.c 3.6 KB

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