/gcc/testsuite/gcc.dg/Wstringop-overflow-11.c

https://gitlab.com/adotout/gcc · C · 321 lines · 243 code · 63 blank · 15 comment · 4 complexity · d4ebbcbb33a270d753f43e4012923260 MD5 · raw file

  1. /* PR tree-optimization/89350 - Wrong -Wstringop-overflow warning
  2. on a variable offset from the end of an array
  3. Test exercising -Wstringop-truncation with -Wall.
  4. -Wstringop-truncation is disabled to avoid warnings for strncpy
  5. calls whose bound matches the size of the destination getting
  6. in the way of -Wstringop-overflow.
  7. { dg-do compile }
  8. { dg-options "-O2 -Wall -Wno-stringop-truncation -ftrack-macro-expansion=0" } */
  9. #include "range.h"
  10. extern void* memcpy (void*, const void*, size_t);
  11. extern void* memset (void*, int, size_t);
  12. extern char* strcpy (char*, const char*);
  13. extern char* strncpy (char*, const char*, size_t);
  14. void sink (void*);
  15. #define CAT(pfx, line) pfx ## line
  16. #define CONCAT(pfx, line) CAT (pfx, line)
  17. #define UNIQ_NAME(pfx) CONCAT (pfx, __LINE__)
  18. /* Exercise a call to memset with a distinct destination object each
  19. time to prevent GCC from reusing the destination pointer in later
  20. tests. */
  21. #define T(off1, off2, n) \
  22. do { \
  23. extern char UNIQ_NAME (ga)[7]; \
  24. char *d = UNIQ_NAME (ga) + off1; \
  25. d += off2; \
  26. memset (d, 0, n); \
  27. sink (d); \
  28. } while (0)
  29. /* Exercise calls to memset with a destination pointer pointing to
  30. an array plus constant offset plus variable offset, in that order. */
  31. void test_memset_array_cst_range_off (void)
  32. {
  33. T (1, SR (-7, 7), 7);
  34. T (1, SR (-1, 1), 7);
  35. T (1, SR (-1, 1), 9); /* { dg-warning "\\\[-Warray-bounds|-Wstringop-overflow" } */
  36. T (1, SR ( 1, 2), 1);
  37. T (1, SR ( 1, 2), 5);
  38. T (1, SR ( 0, 1), 6);
  39. T (1, UR ( 1, 2), 7); /* { dg-warning "\\\[-Warray-bounds|-Wstringop-overflow" } */
  40. T (2, SR (-7, 7), 7);
  41. T (2, SR (-2, 7), 7);
  42. T (2, SR (-1, 1), 7); /* { dg-warning "\\\[-Warray-bounds|-Wstringop-overflow" } */
  43. T (2, SR (-1, 1), 9); /* { dg-warning "\\\[-Warray-bounds|-Wstringop-overflow" } */
  44. T (2, SR ( 1, 2), 1);
  45. T (2, SR ( 1, 2), 3);
  46. T (2, SR ( 1, 2), 4);
  47. T (2, SR ( 1, 2), 5); /* { dg-warning "\\\[-Warray-bounds|-Wstringop-overflow" } */
  48. T (2, SR ( 0, 1), 6); /* { dg-warning "\\\[-Warray-bounds|-Wstringop-overflow" "pr89428" } */
  49. T (2, UR ( 1, 2), 7); /* { dg-warning "\\\[-Warray-bounds|-Wstringop-overflow" } */
  50. T (7, UR (-7, 0), 7);
  51. T (7, UR (-7, 0), 9); /* { dg-warning "\\\[-Warray-bounds|-Wstringop-overflow" } */
  52. T (7, UR (-3, 2), 3);
  53. T (7, UR (-2, 2), 5); /* { dg-warning "\\\[-Warray-bounds|-Wstringop-overflow" } */
  54. }
  55. /* Exercise calls to memset with a destination pointer pointing to
  56. an array plus variable offset plus constant offset. */
  57. void test_memset_array_range_cst_off (void)
  58. {
  59. T (SR (-7, 7), 1, 7);
  60. T (SR (-1, 1), 1, 7);
  61. T (SR (-1, 1), 1, 9); /* { dg-warning "\\\[-Warray-bounds|-Wstringop-overflow" "pr89428" } */
  62. T (SR ( 1, 2), 1, 1);
  63. T (SR ( 1, 2), 1, 5);
  64. T (SR ( 0, 1), 1, 6);
  65. T (UR ( 1, 2), 1, 7); /* { dg-warning "\\\[-Warray-bounds|-Wstringop-overflow" } */
  66. T (SR (-7, 7), 2, 7);
  67. T (SR (-1, 1), 2, 7); /* { dg-warning "\\\[-Warray-bounds|-Wstringop-overflow" } */
  68. T (SR (-1, 1), 2, 9); /* { dg-warning "\\\[-Warray-bounds|-Wstringop-overflow" } */
  69. T (SR ( 1, 2), 2, 1);
  70. T (SR ( 1, 2), 2, 3);
  71. T (SR ( 1, 2), 2, 4);
  72. T (SR ( 1, 2), 2, 5); /* { dg-warning "\\\[-Warray-bounds|-Wstringop-overflow" } */
  73. T (SR ( 0, 1), 2, 6); /* { dg-warning "\\\[-Warray-bounds|-Wstringop-overflow" } */
  74. T (UR ( 1, 2), 2, 7); /* { dg-warning "\\\[-Warray-bounds|-Wstringop-overflow" } */
  75. }
  76. void test_memset_array_range_range_off (void)
  77. {
  78. T (UR (0, 1), UR (0, 1), 7);
  79. T (UR (3, 5), UR (2, 7), 1);
  80. T (UR (3, 7), UR (2, 9), 2);
  81. T (UR (3, 9), UR (2, 9), 3); /* { dg-warning "\\\[-Warray-bounds|-Wstringop-overflow" } */
  82. T (UR (0, 1), UR (1, 2), 7); /* { dg-warning "\\\[-Warray-bounds|-Wstringop-overflow" } */
  83. }
  84. #undef T
  85. #define T(off1, off2, n) \
  86. do { \
  87. extern char UNIQ_NAME (ga)[7]; \
  88. char *d = UNIQ_NAME (ga) + off1; \
  89. d += off2; \
  90. memcpy (d, s, n); \
  91. sink (d); \
  92. } while (0)
  93. void test_memcpy_array_cst_range_off (const void *s)
  94. {
  95. T (1, SR (-7, 7), 7);
  96. T (1, SR (-1, 1), 7);
  97. T (1, SR (-1, 1), 9); /* { dg-warning "\\\[-Warray-bounds|-Wstringop-overflow" } */
  98. T (1, SR ( 1, 2), 1);
  99. T (1, SR ( 1, 2), 5);
  100. T (1, SR ( 0, 1), 6);
  101. T (1, UR ( 1, 2), 7); /* { dg-warning "\\\[-Warray-bounds|-Wstringop-overflow" } */
  102. T (2, SR (-7, 7), 7);
  103. T (2, SR (-2, 7), 7);
  104. T (2, SR (-1, 1), 7); /* { dg-warning "\\\[-Warray-bounds|-Wstringop-overflow" } */
  105. T (2, SR (-1, 1), 9); /* { dg-warning "\\\[-Warray-bounds|-Wstringop-overflow" } */
  106. T (2, SR ( 1, 2), 1);
  107. T (2, SR ( 1, 2), 3);
  108. T (2, SR ( 1, 2), 4);
  109. T (2, SR ( 1, 2), 5); /* { dg-warning "\\\[-Warray-bounds|-Wstringop-overflow" } */
  110. T (2, SR ( 0, 1), 6); /* { dg-warning "\\\[-Warray-bounds|-Wstringop-overflow" "pr89428" } */
  111. T (2, UR ( 1, 2), 7); /* { dg-warning "\\\[-Warray-bounds|-Wstringop-overflow" } */
  112. T (7, UR (-7, 0), 7);
  113. T (7, UR (-7, 0), 9); /* { dg-warning "\\\[-Warray-bounds|-Wstringop-overflow" } */
  114. T (7, UR (-3, 2), 3);
  115. T (7, UR (-2, 2), 5); /* { dg-warning "\\\[-Warray-bounds|-Wstringop-overflow" } */
  116. }
  117. void test_memcpy_array_range_cst_off (const void *s)
  118. {
  119. T (SR (-7, 7), 1, 7);
  120. T (SR (-1, 1), 1, 7);
  121. T (SR (-1, 1), 1, 9); /* { dg-warning "\\\[-Warray-bounds|-Wstringop-overflow" "pr89428" } */
  122. T (SR ( 1, 2), 1, 1);
  123. T (SR ( 1, 2), 1, 5);
  124. T (SR ( 0, 1), 1, 6);
  125. T (UR ( 1, 2), 1, 7); /* { dg-warning "\\\[-Warray-bounds|-Wstringop-overflow" } */
  126. T (SR (-7, 7), 2, 7);
  127. T (SR (-1, 1), 2, 7); /* { dg-warning "\\\[-Warray-bounds|-Wstringop-overflow" } */
  128. T (SR (-1, 1), 2, 9); /* { dg-warning "\\\[-Warray-bounds|-Wstringop-overflow" } */
  129. T (SR ( 1, 2), 2, 1);
  130. T (SR ( 1, 2), 2, 3);
  131. T (SR ( 1, 2), 2, 4);
  132. T (SR ( 1, 2), 2, 5); /* { dg-warning "\\\[-Warray-bounds|-Wstringop-overflow" } */
  133. T (SR ( 0, 1), 2, 6); /* { dg-warning "\\\[-Warray-bounds|-Wstringop-overflow" } */
  134. T (UR ( 1, 2), 2, 7); /* { dg-warning "\\\[-Warray-bounds|-Wstringop-overflow" } */
  135. }
  136. void test_memcpy_array_range_range_off (const void *s)
  137. {
  138. T (UR (0, 1), UR (0, 1), 7);
  139. T (UR (3, 5), UR (2, 7), 1);
  140. T (UR (3, 7), UR (2, 9), 2);
  141. T (UR (3, 9), UR (2, 9), 3); /* { dg-warning "\\\[-Warray-bounds|-Wstringop-overflow" } */
  142. T (UR (0, 1), UR (1, 2), 7); /* { dg-warning "\\\[-Warray-bounds|-Wstringop-overflow" } */
  143. }
  144. #undef T
  145. #define T(off1, off2, n) \
  146. do { \
  147. extern char UNIQ_NAME (ga)[7]; \
  148. char *d = UNIQ_NAME (ga) + off1; \
  149. d += off2; \
  150. const char str[] = "0123456789"; \
  151. const char *s = str + sizeof str - 1 - n; \
  152. strcpy (d, s); \
  153. sink (d); \
  154. } while (0)
  155. void test_strcpy_array_cst_range_off (void)
  156. {
  157. T (1, SR (-7, 7), 6);
  158. T (1, SR (-1, 1), 6);
  159. T (1, SR (-1, 1), 8); /* { dg-warning "\\\[-Warray-bounds|-Wstringop-overflow" } */
  160. T (1, SR ( 1, 2), 0);
  161. T (1, SR ( 1, 2), 4);
  162. T (1, SR ( 0, 1), 5);
  163. T (1, UR ( 1, 2), 6); /* { dg-warning "\\\[-Warray-bounds|-Wstringop-overflow" } */
  164. T (2, SR (-7, 7), 6);
  165. T (2, SR (-2, 7), 6);
  166. T (2, SR (-1, 1), 6); /* { dg-warning "\\\[-Warray-bounds|-Wstringop-overflow" } */
  167. T (2, SR (-1, 1), 8); /* { dg-warning "\\\[-Warray-bounds|-Wstringop-overflow" } */
  168. T (2, SR ( 1, 2), 0);
  169. T (2, SR ( 1, 2), 2);
  170. T (2, SR ( 1, 2), 3);
  171. T (2, SR ( 1, 2), 4); /* { dg-warning "\\\[-Warray-bounds|-Wstringop-overflow" } */
  172. T (2, SR ( 0, 1), 5); /* { dg-warning "\\\[-Warray-bounds|-Wstringop-overflow" "pr89428" } */
  173. T (2, UR ( 1, 2), 6); /* { dg-warning "\\\[-Warray-bounds|-Wstringop-overflow" } */
  174. T (7, UR (-7, 0), 6);
  175. T (7, UR (-7, 0), 8); /* { dg-warning "\\\[-Warray-bounds|-Wstringop-overflow" } */
  176. T (7, UR (-3, 2), 2);
  177. T (7, UR (-2, 2), 4); /* { dg-warning "\\\[-Warray-bounds|-Wstringop-overflow" } */
  178. }
  179. void test_strcpy_array_range_cst_off (const char *s)
  180. {
  181. T (SR (-7, 7), 1, 6);
  182. T (SR (-1, 1), 1, 6);
  183. T (SR (-1, 1), 1, 8); /* { dg-warning "\\\[-Warray-bounds|-Wstringop-overflow" "pr89428" } */
  184. T (SR ( 1, 2), 1, 0);
  185. T (SR ( 1, 2), 1, 1);
  186. T (SR ( 1, 2), 1, 4);
  187. T (SR ( 0, 1), 1, 5);
  188. T (UR ( 1, 2), 1, 6); /* { dg-warning "\\\[-Warray-bounds|-Wstringop-overflow" } */
  189. T (SR (-7, 7), 2, 6);
  190. T (SR (-1, 1), 2, 6); /* { dg-warning "\\\[-Warray-bounds|-Wstringop-overflow" } */
  191. T (SR (-1, 1), 2, 8); /* { dg-warning "\\\[-Warray-bounds|-Wstringop-overflow" } */
  192. T (SR ( 1, 2), 2, 0);
  193. T (SR ( 1, 2), 2, 1);
  194. T (SR ( 1, 2), 2, 2);
  195. T (SR ( 1, 2), 2, 3);
  196. T (SR ( 1, 2), 2, 4); /* { dg-warning "\\\[-Warray-bounds|-Wstringop-overflow" } */
  197. T (SR ( 0, 1), 2, 5); /* { dg-warning "\\\[-Warray-bounds|-Wstringop-overflow" } */
  198. T (UR ( 1, 2), 2, 6); /* { dg-warning "\\\[-Warray-bounds|-Wstringop-overflow" } */
  199. }
  200. #undef T
  201. #define T(off1, off2, n) \
  202. do { \
  203. extern char UNIQ_NAME (ga)[7]; \
  204. char *d = UNIQ_NAME (ga) + off1; \
  205. d += off2; \
  206. strncpy (d, s, n); \
  207. sink (d); \
  208. } while (0)
  209. void test_strncpy_array_cst_range_off (const char *s)
  210. {
  211. T (1, SR (-7, 7), 7);
  212. T (1, SR (-1, 1), 7);
  213. T (1, SR (-1, 1), 9); /* { dg-warning "\\\[-Warray-bounds|-Wstringop-overflow" } */
  214. T (1, SR ( 1, 2), 1);
  215. T (1, SR ( 1, 2), 5);
  216. T (1, SR ( 0, 1), 6);
  217. T (1, UR ( 1, 2), 7); /* { dg-warning "\\\[-Warray-bounds|-Wstringop-overflow" } */
  218. T (2, SR ( -7, 7), 7);
  219. T (2, SR ( -1, 1), 7); /* { dg-warning "\\\[-Warray-bounds|-Wstringop-overflow" } */
  220. T (2, SR ( -1, 1), 9); /* { dg-warning "\\\[-Warray-bounds|-Wstringop-overflow" } */
  221. T (2, SR ( 1, 2), 1);
  222. T (2, SR ( 1, 2), 3);
  223. T (2, SR ( 1, 2), 4);
  224. T (2, SR ( 1, 2), 5); /* { dg-warning "\\\[-Warray-bounds|-Wstringop-overflow" } */
  225. T (2, SR ( 0, 1), 6); /* { dg-warning "\\\[-Warray-bounds|-Wstringop-overflow" "pr89428" } */
  226. T (2, UR ( 1, 2), 7); /* { dg-warning "\\\[-Warray-bounds|-Wstringop-overflow" } */
  227. T (7, UR (-7, 0), 7);
  228. T (7, UR (-7, 0), 9); /* { dg-warning "\\\[-Warray-bounds|-Wstringop-overflow" } */
  229. T (7, UR (-3, 2), 3);
  230. T (7, UR (-2, 2), 5); /* { dg-warning "\\\[-Warray-bounds|-Wstringop-overflow" } */
  231. }
  232. void test_strncpy_array_range_cst_off (const char *s)
  233. {
  234. T (SR (-7, 7), 1, 7);
  235. T (SR (-1, 1), 1, 7);
  236. T (SR (-1, 1), 1, 9); /* { dg-warning "\\\[-Warray-bounds|-Wstringop-overflow" "pr89428" } */
  237. T (SR ( 1, 2), 1, 1);
  238. T (SR ( 1, 2), 1, 5);
  239. T (SR ( 0, 1), 1, 6);
  240. T (UR ( 1, 2), 1, 7); /* { dg-warning "\\\[-Warray-bounds|-Wstringop-overflow" } */
  241. T (SR (-7, 7), 2, 7);
  242. T (SR (-1, 1), 2, 7); /* { dg-warning "\\\[-Warray-bounds|-Wstringop-overflow" } */
  243. T (SR (-1, 1), 2, 9); /* { dg-warning "\\\[-Warray-bounds|-Wstringop-overflow" } */
  244. T (SR ( 1, 2), 2, 1);
  245. T (SR ( 1, 2), 2, 3);
  246. T (SR ( 1, 2), 2, 4);
  247. T (SR ( 1, 2), 2, 5); /* { dg-warning "\\\[-Warray-bounds|-Wstringop-overflow" } */
  248. T (SR ( 0, 1), 2, 6); /* { dg-warning "\\\[-Warray-bounds|-Wstringop-overflow" } */
  249. T (UR ( 1, 2), 2, 7); /* { dg-warning "\\\[-Warray-bounds|-Wstringop-overflow" } */
  250. }
  251. void test_strncpy_array_range_range_off (const char *s)
  252. {
  253. T (UR (0, 1), UR (0, 1), 7);
  254. T (UR (3, 5), UR (2, 7), 1);
  255. T (UR (3, 7), UR (2, 9), 2);
  256. T (UR (3, 9), UR (2, 9), 3); /* { dg-warning "\\\[-Warray-bounds|-Wstringop-overflow" } */
  257. T (UR (0, 1), UR (1, 2), 7); /* { dg-warning "\\\[-Warray-bounds|-Wstringop-overflow" } */
  258. }