Module Name:    src
Committed By:   rillig
Date:           Sun Apr 23 09:04:44 UTC 2023

Modified Files:
        src/tests/usr.bin/xlint/lint1: t_usage.sh
        src/usr.bin/xlint/lint1: err.c externs1.h

Log Message:
lint: be strict when parsing command line for excluded message IDs

Previously, lint accepted -X '1, 2, 3', while the manual page lists the
IDs without spaces.

On 32-bit platforms, lint accepted -X -4294967295, and on 64-bit
platforms, it accepted the corresponding larger numbers.

The code for parsing message IDs and query IDs conceptually does the
same, but the implementations differed for no reason.


To generate a diff of this commit:
cvs rdiff -u -r1.1 -r1.2 src/tests/usr.bin/xlint/lint1/t_usage.sh
cvs rdiff -u -r1.193 -r1.194 src/usr.bin/xlint/lint1/err.c
cvs rdiff -u -r1.176 -r1.177 src/usr.bin/xlint/lint1/externs1.h

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/t_usage.sh
diff -u src/tests/usr.bin/xlint/lint1/t_usage.sh:1.1 src/tests/usr.bin/xlint/lint1/t_usage.sh:1.2
--- src/tests/usr.bin/xlint/lint1/t_usage.sh:1.1	Sun Apr 23 08:47:27 2023
+++ src/tests/usr.bin/xlint/lint1/t_usage.sh	Sun Apr 23 09:04:44 2023
@@ -1,4 +1,4 @@
-# $NetBSD: t_usage.sh,v 1.1 2023/04/23 08:47:27 rillig Exp $
+# $NetBSD: t_usage.sh,v 1.2 2023/04/23 09:04:44 rillig Exp $
 #
 # Copyright (c) 2023 The NetBSD Foundation, Inc.
 # All rights reserved.
@@ -48,29 +48,35 @@ suppress_messages_body()
 	# Larger than the largest known message.
 	atf_check \
 	    -s 'exit:1' \
-	    -e "inline:lint1: invalid error message id '353'\n" \
+	    -e "inline:lint1: invalid message ID '353'\n" \
 	    "$lint1" -X 353 code.c /dev/null
 
-	# XXX: Whitespace should not be allowed before a message ID.
+	# Whitespace is not allowed before a message ID.
 	atf_check \
+	    -s 'exit:1' \
+	    -e "inline:lint1: invalid message ID ' 1'\n" \
 	    "$lint1" -X ' 1' code.c /dev/null
 
 	# Whitespace is not allowed after a message ID.
 	atf_check \
 	    -s 'exit:1' \
-	    -e "inline:lint1: invalid error message id '1 '\n" \
+	    -e "inline:lint1: invalid message ID '1 '\n" \
 	    "$lint1" -X '1 ' code.c /dev/null
 
 	# Multiple message IDs can be comma-separated.
 	atf_check \
 	    "$lint1" -X '1,2,3,4' code.c /dev/null
 
-	# XXX: Whitespace should not be allowed after a comma.
+	# Whitespace is not allowed after a comma.
 	atf_check \
+	    -s 'exit:1' \
+	    -e "inline:lint1: invalid message ID ' 2'\n" \
 	    "$lint1" -X '1, 2, 3, 4' code.c /dev/null
 
-	# XXX: Trailing commas should not be allowed.
+	# Trailing commas are not allowed.
 	atf_check \
+	    -s 'exit:1' \
+	    -e "inline:lint1: invalid message ID ''\n" \
 	    "$lint1" -X '1,,,,,,,' code.c /dev/null
 }
 

Index: src/usr.bin/xlint/lint1/err.c
diff -u src/usr.bin/xlint/lint1/err.c:1.193 src/usr.bin/xlint/lint1/err.c:1.194
--- src/usr.bin/xlint/lint1/err.c:1.193	Sat Apr 15 11:34:45 2023
+++ src/usr.bin/xlint/lint1/err.c	Sun Apr 23 09:04:44 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: err.c,v 1.193 2023/04/15 11:34:45 rillig Exp $	*/
+/*	$NetBSD: err.c,v 1.194 2023/04/23 09:04:44 rillig Exp $	*/
 
 /*
  * Copyright (c) 1994, 1995 Jochen Pohl
@@ -37,7 +37,7 @@
 
 #include <sys/cdefs.h>
 #if defined(__RCSID)
-__RCSID("$NetBSD: err.c,v 1.193 2023/04/15 11:34:45 rillig Exp $");
+__RCSID("$NetBSD: err.c,v 1.194 2023/04/23 09:04:44 rillig Exp $");
 #endif
 
 #include <limits.h>
@@ -419,18 +419,23 @@ static struct include_level {
 } *includes;
 
 void
-suppress_messages(char *ids)
+suppress_messages(const char *p)
 {
-	char *ptr, *end;
-	unsigned long id;
+	char *end;
+
+	for (; ch_isdigit(*p); p = end + 1) {
+		unsigned long id = strtoul(p, &end, 10);
+		if ((*end != '\0' && *end != ',') ||
+		    id >= sizeof(msgs) / sizeof(msgs[0]) ||
+		    msgs[id][0] == '\0')
+			break;
 
-	for (ptr = strtok(ids, ","); ptr != NULL; ptr = strtok(NULL, ",")) {
-		id = strtoul(ptr, &end, 10);
-		if (*end != '\0' || ptr == end ||
-		    id >= sizeof(msgs) / sizeof(msgs[0]))
-			errx(1, "invalid error message id '%s'", ptr);
 		is_suppressed[id] = true;
+
+		if (*end == '\0')
+			return;
 	}
+	errx(1, "invalid message ID '%.*s'", (int)(strcspn(p, ",")), p);
 }
 
 void
@@ -722,24 +727,22 @@ void
 }
 
 void
-enable_queries(const char *arg)
+enable_queries(const char *p)
 {
+	char *end;
 
-	for (const char *s = arg;;) {
-		const char *e = s + strcspn(s, ",");
-
-		char *end;
-		unsigned long id = strtoul(s, &end, 10);
-		if (!(ch_isdigit(s[0]) && end == e &&
-		      id < sizeof(queries) / sizeof(queries[0]) &&
-		      queries[id][0] != '\0'))
-			errx(1, "invalid query ID '%.*s'", (int)(e - s), s);
+	for (; ch_isdigit(*p); p = end + 1) {
+		unsigned long id = strtoul(p, &end, 10);
+		if ((*end != '\0' && *end != ',') ||
+		    id >= sizeof(queries) / sizeof(queries[0]) ||
+		    queries[id][0] == '\0')
+			break;
 
 		any_query_enabled = true;
 		is_query_enabled[id] = true;
 
-		if (*e == '\0')
-			break;
-		s = e + 1;
+		if (*end == '\0')
+			return;
 	}
+	errx(1, "invalid query ID '%.*s'", (int)(strcspn(p, ",")), p);
 }

Index: src/usr.bin/xlint/lint1/externs1.h
diff -u src/usr.bin/xlint/lint1/externs1.h:1.176 src/usr.bin/xlint/lint1/externs1.h:1.177
--- src/usr.bin/xlint/lint1/externs1.h:1.176	Tue Apr 11 17:52:11 2023
+++ src/usr.bin/xlint/lint1/externs1.h	Sun Apr 23 09:04:44 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: externs1.h,v 1.176 2023/04/11 17:52:11 rillig Exp $	*/
+/*	$NetBSD: externs1.h,v 1.177 2023/04/23 09:04:44 rillig Exp $	*/
 
 /*
  * Copyright (c) 1994, 1995 Jochen Pohl
@@ -171,7 +171,7 @@ extern	void	c11ism(int, ...);
 extern	void	assert_failed(const char *, int, const char *, const char *)
 		__attribute__((__noreturn__));
 extern	void	update_location(const char *, int, bool, bool);
-extern	void	suppress_messages(char *);
+extern	void	suppress_messages(const char *);
 
 extern	void	query_message(int, ...);
 extern	void	enable_queries(const char *);

Reply via email to