msg_168.c revision 1.6
1/*	$NetBSD: msg_168.c,v 1.6 2022/05/30 08:04:00 rillig Exp $	*/
2# 3 "msg_168.c"
3
4// Test for message: array subscript cannot be > %d: %ld [168]
5
6void print_string(const char *);
7void print_char(char);
8
9void
10example(void)
11{
12	char buf[20] = {};	/* empty initializer is a GCC extension */
13
14	print_string(buf + 19);	/* inside the array */
15
16	/*
17	 * It is valid to point at the end of the array, but reading a
18	 * character from there invokes undefined behavior.
19	 *
20	 * The pointer to the end of the array is typically used in (begin,
21	 * end) tuples.  These are more common in C++ than in C though.
22	 */
23	print_string(buf + 20);
24
25	print_string(buf + 21);	/* undefined behavior, not detected */
26
27	print_char(buf[19]);
28	print_char(buf[20]);	/* expect: 168 */
29}
30
31void
32array_with_c99_initializer(void)
33{
34	static const char *const to_roman[] = {
35	    ['0'] = "undefined",
36	    ['5'] = "V",
37	    ['9'] = "IX"
38	};
39
40	print_string(to_roman['9']);
41	print_string(to_roman[':']);	/* expect: 168 */
42}
43
44
45struct s {
46	char offset_0;
47	char offset_1;
48	int offset_4;
49	short offset_8;
50	char offset_10;
51};
52
53struct s
54s_init(void)
55{
56	struct s s[1];
57	s->offset_0 = 1;
58	/* expect+1: warning: array subscript cannot be > 0: 1 [168] */
59	s->offset_1 = 2;
60	/* expect+1: warning: array subscript cannot be > 0: 4 [168] */
61	s->offset_4 = 3;
62	/* expect+1: warning: array subscript cannot be > 0: 8 [168] */
63	s->offset_8 = 4;
64	/* expect+1: warning: array subscript cannot be > 0: 10 [168] */
65	s->offset_10 = 5;
66	return s[0];
67}
68