1 /* Test to verify that even strictly undefined strlen() calls with
2    unterminated character arrays yield the "expected" results when
3    the terminating nul is present in a subsequent suobobject.  */
4 
5 extern __SIZE_TYPE__ strlen (const char *);
6 
7 unsigned nfails;
8 
9 #define A(expr, N)						\
10   do {								\
11     const char *s = (expr);					\
12     unsigned n = strlen (s);					\
13     ((n == N)							\
14      ? 0							\
15      : (__builtin_printf ("line %i: strlen (%s = \"%s\")"	\
16 			  " == %u failed\n",			\
17 			  __LINE__, #expr, s, N),		\
18 	++nfails));						\
19   } while (0)
20 
21 
22 int idx;
23 
24 
25 const char ca[][4] = {
26   { '1', '2', '3', '4' }, { '5' },
27   { '1', '2', '3', '4' }, { '5', '6' },
28   { '1', '2', '3', '4' }, { '5', '6', '7' },
29   { '1', '2', '3', '4' }, { '5', '6', '7', '8' },
30   { '9' }
31 };
32 
test_const_global_arrays(void)33 static void test_const_global_arrays (void)
34 {
35   A (ca[0], 5);
36   A (&ca[0][0], 5);
37   A (&ca[0][1], 4);
38   A (&ca[0][3], 2);
39 
40   int i = 0;
41   A (ca[i], 5);
42   A (&ca[i][0], 5);
43   A (&ca[i][1], 4);
44   A (&ca[i][3], 2);
45 
46   int j = i;
47   A (&ca[i][i], 5);
48   A (&ca[i][j + 1], 4);
49   A (&ca[i][j + 2], 3);
50 
51   A (&ca[idx][i], 5);
52   A (&ca[idx][j + 1], 4);
53   A (&ca[idx][j + 2], 3);
54 
55   A (&ca[idx][idx], 5);
56   A (&ca[idx][idx + 1], 4);
57   A (&ca[idx][idx + 2], 3);
58 
59   A (&ca[0][++j], 4);
60   A (&ca[0][++j], 3);
61   A (&ca[0][++j], 2);
62 
63   if (j != 3)
64     ++nfails;
65 }
66 
67 
test_const_local_arrays(void)68 static void test_const_local_arrays (void)
69 {
70   const char a[][4] = {
71     { '1', '2', '3', '4' }, { '5' },
72     { '1', '2', '3', '4' }, { '5', '6' },
73     { '1', '2', '3', '4' }, { '5', '6', '7' },
74     { '1', '2', '3', '4' }, { '5', '6', '7', '8' },
75     { '9' }
76   };
77 
78   A (a[0], 5);
79   A (&a[0][0], 5);
80   A (&a[0][1], 4);
81   A (&a[0][3], 2);
82 
83   int i = 0;
84   A (a[i], 5);
85   A (&a[i][0], 5);
86   A (&a[i][1], 4);
87   A (&a[i][3], 2);
88 
89   int j = i;
90   A (&a[i][i], 5);
91   A (&a[i][j + 1], 4);
92   A (&a[i][j + 2], 3);
93 
94   A (&a[idx][i], 5);
95   A (&a[idx][j + 1], 4);
96   A (&a[idx][j + 2], 3);
97 
98   A (&a[idx][idx], 5);
99   A (&a[idx][idx + 1], 4);
100   A (&a[idx][idx + 2], 3);
101 
102   A (&a[0][++j], 4);
103   A (&a[0][++j], 3);
104   A (&a[0][++j], 2);
105 
106   if (j != 3)
107     ++nfails;
108 }
109 
110 
111 char va[][4] = {
112   { '1', '2', '3', '4' }, { '5' },
113   { '1', '2', '3', '4' }, { '5', '6' },
114   { '1', '2', '3', '4' }, { '5', '6', '7' },
115   { '1', '2', '3', '4' }, { '5', '6', '7', '8' },
116   { '9' }
117 };
118 
test_nonconst_global_arrays(void)119 static void test_nonconst_global_arrays (void)
120 {
121   {
122     A (va[0], 5);
123     A (&va[0][0], 5);
124     A (&va[0][1], 4);
125     A (&va[0][3], 2);
126 
127     int i = 0;
128     A (va[i], 5);
129     A (&va[i][0], 5);
130     A (&va[i][1], 4);
131     A (&va[i][3], 2);
132 
133     int j = i;
134     A (&va[i][i], 5);
135     A (&va[i][j + 1], 4);
136     A (&va[i][j + 2], 3);
137 
138     A (&va[idx][i], 5);
139     A (&va[idx][j + 1], 4);
140     A (&va[idx][j + 2], 3);
141 
142     A (&va[idx][idx], 5);
143     A (&va[idx][idx + 1], 4);
144     A (&va[idx][idx + 2], 3);
145   }
146 
147   {
148     A (va[2], 6);
149     A (&va[2][0], 6);
150     A (&va[2][1], 5);
151     A (&va[2][3], 3);
152 
153     int i = 2;
154     A (va[i], 6);
155     A (&va[i][0], 6);
156     A (&va[i][1], 5);
157     A (&va[i][3], 3);
158 
159     int j = i - 1;
160     A (&va[i][j - 1], 6);
161     A (&va[i][j], 5);
162     A (&va[i][j + 1], 4);
163 
164     A (&va[idx + 2][i - 1], 5);
165     A (&va[idx + 2][j], 5);
166     A (&va[idx + 2][j + 1], 4);
167   }
168 
169   int j = 0;
170 
171   A (&va[0][++j], 4);
172   A (&va[0][++j], 3);
173   A (&va[0][++j], 2);
174 
175   if (j != 3)
176     ++nfails;
177 }
178 
179 
test_nonconst_local_arrays(void)180 static void test_nonconst_local_arrays (void)
181 {
182   char a[][4] = {
183     { '1', '2', '3', '4' }, { '5' },
184     { '1', '2', '3', '4' }, { '5', '6' },
185     { '1', '2', '3', '4' }, { '5', '6', '7' },
186     { '1', '2', '3', '4' }, { '5', '6', '7', '8' },
187     { '9' }
188   };
189 
190   A (a[0], 5);
191   A (&a[0][0], 5);
192   A (&a[0][1], 4);
193   A (&a[0][3], 2);
194 
195   int i = 0;
196   A (a[i], 5);
197   A (&a[i][0], 5);
198   A (&a[i][1], 4);
199   A (&a[i][3], 2);
200 
201   int j = i;
202   A (&a[i][i], 5);
203   A (&a[i][j + 1], 4);
204   A (&a[i][j + 2], 3);
205 
206   A (&a[idx][i], 5);
207   A (&a[idx][j + 1], 4);
208   A (&a[idx][j + 2], 3);
209 
210   A (&a[idx][idx], 5);
211   A (&a[idx][idx + 1], 4);
212   A (&a[idx][idx + 2], 3);
213 
214   A (&a[0][++j], 4);
215   A (&a[0][++j], 3);
216   A (&a[0][++j], 2);
217 
218   if (j != 3)
219     ++nfails;
220 }
221 
222 
223 struct MemArrays { char a[4], b[4]; };
224 
225 const struct MemArrays cma[] = {
226   { { '1', '2', '3', '4' }, { '5' } },
227   { { '1', '2', '3', '4' }, { '5', '6' } },
228   { { '1', '2', '3', '4' }, { '5', '6' } },
229   { { '1', '2', '3', '4' }, { '5', '6', '7' } },
230   { { '1', '2', '3', '4' }, { '5', '6', '7', '8' } },
231   { { '9' }, { '\0' } }
232 };
233 
test_const_global_member_arrays(void)234 static void test_const_global_member_arrays (void)
235 {
236   {
237     A (cma[0].a, 5);
238     A (&cma[0].a[0], 5);
239     A (&cma[0].a[1], 4);
240     A (&cma[0].a[2], 3);
241 
242     int i = 0;
243     A (cma[i].a, 5);
244     A (&cma[i].a[0], 5);
245     A (&cma[i].a[1], 4);
246     A (&cma[i].a[2], 3);
247 
248     int j = i;
249     A (&cma[i].a[j], 5);
250     A (&cma[i].a[j + 1], 4);
251     A (&cma[i].a[j + 2], 3);
252 
253     A (&cma[idx].a[i], 5);
254     A (&cma[idx].a[j + 1], 4);
255     A (&cma[idx].a[j + 2], 3);
256 
257     A (&cma[idx].a[idx], 5);
258     A (&cma[idx].a[idx + 1], 4);
259     A (&cma[idx].a[idx + 2], 3);
260   }
261 
262   {
263     A (cma[1].a, 6);
264     A (&cma[1].a[0], 6);
265     A (&cma[1].a[1], 5);
266     A (&cma[1].a[2], 4);
267 
268     int i = 1;
269     A (cma[i].a, 6);
270     A (&cma[i].a[0], 6);
271     A (&cma[i].a[1], 5);
272     A (&cma[i].a[2], 4);
273 
274     int j = i - 1;
275     A (&cma[i].a[j], 6);
276     A (&cma[i].a[j + 1], 5);
277     A (&cma[i].a[j + 2], 4);
278 
279     A (&cma[idx + 1].a[j], 6);
280     A (&cma[idx + 1].a[j + 1], 5);
281     A (&cma[idx + 1].a[j + 2], 4);
282 
283     A (&cma[idx + 1].a[idx], 6);
284     A (&cma[idx + 1].a[idx + 1], 5);
285     A (&cma[idx + 1].a[idx + 2], 4);
286   }
287 
288   {
289     A (cma[4].a, 9);
290     A (&cma[4].a[0], 9);
291     A (&cma[4].a[1], 8);
292     A (&cma[4].b[0], 5);
293 
294     int i = 4;
295     A (cma[i].a, 9);
296     A (&cma[i].a[0], 9);
297     A (&cma[i].a[1], 8);
298     A (&cma[i].b[0], 5);
299 
300     int j = i - 1;
301     A (&cma[i].a[j], 6);
302     A (&cma[i].a[j + 1], 5);
303     A (&cma[i].b[j - 2], 4);
304 
305     A (&cma[idx + 4].a[j], 6);
306     A (&cma[idx + 4].a[j + 1], 5);
307     A (&cma[idx + 4].b[j - 2], 4);
308 
309     A (&cma[idx + 4].a[idx], 9);
310     A (&cma[idx + 4].a[idx + 1], 8);
311     A (&cma[idx + 4].b[idx + 1], 4);
312   }
313 }
314 
315 
test_const_local_member_arrays(void)316 static void test_const_local_member_arrays (void)
317 {
318   const struct MemArrays ma[] = {
319     { { '1', '2', '3', '4' }, { '5' } },
320     { { '1', '2', '3', '4' }, { '5', '6' } },
321     { { '1', '2', '3', '4' }, { '5', '6' } },
322     { { '1', '2', '3', '4' }, { '5', '6', '7' } },
323     { { '1', '2', '3', '4' }, { '5', '6', '7', '8' } },
324     { { '9' }, { '\0' } }
325   };
326 
327   {
328     A (ma[0].a, 5);
329     A (&ma[0].a[0], 5);
330     A (&ma[0].a[1], 4);
331     A (&ma[0].a[2], 3);
332 
333     int i = 0;
334     A (ma[i].a, 5);
335     A (&ma[i].a[0], 5);
336     A (&ma[i].a[1], 4);
337     A (&ma[i].a[2], 3);
338 
339     int j = i;
340     A (&ma[i].a[j], 5);
341     A (&ma[i].a[j + 1], 4);
342     A (&ma[i].a[j + 2], 3);
343 
344     A (&ma[idx].a[i], 5);
345     A (&ma[idx].a[j + 1], 4);
346     A (&ma[idx].a[j + 2], 3);
347 
348     A (&ma[idx].a[idx], 5);
349     A (&ma[idx].a[idx + 1], 4);
350     A (&ma[idx].a[idx + 2], 3);
351   }
352 
353   {
354     A (ma[1].a, 6);
355     A (&ma[1].a[0], 6);
356     A (&ma[1].a[1], 5);
357     A (&ma[1].a[2], 4);
358 
359     int i = 1;
360     A (ma[i].a, 6);
361     A (&ma[i].a[0], 6);
362     A (&ma[i].a[1], 5);
363     A (&ma[i].a[2], 4);
364 
365     int j = i - 1;
366     A (&ma[i].a[j], 6);
367     A (&ma[i].a[j + 1], 5);
368     A (&ma[i].a[j + 2], 4);
369 
370     A (&ma[idx + 1].a[j], 6);
371     A (&ma[idx + 1].a[j + 1], 5);
372     A (&ma[idx + 1].a[j + 2], 4);
373 
374     A (&ma[idx + 1].a[idx], 6);
375     A (&ma[idx + 1].a[idx + 1], 5);
376     A (&ma[idx + 1].a[idx + 2], 4);
377   }
378 
379   {
380     A (ma[4].a, 9);
381     A (&ma[4].a[0], 9);
382     A (&ma[4].a[1], 8);
383     A (&ma[4].b[0], 5);
384 
385     int i = 4;
386     A (ma[i].a, 9);
387     A (&ma[i].a[0], 9);
388     A (&ma[i].a[1], 8);
389     A (&ma[i].b[0], 5);
390 
391     int j = i - 1;
392     A (&ma[i].a[j], 6);
393     A (&ma[i].a[j + 1], 5);
394     A (&ma[i].b[j - 2], 4);
395 
396     A (&ma[idx + 4].a[j], 6);
397     A (&ma[idx + 4].a[j + 1], 5);
398     A (&ma[idx + 4].b[j - 2], 4);
399 
400     A (&ma[idx + 4].a[idx], 9);
401     A (&ma[idx + 4].a[idx + 1], 8);
402     A (&ma[idx + 4].b[idx + 1], 4);
403   }
404 }
405 
406 struct MemArrays vma[] = {
407   { { '1', '2', '3', '4' }, { '5' } },
408   { { '1', '2', '3', '4' }, { '5', '6' } },
409   { { '1', '2', '3', '4' }, { '5', '6' } },
410   { { '1', '2', '3', '4' }, { '5', '6', '7' } },
411   { { '1', '2', '3', '4' }, { '5', '6', '7', '8' } },
412   { { '9' }, { '\0' } }
413 };
414 
test_nonconst_global_member_arrays(void)415 static void test_nonconst_global_member_arrays (void)
416 {
417   {
418     A (vma[0].a, 5);
419     A (&vma[0].a[0], 5);
420     A (&vma[0].a[1], 4);
421     A (&vma[0].a[2], 3);
422 
423     int i = 0;
424     A (vma[i].a, 5);
425     A (&vma[i].a[0], 5);
426     A (&vma[i].a[1], 4);
427     A (&vma[i].a[2], 3);
428 
429     int j = i;
430     A (&vma[i].a[j], 5);
431     A (&vma[i].a[j + 1], 4);
432     A (&vma[i].a[j + 2], 3);
433 
434     A (&vma[idx].a[i], 5);
435     A (&vma[idx].a[j + 1], 4);
436     A (&vma[idx].a[j + 2], 3);
437 
438     A (&vma[idx].a[idx], 5);
439     A (&vma[idx].a[idx + 1], 4);
440     A (&vma[idx].a[idx + 2], 3);
441   }
442 
443   {
444     A (vma[1].a, 6);
445     A (&vma[1].a[0], 6);
446     A (&vma[1].a[1], 5);
447     A (&vma[1].a[2], 4);
448 
449     int i = 1;
450     A (vma[i].a, 6);
451     A (&vma[i].a[0], 6);
452     A (&vma[i].a[1], 5);
453     A (&vma[i].a[2], 4);
454 
455     int j = i - 1;
456     A (&vma[i].a[j], 6);
457     A (&vma[i].a[j + 1], 5);
458     A (&vma[i].a[j + 2], 4);
459 
460     A (&vma[idx + 1].a[j], 6);
461     A (&vma[idx + 1].a[j + 1], 5);
462     A (&vma[idx + 1].a[j + 2], 4);
463 
464     A (&vma[idx + 1].a[idx], 6);
465     A (&vma[idx + 1].a[idx + 1], 5);
466     A (&vma[idx + 1].a[idx + 2], 4);
467   }
468 
469   {
470     A (vma[4].a, 9);
471     A (&vma[4].a[0], 9);
472     A (&vma[4].a[1], 8);
473     A (&vma[4].b[0], 5);
474 
475     int i = 4;
476     A (vma[i].a, 9);
477     A (&vma[i].a[0], 9);
478     A (&vma[i].a[1], 8);
479     A (&vma[i].b[0], 5);
480 
481     int j = i - 1;
482     A (&vma[i].a[j], 6);
483     A (&vma[i].a[j + 1], 5);
484     A (&vma[i].b[j - 2], 4);
485 
486     A (&vma[idx + 4].a[j], 6);
487     A (&vma[idx + 4].a[j + 1], 5);
488     A (&vma[idx + 4].b[j - 2], 4);
489 
490     A (&vma[idx + 4].a[idx], 9);
491     A (&vma[idx + 4].a[idx + 1], 8);
492     A (&vma[idx + 4].b[idx + 1], 4);
493   }
494 }
495 
496 
test_nonconst_local_member_arrays(void)497 static void test_nonconst_local_member_arrays (void)
498 {
499   struct MemArrays ma[] = {
500     { { '1', '2', '3', '4' }, { '5' } },
501     { { '1', '2', '3', '4' }, { '5', '6' } },
502     { { '1', '2', '3', '4' }, { '5', '6' } },
503     { { '1', '2', '3', '4' }, { '5', '6', '7' } },
504     { { '1', '2', '3', '4' }, { '5', '6', '7', '8' } },
505     { { '9' }, { '\0' } }
506   };
507 
508   {
509     A (ma[0].a, 5);
510     A (&ma[0].a[0], 5);
511     A (&ma[0].a[1], 4);
512     A (&ma[0].a[2], 3);
513 
514     int i = 0;
515     A (ma[i].a, 5);
516     A (&ma[i].a[0], 5);
517     A (&ma[i].a[1], 4);
518     A (&ma[i].a[2], 3);
519 
520     int j = i;
521     A (&ma[i].a[j], 5);
522     A (&ma[i].a[j + 1], 4);
523     A (&ma[i].a[j + 2], 3);
524 
525     A (&ma[idx].a[i], 5);
526     A (&ma[idx].a[j + 1], 4);
527     A (&ma[idx].a[j + 2], 3);
528 
529     A (&ma[idx].a[idx], 5);
530     A (&ma[idx].a[idx + 1], 4);
531     A (&ma[idx].a[idx + 2], 3);
532   }
533 
534   {
535     A (ma[1].a, 6);
536     A (&ma[1].a[0], 6);
537     A (&ma[1].a[1], 5);
538     A (&ma[1].a[2], 4);
539 
540     int i = 1;
541     A (ma[i].a, 6);
542     A (&ma[i].a[0], 6);
543     A (&ma[i].a[1], 5);
544     A (&ma[i].a[2], 4);
545 
546     int j = i - 1;
547     A (&ma[i].a[j], 6);
548     A (&ma[i].a[j + 1], 5);
549     A (&ma[i].a[j + 2], 4);
550 
551     A (&ma[idx + 1].a[j], 6);
552     A (&ma[idx + 1].a[j + 1], 5);
553     A (&ma[idx + 1].a[j + 2], 4);
554 
555     A (&ma[idx + 1].a[idx], 6);
556     A (&ma[idx + 1].a[idx + 1], 5);
557     A (&ma[idx + 1].a[idx + 2], 4);
558   }
559 
560   {
561     A (ma[4].a, 9);
562     A (&ma[4].a[0], 9);
563     A (&ma[4].a[1], 8);
564     A (&ma[4].b[0], 5);
565 
566     int i = 4;
567     A (ma[i].a, 9);
568     A (&ma[i].a[0], 9);
569     A (&ma[i].a[1], 8);
570     A (&ma[i].b[0], 5);
571 
572     int j = i - 1;
573     A (&ma[i].a[j], 6);
574     A (&ma[i].a[j + 1], 5);
575     A (&ma[i].b[j - 2], 4);
576 
577     A (&ma[idx + 4].a[j], 6);
578     A (&ma[idx + 4].a[j + 1], 5);
579     A (&ma[idx + 4].b[j - 2], 4);
580 
581     A (&ma[idx + 4].a[idx], 9);
582     A (&ma[idx + 4].a[idx + 1], 8);
583     A (&ma[idx + 4].b[idx + 1], 4);
584   }
585 }
586 
587 
588 union UnionMemberArrays
589 {
590   struct { char a[4], b[4]; } a;
591   struct { char a[8]; } c;
592 };
593 
594 const union UnionMemberArrays cu = {
595   { { '1', '2', '3', '4' }, { '5', } }
596 };
597 
test_const_union_member_arrays(void)598 static void test_const_union_member_arrays (void)
599 {
600   A (cu.a.a, 5);
601   A (cu.a.b, 1);
602   A (cu.c.a, 5);
603 
604   const union UnionMemberArrays clu = {
605     { { '1', '2', '3', '4' }, { '5', '6' } }
606   };
607 
608   A (clu.a.a, 6);
609   A (clu.a.b, 2);
610   A (clu.c.a, 6);
611 }
612 
613 
614 union UnionMemberArrays vu = {
615   { { '1', '2', '3', '4' }, { '5', '6' } }
616 };
617 
test_nonconst_union_member_arrays(void)618 static void test_nonconst_union_member_arrays (void)
619 {
620   A (vu.a.a, 6);
621   A (vu.a.b, 2);
622   A (vu.c.a, 6);
623 
624   union UnionMemberArrays lvu = {
625     { { '1', '2', '3', '4' }, { '5', '6', '7' } }
626   };
627 
628   A (lvu.a.a, 7);
629   A (lvu.a.b, 3);
630   A (lvu.c.a, 7);
631 }
632 
633 
main(void)634 int main (void)
635 {
636   test_const_global_arrays ();
637   test_const_local_arrays ();
638 
639   test_nonconst_global_arrays ();
640   test_nonconst_local_arrays ();
641 
642   test_const_global_member_arrays ();
643   test_const_local_member_arrays ();
644 
645   test_nonconst_global_member_arrays ();
646   test_nonconst_local_member_arrays ();
647 
648   test_const_union_member_arrays ();
649   test_nonconst_union_member_arrays ();
650 
651   if (nfails)
652     __builtin_abort ();
653 }
654