xref: /netbsd-src/tests/usr.bin/xlint/lint1/msg_357.c (revision ce2046810387440872bfc20878728c18e4f27f6c)
1 /*	$NetBSD: msg_357.c,v 1.3 2024/11/05 06:23:04 rillig Exp $	*/
2 # 3 "msg_357.c"
3 
4 // Test for message: hex escape '%.*s' mixes uppercase and lowercase digits [357]
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  * Since the escape sequences are typically written in lowercase and the
14  * descriptions are typically written in uppercase, a mixture of both cases
15  * indicates a mismatch.
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+5: warning: hex escape '\x0aB' mixes uppercase and lowercase digits [357] */
31 	/* expect+4: warning: hex escape '\x0aB' has more than 2 digits [358] */
32 	/* expect+3: warning: bit position '\x0aB' (171) in '\x0aBIT' out of range 1..32 [371] */
33 	snprintb(buf, sizeof(buf),
34 	    "\020\x0aBIT",
35 	    u32);
36 
37 	// This mismatch goes undetected as it has only 2 digits, does not mix
38 	// case and is in bounds.  A spellchecker could mark the unknown word
39 	// 'ield' to give a hint.
40 	snprintb(buf, sizeof(buf),
41 	    "\020\x1FIELD",
42 	    u32);
43 
44 	// If the input value is restricted further, the unintended hexadecimal
45 	// escape sequence is detected, although with a less obvious message.
46 	/* expect+3: warning: conversion '\x1FIELD' is unreachable by input value [378] */
47 	snprintb(buf, sizeof(buf),
48 	    "\020\x1FIELD",
49 	    u32 & 0xffff);
50 
51 	/* expect+5: warning: hex escape '\x0aB' mixes uppercase and lowercase digits [357] */
52 	/* expect+4: warning: hex escape '\x0aB' has more than 2 digits [358] */
53 	/* expect+3: warning: bit position '\x0aB' (171) in 'b\x0aBIT\0' out of range 0..63 [371] */
54 	snprintb(buf, sizeof(buf),
55 	    "\177\020b\x0aBIT\0",
56 	    u64);
57 }
58