1 /* PR middle-end/88956 - ICE: Floating point exception on a memcpy from
2    an zero-length constant array
3    Verify both that memory and string calls with a zero-length array
4    don't cause an ICE, and also that they emit warnings.
5    { dg-do compile }
6    { dg-options "-O2 -Wall" }  */
7 
8 typedef __SIZE_TYPE__ size_t;
9 
10 extern void* memcpy (void*, const void*, size_t);
11 extern void* memmove (void*, const void*, size_t);
12 extern char* strcpy (char*, const char*);
13 extern char* strncpy (char*, const char*, size_t);
14 
15 const char s0[0] = { };
16 const char s0_0[0][0] = { };
17 const char s0_1[0][1] = { };
18 const char s1_0[1][0] = { };
19 
20 char d[4];
21 
test_memcpy_s0_1(void * d)22 void* test_memcpy_s0_1 (void *d)
23 {
24   return memcpy (d, s0, 1);       /* { dg-warning "\\\[-Warray-bounds|-Wstringop-overflow" } */
25 }
26 
test_memcpy_s0_2(void * d)27 void* test_memcpy_s0_2 (void *d)
28 {
29   return memcpy (d, s0, 2);       /* { dg-warning "\\\[-Warray-bounds|-Wstringop-overflow" } */
30 }
31 
test_memcpy_s0_0_1(void * d)32 void* test_memcpy_s0_0_1 (void *d)
33 {
34   return memcpy (d, s0_0, 1);     /* { dg-warning "\\\[-Warray-bounds|-Wstringop-overflow" } */
35 }
36 
test_memcpy_s0_0_2(void * d)37 void* test_memcpy_s0_0_2 (void *d)
38 {
39   return memcpy (d, s0_0, 2);     /* { dg-warning "\\\[-Warray-bounds|-Wstringop-overflow" } */
40 }
41 
42 
test_memcpy_s0_1_1(void * d)43 void* test_memcpy_s0_1_1 (void *d)
44 {
45   return memcpy (d, s0_1, 1);     /* { dg-warning "\\\[-Warray-bounds|-Wstringop-overflow" } */
46 }
47 
test_memcpy_s0_1_2(void * d)48 void* test_memcpy_s0_1_2 (void *d)
49 {
50   return memcpy (d, s0_1, 2);     /* { dg-warning "\\\[-Warray-bounds|-Wstringop-overflow" } */
51 }
52 
53 
test_memcpy_s1_0_1(void * d)54 void* test_memcpy_s1_0_1 (void *d)
55 {
56   return memcpy (d, s1_0, 1);     /* { dg-warning "\\\[-Warray-bounds|-Wstringop-overflow" } */
57 }
58 
test_memcpy_s1_0_2(void * d)59 void* test_memcpy_s1_0_2 (void *d)
60 {
61   return memcpy (d, s1_0, 2);     /* { dg-warning "\\\[-Warray-bounds|-Wstringop-overflow" } */
62 }
63 
64 
test_memmove_s0_1(void * d)65 void* test_memmove_s0_1 (void *d)
66 {
67   return memmove (d, s0, 1);      /* { dg-warning "\\\[-Warray-bounds|-Wstringop-overflow" } */
68 }
69 
test_memmove_s0_2(void * d)70 void* test_memmove_s0_2 (void *d)
71 {
72   return memmove (d, s0, 2);      /* { dg-warning "\\\[-Warray-bounds|-Wstringop-overflow" } */
73 }
74 
test_memmove_s0_0_1(void * d)75 void* test_memmove_s0_0_1 (void *d)
76 {
77   return memmove (d, s0_0, 1);    /* { dg-warning "\\\[-Warray-bounds|-Wstringop-overflow" } */
78 }
79 
test_memmove_s0_0_2(void * d)80 void* test_memmove_s0_0_2 (void *d)
81 {
82   return memmove (d, s0_0, 2);    /* { dg-warning "\\\[-Warray-bounds|-Wstringop-overflow" } */
83 }
84 
85 
86 struct Empty { };
87 const struct Empty e = { };
88 const struct Empty e0[0] = { };
89 const struct Empty e0_0[0][0] = { };
90 const struct Empty e0_1[0][1] = { };
91 const struct Empty e1_0[1][0] = { };
92 
test_memcpy_e_1(void * d)93 void* test_memcpy_e_1 (void *d)
94 {
95   return memcpy (d, &e, 1);       /* { dg-warning "\\\[-Warray-bounds|-Wstringop-overflow" } */
96 }
97 
test_memcpy_e0_1(void * d)98 void* test_memcpy_e0_1 (void *d)
99 {
100   return memcpy (d, e0, 1);       /* { dg-warning "\\\[-Warray-bounds|-Wstringop-overflow" } */
101 }
102 
test_memcpy_e0_0_1(void * d)103 void* test_memcpy_e0_0_1 (void *d)
104 {
105   return memcpy (d, e0_0, 1);     /* { dg-warning "\\\[-Warray-bounds|-Wstringop-overflow" } */
106 }
107 
test_memcpy_e0_1_1(void * d)108 void* test_memcpy_e0_1_1 (void *d)
109 {
110   return memcpy (d, e0_1, 1);     /* { dg-warning "\\\[-Warray-bounds|-Wstringop-overflow" } */
111 }
112 
test_memcpy_e1_0_1(void * d)113 void* test_memcpy_e1_0_1 (void *d)
114 {
115   return memcpy (d, e1_0, 1);     /* { dg-warning "\\\[-Warray-bounds|-Wstringop-overflow" } */
116 }
117 
118 
test_strcpy_s0(char * d)119 char* test_strcpy_s0 (char *d)
120 {
121   return strcpy (d, s0);          /* { dg-warning "\\\[-Warray-bounds|-Wstringop-overflow" } */
122 }
123 
test_strcpy_s0_0(char * d)124 char* test_strcpy_s0_0 (char *d)
125 {
126   return strcpy (d, s0_0[0]);     /* { dg-warning "\\\[-Warray-bounds|-Wstringop-overflow" } */
127 }
128 
129 
test_strncpy_s0_1(char * d)130 char* test_strncpy_s0_1 (char *d)
131 {
132   return strncpy (d, s0, 1);    /* { dg-warning "\\\[-Warray-bounds|-Wstringop-overflow" } */
133 }
134 
test_strncpy_s0_2(char * d)135 char* test_strncpy_s0_2 (char *d)
136 {
137   return strncpy (d, s0, 2);    /* { dg-warning "\\\[-Warray-bounds|-Wstringop-overflow" } */
138 }
139 
test_strncpy_s0_0_1(char * d)140 char* test_strncpy_s0_0_1 (char *d)
141 {
142   return strncpy (d, s0_0[0], 1); /* { dg-warning "\\\[-Warray-bounds|-Wstringop-overflow" } */
143 }
144 
test_strncpy_s0_0_2(char * d)145 char* test_strncpy_s0_0_2 (char *d)
146 {
147   return strncpy (d, s0_0[0], 2); /* { dg-warning "\\\[-Warray-bounds|-Wstringop-overflow" } */
148 }
149