xref: /netbsd-src/tests/usr.bin/xlint/lint1/msg_168.c (revision 82d56013d7b633d116a93943de88e08335357a7c)
1 /*	$NetBSD: msg_168.c,v 1.5 2021/03/25 22:53:05 rillig Exp $	*/
2 # 3 "msg_168.c"
3 
4 // Test for message: array subscript cannot be > %d: %ld [168]
5 
6 void print_string(const char *);
7 void print_char(char);
8 
9 void
10 example(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 
31 void
32 array_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