Module Name:    src
Committed By:   rillig
Date:           Tue Nov  5 06:23:04 UTC 2024

Modified Files:
        src/tests/usr.bin/xlint/lint1: msg_357.c msg_358.c msg_359.c msg_360.c
            msg_361.c msg_363.c

Log Message:
tests/lint: extend snprintb tests and improve their documentation


To generate a diff of this commit:
cvs rdiff -u -r1.2 -r1.3 src/tests/usr.bin/xlint/lint1/msg_357.c \
    src/tests/usr.bin/xlint/lint1/msg_359.c
cvs rdiff -u -r1.3 -r1.4 src/tests/usr.bin/xlint/lint1/msg_358.c \
    src/tests/usr.bin/xlint/lint1/msg_360.c \
    src/tests/usr.bin/xlint/lint1/msg_361.c
cvs rdiff -u -r1.6 -r1.7 src/tests/usr.bin/xlint/lint1/msg_363.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/tests/usr.bin/xlint/lint1/msg_357.c
diff -u src/tests/usr.bin/xlint/lint1/msg_357.c:1.2 src/tests/usr.bin/xlint/lint1/msg_357.c:1.3
--- src/tests/usr.bin/xlint/lint1/msg_357.c:1.2	Sat Aug 31 06:57:31 2024
+++ src/tests/usr.bin/xlint/lint1/msg_357.c	Tue Nov  5 06:23:04 2024
@@ -1,4 +1,4 @@
-/*	$NetBSD: msg_357.c,v 1.2 2024/08/31 06:57:31 rillig Exp $	*/
+/*	$NetBSD: msg_357.c,v 1.3 2024/11/05 06:23:04 rillig Exp $	*/
 # 3 "msg_357.c"
 
 // Test for message: hex escape '%.*s' mixes uppercase and lowercase digits [357]
@@ -6,9 +6,9 @@
 /*
  * In the format argument of the snprintb and snprintb_m functions, a bit
  * position or field width is written as an octal or hexadecimal escape
- * sequence.  If the description that follows starts with hex digits (A-Fa-f),
- * these digits are still part of the escape sequence instead of the
- * description.
+ * sequence.  If the description that follows a hexadecimal escape sequence
+ * starts with hexadecimal digits (A-Fa-f), these digits are still part of the
+ * escape sequence instead of the description.
  *
  * Since the escape sequences are typically written in lowercase and the
  * descriptions are typically written in uppercase, a mixture of both cases
@@ -41,6 +41,13 @@ examples(unsigned u32, uint64_t u64)
 	    "\020\x1FIELD",
 	    u32);
 
+	// If the input value is restricted further, the unintended hexadecimal
+	// escape sequence is detected, although with a less obvious message.
+	/* expect+3: warning: conversion '\x1FIELD' is unreachable by input value [378] */
+	snprintb(buf, sizeof(buf),
+	    "\020\x1FIELD",
+	    u32 & 0xffff);
+
 	/* expect+5: warning: hex escape '\x0aB' mixes uppercase and lowercase digits [357] */
 	/* expect+4: warning: hex escape '\x0aB' has more than 2 digits [358] */
 	/* expect+3: warning: bit position '\x0aB' (171) in 'b\x0aBIT\0' out of range 0..63 [371] */
Index: src/tests/usr.bin/xlint/lint1/msg_359.c
diff -u src/tests/usr.bin/xlint/lint1/msg_359.c:1.2 src/tests/usr.bin/xlint/lint1/msg_359.c:1.3
--- src/tests/usr.bin/xlint/lint1/msg_359.c:1.2	Sat Aug 31 06:57:31 2024
+++ src/tests/usr.bin/xlint/lint1/msg_359.c	Tue Nov  5 06:23:04 2024
@@ -1,11 +1,13 @@
-/*	$NetBSD: msg_359.c,v 1.2 2024/08/31 06:57:31 rillig Exp $	*/
+/*	$NetBSD: msg_359.c,v 1.3 2024/11/05 06:23:04 rillig Exp $	*/
 # 3 "msg_359.c"
 
 // Test for message: missing new-style '\177' or old-style number base [359]
 
 /*
  * The first or second character of the snprintb format specifies the number
- * base.  It must be given in binary.
+ * base.  It must be an octal or hexadecimal escape sequence, as the characters
+ * 2, 10 and 16 are not printable, and writing '\n' instead of '\x0a' would be
+ * misleading.
  */
 
 /* lint1-extra-flags: -X 351 */

Index: src/tests/usr.bin/xlint/lint1/msg_358.c
diff -u src/tests/usr.bin/xlint/lint1/msg_358.c:1.3 src/tests/usr.bin/xlint/lint1/msg_358.c:1.4
--- src/tests/usr.bin/xlint/lint1/msg_358.c:1.3	Sat Aug 31 06:57:31 2024
+++ src/tests/usr.bin/xlint/lint1/msg_358.c	Tue Nov  5 06:23:04 2024
@@ -1,4 +1,4 @@
-/*	$NetBSD: msg_358.c,v 1.3 2024/08/31 06:57:31 rillig Exp $	*/
+/*	$NetBSD: msg_358.c,v 1.4 2024/11/05 06:23:04 rillig Exp $	*/
 # 3 "msg_358.c"
 
 // Test for message: hex escape '%.*s' has more than 2 digits [358]
@@ -6,9 +6,13 @@
 /*
  * In the format argument of the snprintb and snprintb_m functions, a bit
  * position or field width is written as an octal or hexadecimal escape
- * sequence.  If the description that follows starts with hex digits (A-Fa-f),
- * these digits are still part of the escape sequence instead of the
- * description.
+ * sequence.  If the description that follows a hexadecimal escape sequence
+ * starts with hexadecimal digits (A-Fa-f), these digits are still part of the
+ * escape sequence instead of the description.
+ *
+ * All platforms supported by lint have 8-bit char, so using more than the
+ * maximum necessary 2 hexadecimal digits in an escape sequence is suspicious
+ * of being unintended.
  */
 
 /* lint1-extra-flags: -X 351 */
Index: src/tests/usr.bin/xlint/lint1/msg_360.c
diff -u src/tests/usr.bin/xlint/lint1/msg_360.c:1.3 src/tests/usr.bin/xlint/lint1/msg_360.c:1.4
--- src/tests/usr.bin/xlint/lint1/msg_360.c:1.3	Sat Aug 31 06:57:31 2024
+++ src/tests/usr.bin/xlint/lint1/msg_360.c	Tue Nov  5 06:23:04 2024
@@ -1,12 +1,12 @@
-/*	$NetBSD: msg_360.c,v 1.3 2024/08/31 06:57:31 rillig Exp $	*/
+/*	$NetBSD: msg_360.c,v 1.4 2024/11/05 06:23:04 rillig Exp $	*/
 # 3 "msg_360.c"
 
 // Test for message: missing new-style number base after '\177' [360]
 
 /*
  * The new-style format requires the number base as the second character.
- * This check is merely a byproduct of the implementation, it does not provide
- * much value on its own.
+ * This check is merely a byproduct of the implementation, it provides little
+ * value of its own.
  */
 
 /* lint1-extra-flags: -X 351 */
Index: src/tests/usr.bin/xlint/lint1/msg_361.c
diff -u src/tests/usr.bin/xlint/lint1/msg_361.c:1.3 src/tests/usr.bin/xlint/lint1/msg_361.c:1.4
--- src/tests/usr.bin/xlint/lint1/msg_361.c:1.3	Sat Aug 31 06:57:31 2024
+++ src/tests/usr.bin/xlint/lint1/msg_361.c	Tue Nov  5 06:23:04 2024
@@ -1,11 +1,11 @@
-/*	$NetBSD: msg_361.c,v 1.3 2024/08/31 06:57:31 rillig Exp $	*/
+/*	$NetBSD: msg_361.c,v 1.4 2024/11/05 06:23:04 rillig Exp $	*/
 # 3 "msg_361.c"
 
 // Test for message: number base '%.*s' is %ju, must be 8, 10 or 16 [361]
 
 /*
  * The first or second character of the snprintb format specifies the number
- * base.  It must be given in binary.
+ * base.  It must be given as an octal or hexadecimal escape sequence.
  */
 
 /* lint1-extra-flags: -X 351 */

Index: src/tests/usr.bin/xlint/lint1/msg_363.c
diff -u src/tests/usr.bin/xlint/lint1/msg_363.c:1.6 src/tests/usr.bin/xlint/lint1/msg_363.c:1.7
--- src/tests/usr.bin/xlint/lint1/msg_363.c:1.6	Sat Aug 31 06:57:31 2024
+++ src/tests/usr.bin/xlint/lint1/msg_363.c	Tue Nov  5 06:23:04 2024
@@ -1,4 +1,4 @@
-/*	$NetBSD: msg_363.c,v 1.6 2024/08/31 06:57:31 rillig Exp $	*/
+/*	$NetBSD: msg_363.c,v 1.7 2024/11/05 06:23:04 rillig Exp $	*/
 # 3 "msg_363.c"
 
 // Test for message: escaped character '%.*s' in description of conversion '%.*s' [363]
@@ -30,6 +30,18 @@ old_style_description(unsigned u32)
 	    "\001non\tprint\nable\377",
 	    u32);
 
+	// In the new format, the description can technically contain
+	// arbitrary characters, but having non-printable characters would
+	// produce confusing output, so any escaped characters are suspicious
+	// of being unintended.
+	/* expect+6: warning: escaped character '\t' in description of conversion 'b\000non\t' [363] */
+	/* expect+5: warning: escaped character '\n' in description of conversion 'b\000non\tprint\n' [363] */
+	/* expect+4: warning: escaped character '\377' in description of conversion 'b\000non\tprint\nable\377' [363] */
+	snprintb(buf, sizeof(buf),
+	    "\177\020"
+	    "b\000non\tprint\nable\377\0",
+	    u32);
+
 	/* expect+10: warning: escaped character '\177' in description of conversion '\002""\177' [363] */
 	/* expect+9: warning: escaped character '\177' in description of conversion '\003aa""""\177' [363] */
 	/* expect+8: warning: escaped character '\177' in description of conversion '\004""bb""\177' [363] */

Reply via email to