xref: /netbsd-src/tests/usr.bin/xlint/lint1/msg_358.c (revision ce2046810387440872bfc20878728c18e4f27f6c)
1 /*	$NetBSD: msg_358.c,v 1.4 2024/11/05 06:23:04 rillig Exp $	*/
2 # 3 "msg_358.c"
3 
4 // Test for message: hex escape '%.*s' has more than 2 digits [358]
5 
6 /*
7  * In the format argument of the snprintb and snprintb_m functions, a bit
8  * position or field width is written as an octal or hexadecimal escape
9  * sequence.  If the description that follows a hexadecimal escape sequence
10  * starts with hexadecimal digits (A-Fa-f), these digits are still part of the
11  * escape sequence instead of the description.
12  *
13  * All platforms supported by lint have 8-bit char, so using more than the
14  * maximum necessary 2 hexadecimal digits in an escape sequence is suspicious
15  * of being unintended.
16  */
17 
18 /* lint1-extra-flags: -X 351 */
19 
20 typedef typeof(sizeof(0)) size_t;
21 typedef unsigned long long uint64_t;
22 
23 int snprintb(char *, size_t, const char *, uint64_t);
24 
25 void
26 examples(unsigned u32, uint64_t u64)
27 {
28 	char buf[64];
29 
30 	/* expect+3: warning: hex escape '\x01B' has more than 2 digits [358] */
31 	snprintb(buf, sizeof(buf),
32 	    "\020\x01BIT",
33 	    u32);
34 
35 	/* expect+3: warning: hex escape '\x01b' has more than 2 digits [358] */
36 	snprintb(buf, sizeof(buf),
37 	    "\020\x01bit",
38 	    u32);
39 
40 	// This mismatch goes undetected as it has only 2 digits, does not mix
41 	// case and is in bounds.  A spellchecker could mark the unknown word
42 	// 'ield' to give a hint.
43 	snprintb(buf, sizeof(buf),
44 	    "\020\x1FIELD",
45 	    u32);
46 
47 	/* expect+3: warning: hex escape '\x01b' has more than 2 digits [358] */
48 	snprintb(buf, sizeof(buf),
49 	    "\177\020b\x01bit\0",
50 	    u64);
51 
52 	/* expect+3: warning: hex escape '\x02b' has more than 2 digits [358] */
53 	snprintb(buf, sizeof(buf),
54 	    "\177\020f\x00\x02bit\0",
55 	    u64);
56 
57 	// In this example from the snprintb manual page, the descriptions
58 	// that start with a hexadecimal digit must be separated from the
59 	// hexadecimal escape sequence for the bit position.
60 	snprintb(buf, sizeof(buf),
61 	    "\20\x10NOTBOOT\x0f" "FPP\x0eSDVMA\x0cVIDEO"
62 	    "\x0bLORES\x0a" "FPA\x09" "DIAG\x07" "CACHE"
63 	    "\x06IOCACHE\x05LOOPBACK\x04" "DBGCACHE",
64 	    u32);
65 }
66