Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/usr.bin/xlint/lint1 lint: be strict when parsing command lin...



details:   https://anonhg.NetBSD.org/src/rev/bf96256d7677
branches:  trunk
changeset: 374454:bf96256d7677
user:      rillig <rillig%NetBSD.org@localhost>
date:      Sun Apr 23 09:04:44 2023 +0000

description:
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.

diffstat:

 tests/usr.bin/xlint/lint1/t_usage.sh |  18 ++++++++----
 usr.bin/xlint/lint1/err.c            |  49 +++++++++++++++++++----------------
 usr.bin/xlint/lint1/externs1.h       |   4 +-
 3 files changed, 40 insertions(+), 31 deletions(-)

diffs (154 lines):

diff -r 4c3d774c72da -r bf96256d7677 tests/usr.bin/xlint/lint1/t_usage.sh
--- a/tests/usr.bin/xlint/lint1/t_usage.sh      Sun Apr 23 08:53:08 2023 +0000
+++ b/tests/usr.bin/xlint/lint1/t_usage.sh      Sun Apr 23 09:04:44 2023 +0000
@@ -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
 }
 
diff -r 4c3d774c72da -r bf96256d7677 usr.bin/xlint/lint1/err.c
--- a/usr.bin/xlint/lint1/err.c Sun Apr 23 08:53:08 2023 +0000
+++ b/usr.bin/xlint/lint1/err.c Sun Apr 23 09:04:44 2023 +0000
@@ -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 (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);
+       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;
+
                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)
 {
-
-       for (const char *s = arg;;) {
-               const char *e = s + strcspn(s, ",");
+       char *end;
 
-               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);
 }
diff -r 4c3d774c72da -r bf96256d7677 usr.bin/xlint/lint1/externs1.h
--- a/usr.bin/xlint/lint1/externs1.h    Sun Apr 23 08:53:08 2023 +0000
+++ b/usr.bin/xlint/lint1/externs1.h    Sun Apr 23 09:04:44 2023 +0000
@@ -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 *);



Home | Main Index | Thread Index | Old Index