xref: /netbsd-src/tests/usr.bin/xlint/lint1/msg_357.c (revision ce2046810387440872bfc20878728c18e4f27f6c)
1*ce204681Srillig /*	$NetBSD: msg_357.c,v 1.3 2024/11/05 06:23:04 rillig Exp $	*/
238c0bdf2Srillig # 3 "msg_357.c"
338c0bdf2Srillig 
438c0bdf2Srillig // Test for message: hex escape '%.*s' mixes uppercase and lowercase digits [357]
538c0bdf2Srillig 
638c0bdf2Srillig /*
738c0bdf2Srillig  * In the format argument of the snprintb and snprintb_m functions, a bit
838c0bdf2Srillig  * position or field width is written as an octal or hexadecimal escape
9*ce204681Srillig  * sequence.  If the description that follows a hexadecimal escape sequence
10*ce204681Srillig  * starts with hexadecimal digits (A-Fa-f), these digits are still part of the
11*ce204681Srillig  * escape sequence instead of the description.
1238c0bdf2Srillig  *
1338c0bdf2Srillig  * Since the escape sequences are typically written in lowercase and the
1438c0bdf2Srillig  * descriptions are typically written in uppercase, a mixture of both cases
1538c0bdf2Srillig  * indicates a mismatch.
1638c0bdf2Srillig  */
1738c0bdf2Srillig 
1838c0bdf2Srillig /* lint1-extra-flags: -X 351 */
1938c0bdf2Srillig 
2038c0bdf2Srillig typedef typeof(sizeof(0)) size_t;
2138c0bdf2Srillig typedef unsigned long long uint64_t;
2238c0bdf2Srillig 
2338c0bdf2Srillig int snprintb(char *, size_t, const char *, uint64_t);
2438c0bdf2Srillig 
2538c0bdf2Srillig void
2638c0bdf2Srillig examples(unsigned u32, uint64_t u64)
2738c0bdf2Srillig {
2838c0bdf2Srillig 	char buf[64];
2938c0bdf2Srillig 
3038c0bdf2Srillig 	/* expect+5: warning: hex escape '\x0aB' mixes uppercase and lowercase digits [357] */
3138c0bdf2Srillig 	/* expect+4: warning: hex escape '\x0aB' has more than 2 digits [358] */
3238c0bdf2Srillig 	/* expect+3: warning: bit position '\x0aB' (171) in '\x0aBIT' out of range 1..32 [371] */
3338c0bdf2Srillig 	snprintb(buf, sizeof(buf),
3438c0bdf2Srillig 	    "\020\x0aBIT",
3538c0bdf2Srillig 	    u32);
3638c0bdf2Srillig 
3738c0bdf2Srillig 	// This mismatch goes undetected as it has only 2 digits, does not mix
3838c0bdf2Srillig 	// case and is in bounds.  A spellchecker could mark the unknown word
3938c0bdf2Srillig 	// 'ield' to give a hint.
4038c0bdf2Srillig 	snprintb(buf, sizeof(buf),
4138c0bdf2Srillig 	    "\020\x1FIELD",
4238c0bdf2Srillig 	    u32);
4338c0bdf2Srillig 
44*ce204681Srillig 	// If the input value is restricted further, the unintended hexadecimal
45*ce204681Srillig 	// escape sequence is detected, although with a less obvious message.
46*ce204681Srillig 	/* expect+3: warning: conversion '\x1FIELD' is unreachable by input value [378] */
47*ce204681Srillig 	snprintb(buf, sizeof(buf),
48*ce204681Srillig 	    "\020\x1FIELD",
49*ce204681Srillig 	    u32 & 0xffff);
50*ce204681Srillig 
5138c0bdf2Srillig 	/* expect+5: warning: hex escape '\x0aB' mixes uppercase and lowercase digits [357] */
5238c0bdf2Srillig 	/* expect+4: warning: hex escape '\x0aB' has more than 2 digits [358] */
5338c0bdf2Srillig 	/* expect+3: warning: bit position '\x0aB' (171) in 'b\x0aBIT\0' out of range 0..63 [371] */
5438c0bdf2Srillig 	snprintb(buf, sizeof(buf),
5538c0bdf2Srillig 	    "\177\020b\x0aBIT\0",
5638c0bdf2Srillig 	    u64);
5738c0bdf2Srillig }
58