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: warn about for wrong type cast in ...



details:   https://anonhg.NetBSD.org/src/rev/1f326af91ddc
branches:  trunk
changeset: 954321:1f326af91ddc
user:      rillig <rillig%NetBSD.org@localhost>
date:      Mon Apr 05 02:05:47 2021 +0000

description:
lint: warn about for wrong type cast in argument to ctype.h functions

The argument to most of the functions from <ctype.h> "shall either be
representable as an 'unsigned char' or shall equal the value of the
macro EOF".

When confronted with the infamous warning 'array subscript has type
char', there are enough programmers who don't know the background of
that warning and thus fix it in a wrong way.  Neither GCC nor Clang
explain its warning to target these programmers.

Both GCC and Clang warn about 'array subscript has type char', but they
ignore the other requirements of the <ctype.h> functions, even though
these are in the C standard library.

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=94182
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=95177
https://stackoverflow.com/a/60696378

diffstat:

 distrib/sets/lists/tests/mi                |    6 +-
 tests/usr.bin/xlint/lint1/Makefile         |    5 +-
 tests/usr.bin/xlint/lint1/msg_341.c        |   77 ++++++++++++++
 tests/usr.bin/xlint/lint1/msg_341.exp      |    6 +
 tests/usr.bin/xlint/lint1/msg_342.c        |   77 ++++++++++++++
 tests/usr.bin/xlint/lint1/msg_342.exp      |    6 +
 tests/usr.bin/xlint/lint1/t_integration.sh |    4 +-
 usr.bin/xlint/lint1/Makefile               |    4 +-
 usr.bin/xlint/lint1/ckctype.c              |  151 +++++++++++++++++++++++++++++
 usr.bin/xlint/lint1/err.c                  |    6 +-
 usr.bin/xlint/lint1/externs1.h             |    9 +-
 usr.bin/xlint/lint1/tree.c                 |   11 +-
 12 files changed, 349 insertions(+), 13 deletions(-)

diffs (truncated from 516 to 300 lines):

diff -r f0db79b148c4 -r 1f326af91ddc distrib/sets/lists/tests/mi
--- a/distrib/sets/lists/tests/mi       Mon Apr 05 01:35:34 2021 +0000
+++ b/distrib/sets/lists/tests/mi       Mon Apr 05 02:05:47 2021 +0000
@@ -1,4 +1,4 @@
-# $NetBSD: mi,v 1.1036 2021/04/04 13:20:52 rillig Exp $
+# $NetBSD: mi,v 1.1037 2021/04/05 02:05:47 rillig Exp $
 #
 # Note: don't delete entries from here - mark them as "obsolete" instead.
 #
@@ -6863,6 +6863,10 @@
 ./usr/tests/usr.bin/xlint/lint1/msg_339.exp                    tests-usr.bin-tests     compattestfile,atf
 ./usr/tests/usr.bin/xlint/lint1/msg_340.c                      tests-usr.bin-tests     compattestfile,atf
 ./usr/tests/usr.bin/xlint/lint1/msg_340.exp                    tests-usr.bin-tests     compattestfile,atf
+./usr/tests/usr.bin/xlint/lint1/msg_341.c                      tests-usr.bin-tests     compattestfile,atf
+./usr/tests/usr.bin/xlint/lint1/msg_341.exp                    tests-usr.bin-tests     compattestfile,atf
+./usr/tests/usr.bin/xlint/lint1/msg_342.c                      tests-usr.bin-tests     compattestfile,atf
+./usr/tests/usr.bin/xlint/lint1/msg_342.exp                    tests-usr.bin-tests     compattestfile,atf
 ./usr/tests/usr.bin/xlint/lint1/op_colon.c                     tests-usr.bin-tests     compattestfile,atf
 ./usr/tests/usr.bin/xlint/lint1/op_colon.exp                   tests-usr.bin-tests     compattestfile,atf
 ./usr/tests/usr.bin/xlint/lint1/t_integration                  tests-usr.bin-tests     compattestfile,atf
diff -r f0db79b148c4 -r 1f326af91ddc tests/usr.bin/xlint/lint1/Makefile
--- a/tests/usr.bin/xlint/lint1/Makefile        Mon Apr 05 01:35:34 2021 +0000
+++ b/tests/usr.bin/xlint/lint1/Makefile        Mon Apr 05 02:05:47 2021 +0000
@@ -1,6 +1,7 @@
-# $NetBSD: Makefile,v 1.38 2021/04/02 17:25:04 rillig Exp $
+# $NetBSD: Makefile,v 1.39 2021/04/05 02:05:47 rillig Exp $
 
 NOMAN=         # defined
+MAX_MESSAGE=   342             # see lint1/err.c
 
 .include <bsd.own.mk>
 
@@ -98,7 +99,7 @@
 FILES+=                d_typefun.c
 FILES+=                d_typename_as_var.c
 FILES+=                d_zero_sized_arrays.c
-FILES+=                ${:U0 ${:U:range=340}:C,^.$,0&,:C,^..$,0&,:@msg@msg_${msg}.c msg_${msg}.exp@:Nmsg_176.exp}
+FILES+=                ${:U0 ${:U:${:Urange=${MAX_MESSAGE}}}:C,^.$,0&,:C,^..$,0&,:@i@msg_${i}.c msg_${i}.exp@:Nmsg_176.exp}
 FILES+=                op_colon.c
 FILES+=                op_colon.exp
 
diff -r f0db79b148c4 -r 1f326af91ddc tests/usr.bin/xlint/lint1/msg_341.c
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/usr.bin/xlint/lint1/msg_341.c       Mon Apr 05 02:05:47 2021 +0000
@@ -0,0 +1,77 @@
+/*     $NetBSD: msg_341.c,v 1.1 2021/04/05 02:05:47 rillig Exp $       */
+# 3 "msg_341.c"
+
+// Test for message: argument to '%s' must be 'unsigned char' or EOF, not '%s' [341]
+
+/*
+ * Ensure that the functions from <ctype.h> are called with the correct
+ * argument.
+ */
+
+/* NetBSD 9.99.81, <ctype.h> */
+extern const unsigned short *_ctype_tab_;
+extern const short *_tolower_tab_;
+extern const short *_toupper_tab_;
+int isspace(int);
+
+void sink(int);
+
+void
+function_call_char(char c)
+{
+
+       /* expect+1: argument to 'isspace' must be 'unsigned char' or EOF, not 'char' */
+       (isspace)(c);
+
+       /* This is the only allowed form. */
+       isspace((unsigned char)c);
+
+       /* The cast to 'int' is redundant, it doesn't hurt though. */
+       isspace((int)(unsigned char)c);
+
+       /* expect+1: argument to 'isspace' must be cast to 'unsigned char', not to 'int' */
+       isspace((int)c);
+
+       /* expect+1: argument to 'isspace' must be cast to 'unsigned char', not to 'unsigned int' */
+       isspace((unsigned int)c);
+}
+
+/*
+ * If the expression starts with type 'unsigned char', it can be cast to any
+ * other type.  Chances are low enough that the cast is to 'char', which would
+ * be the only bad type.
+ */
+void
+function_call_unsigned_char(unsigned char c)
+{
+
+       (isspace)(c);
+       isspace((unsigned char)c);
+       isspace((int)c);
+       isspace((unsigned int)c);
+}
+
+/* When used in a loop of fgetc, the type is already 'int'.  That's fine. */
+void
+function_call_int(int c)
+{
+
+       isspace(c);
+}
+
+void
+macro_invocation_NetBSD(char c)
+{
+
+       /* expect+1: argument to 'function from <ctype.h>' must be 'unsigned char' or EOF, not 'char' */
+       sink(((int)((_ctype_tab_ + 1)[(c)] & 0x0040)));
+
+       /* This is the only allowed form. */
+       sink(((int)((_ctype_tab_ + 1)[((unsigned char)c)] & 0x0040)));
+
+       /* expect+1: argument to 'function from <ctype.h>' must be cast to 'unsigned char', not to 'int' */
+       sink(((int)((_ctype_tab_ + 1)[((int)c)] & 0x0040)));
+
+       /* expect+1: argument to 'function from <ctype.h>' must be cast to 'unsigned char', not to 'unsigned int' */
+       sink(((int)((_ctype_tab_ + 1)[((unsigned int)c)] & 0x0040)));
+}
diff -r f0db79b148c4 -r 1f326af91ddc tests/usr.bin/xlint/lint1/msg_341.exp
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/usr.bin/xlint/lint1/msg_341.exp     Mon Apr 05 02:05:47 2021 +0000
@@ -0,0 +1,6 @@
+msg_341.c(24): warning: argument to 'isspace' must be 'unsigned char' or EOF, not 'char' [341]
+msg_341.c(33): warning: argument to 'isspace' must be cast to 'unsigned char', not to 'int' [342]
+msg_341.c(36): warning: argument to 'isspace' must be cast to 'unsigned char', not to 'unsigned int' [342]
+msg_341.c(67): warning: argument to 'function from <ctype.h>' must be 'unsigned char' or EOF, not 'char' [341]
+msg_341.c(73): warning: argument to 'function from <ctype.h>' must be cast to 'unsigned char', not to 'int' [342]
+msg_341.c(76): warning: argument to 'function from <ctype.h>' must be cast to 'unsigned char', not to 'unsigned int' [342]
diff -r f0db79b148c4 -r 1f326af91ddc tests/usr.bin/xlint/lint1/msg_342.c
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/usr.bin/xlint/lint1/msg_342.c       Mon Apr 05 02:05:47 2021 +0000
@@ -0,0 +1,77 @@
+/*     $NetBSD: msg_342.c,v 1.1 2021/04/05 02:05:47 rillig Exp $       */
+# 3 "msg_341.c"
+
+// Test for message: argument to '%s' must be cast to 'unsigned char', not to '%s' [342]
+
+/*
+ * Ensure that the functions from <ctype.h> are called with the correct
+ * argument.
+ */
+
+/* NetBSD 9.99.81, <ctype.h> */
+extern const unsigned short *_ctype_tab_;
+extern const short *_tolower_tab_;
+extern const short *_toupper_tab_;
+int isspace(int);
+
+void sink(int);
+
+void
+function_call_char(char c)
+{
+
+       /* expect+1: argument to 'isspace' must be 'unsigned char' or EOF, not 'char' */
+       (isspace)(c);
+
+       /* This is the only allowed form. */
+       isspace((unsigned char)c);
+
+       /* The cast to 'int' is redundant, it doesn't hurt though. */
+       isspace((int)(unsigned char)c);
+
+       /* expect+1: argument to 'isspace' must be cast to 'unsigned char', not to 'int' */
+       isspace((int)c);
+
+       /* expect+1: argument to 'isspace' must be cast to 'unsigned char', not to 'unsigned int' */
+       isspace((unsigned int)c);
+}
+
+/*
+ * If the expression starts with type 'unsigned char', it can be cast to any
+ * other type.  Chances are low enough that the cast is to 'char', which would
+ * be the only bad type.
+ */
+void
+function_call_unsigned_char(unsigned char c)
+{
+
+       (isspace)(c);
+       isspace((unsigned char)c);
+       isspace((int)c);
+       isspace((unsigned int)c);
+}
+
+/* When used in a loop of fgetc, the type is already 'int'.  That's fine. */
+void
+function_call_int(int c)
+{
+
+       isspace(c);
+}
+
+void
+macro_invocation_NetBSD(char c)
+{
+
+       /* expect+1: argument to 'function from <ctype.h>' must be 'unsigned char' or EOF, not 'char' */
+       sink(((int)((_ctype_tab_ + 1)[(c)] & 0x0040)));
+
+       /* This is the only allowed form. */
+       sink(((int)((_ctype_tab_ + 1)[((unsigned char)c)] & 0x0040)));
+
+       /* expect+1: argument to 'function from <ctype.h>' must be cast to 'unsigned char', not to 'int' */
+       sink(((int)((_ctype_tab_ + 1)[((int)c)] & 0x0040)));
+
+       /* expect+1: argument to 'function from <ctype.h>' must be cast to 'unsigned char', not to 'unsigned int' */
+       sink(((int)((_ctype_tab_ + 1)[((unsigned int)c)] & 0x0040)));
+}
diff -r f0db79b148c4 -r 1f326af91ddc tests/usr.bin/xlint/lint1/msg_342.exp
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/usr.bin/xlint/lint1/msg_342.exp     Mon Apr 05 02:05:47 2021 +0000
@@ -0,0 +1,6 @@
+msg_341.c(24): warning: argument to 'isspace' must be 'unsigned char' or EOF, not 'char' [341]
+msg_341.c(33): warning: argument to 'isspace' must be cast to 'unsigned char', not to 'int' [342]
+msg_341.c(36): warning: argument to 'isspace' must be cast to 'unsigned char', not to 'unsigned int' [342]
+msg_341.c(67): warning: argument to 'function from <ctype.h>' must be 'unsigned char' or EOF, not 'char' [341]
+msg_341.c(73): warning: argument to 'function from <ctype.h>' must be cast to 'unsigned char', not to 'int' [342]
+msg_341.c(76): warning: argument to 'function from <ctype.h>' must be cast to 'unsigned char', not to 'unsigned int' [342]
diff -r f0db79b148c4 -r 1f326af91ddc tests/usr.bin/xlint/lint1/t_integration.sh
--- a/tests/usr.bin/xlint/lint1/t_integration.sh        Mon Apr 05 01:35:34 2021 +0000
+++ b/tests/usr.bin/xlint/lint1/t_integration.sh        Mon Apr 05 02:05:47 2021 +0000
@@ -1,4 +1,4 @@
-# $NetBSD: t_integration.sh,v 1.38 2021/04/02 17:25:04 rillig Exp $
+# $NetBSD: t_integration.sh,v 1.39 2021/04/05 02:05:47 rillig Exp $
 #
 # Copyright (c) 2008, 2010 The NetBSD Foundation, Inc.
 # All rights reserved.
@@ -175,7 +175,7 @@
 
        failed=""
 
-       for msg in $(seq 0 340); do
+       for msg in $(seq 0 342); do
                name="$(printf 'msg_%03d.c' "${msg}")"
                check_lint1 "${name}" \
                || failed="$failed${failed:+ }$name"
diff -r f0db79b148c4 -r 1f326af91ddc usr.bin/xlint/lint1/Makefile
--- a/usr.bin/xlint/lint1/Makefile      Mon Apr 05 01:35:34 2021 +0000
+++ b/usr.bin/xlint/lint1/Makefile      Mon Apr 05 02:05:47 2021 +0000
@@ -1,9 +1,9 @@
-#      $NetBSD: Makefile,v 1.64 2021/03/21 13:10:58 rillig Exp $
+#      $NetBSD: Makefile,v 1.65 2021/04/05 02:05:47 rillig Exp $
 
 .include <bsd.own.mk>
 
 PROG=          lint1
-SRCS=          cgram.y ckgetopt.c decl.c emit.c emit1.c err.c \
+SRCS=          cgram.y ckctype.c ckgetopt.c decl.c emit.c emit1.c err.c \
                func.c init.c inittyp.c lex.c \
                main1.c mem.c mem1.c oper.c print.c scan.l tree.c tyname.c
 
diff -r f0db79b148c4 -r 1f326af91ddc usr.bin/xlint/lint1/ckctype.c
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/usr.bin/xlint/lint1/ckctype.c     Mon Apr 05 02:05:47 2021 +0000
@@ -0,0 +1,151 @@
+/* $NetBSD: ckctype.c,v 1.1 2021/04/05 02:07:14 rillig Exp $ */
+
+/*-
+ * Copyright (c) 2021 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by Roland Illig <rillig%NetBSD.org@localhost>.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#if HAVE_NBTOOL_CONFIG_H
+
+#include "nbtool_config.h"
+
+#endif
+
+#include <sys/cdefs.h>
+
+#if defined(__RCSID) && !defined(lint)



Home | Main Index | Thread Index | Old Index