Module Name:    src
Committed By:   rillig
Date:           Mon Apr  5 02:05:47 UTC 2021

Modified Files:
        src/distrib/sets/lists/tests: mi
        src/tests/usr.bin/xlint/lint1: Makefile t_integration.sh
        src/usr.bin/xlint/lint1: Makefile err.c externs1.h tree.c
Added Files:
        src/tests/usr.bin/xlint/lint1: msg_341.c msg_341.exp msg_342.c
            msg_342.exp

Log Message:
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


To generate a diff of this commit:
cvs rdiff -u -r1.1036 -r1.1037 src/distrib/sets/lists/tests/mi
cvs rdiff -u -r1.38 -r1.39 src/tests/usr.bin/xlint/lint1/Makefile \
    src/tests/usr.bin/xlint/lint1/t_integration.sh
cvs rdiff -u -r0 -r1.1 src/tests/usr.bin/xlint/lint1/msg_341.c \
    src/tests/usr.bin/xlint/lint1/msg_341.exp \
    src/tests/usr.bin/xlint/lint1/msg_342.c \
    src/tests/usr.bin/xlint/lint1/msg_342.exp
cvs rdiff -u -r1.64 -r1.65 src/usr.bin/xlint/lint1/Makefile
cvs rdiff -u -r1.101 -r1.102 src/usr.bin/xlint/lint1/err.c
cvs rdiff -u -r1.99 -r1.100 src/usr.bin/xlint/lint1/externs1.h
cvs rdiff -u -r1.265 -r1.266 src/usr.bin/xlint/lint1/tree.c

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

Modified files:

Index: src/distrib/sets/lists/tests/mi
diff -u src/distrib/sets/lists/tests/mi:1.1036 src/distrib/sets/lists/tests/mi:1.1037
--- src/distrib/sets/lists/tests/mi:1.1036	Sun Apr  4 13:20:52 2021
+++ src/distrib/sets/lists/tests/mi	Mon Apr  5 02:05:47 2021
@@ -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

Index: src/tests/usr.bin/xlint/lint1/Makefile
diff -u src/tests/usr.bin/xlint/lint1/Makefile:1.38 src/tests/usr.bin/xlint/lint1/Makefile:1.39
--- src/tests/usr.bin/xlint/lint1/Makefile:1.38	Fri Apr  2 17:25:04 2021
+++ src/tests/usr.bin/xlint/lint1/Makefile	Mon Apr  5 02:05:47 2021
@@ -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_type_question_colon.c
 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
 
Index: src/tests/usr.bin/xlint/lint1/t_integration.sh
diff -u src/tests/usr.bin/xlint/lint1/t_integration.sh:1.38 src/tests/usr.bin/xlint/lint1/t_integration.sh:1.39
--- src/tests/usr.bin/xlint/lint1/t_integration.sh:1.38	Fri Apr  2 17:25:04 2021
+++ src/tests/usr.bin/xlint/lint1/t_integration.sh	Mon Apr  5 02:05:47 2021
@@ -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 @@ all_messages_body()
 
 	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"

Index: src/usr.bin/xlint/lint1/Makefile
diff -u src/usr.bin/xlint/lint1/Makefile:1.64 src/usr.bin/xlint/lint1/Makefile:1.65
--- src/usr.bin/xlint/lint1/Makefile:1.64	Sun Mar 21 13:10:58 2021
+++ src/usr.bin/xlint/lint1/Makefile	Mon Apr  5 02:05:47 2021
@@ -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
 

Index: src/usr.bin/xlint/lint1/err.c
diff -u src/usr.bin/xlint/lint1/err.c:1.101 src/usr.bin/xlint/lint1/err.c:1.102
--- src/usr.bin/xlint/lint1/err.c:1.101	Fri Apr  2 22:41:53 2021
+++ src/usr.bin/xlint/lint1/err.c	Mon Apr  5 02:05:47 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: err.c,v 1.101 2021/04/02 22:41:53 rillig Exp $	*/
+/*	$NetBSD: err.c,v 1.102 2021/04/05 02:05:47 rillig Exp $	*/
 
 /*
  * Copyright (c) 1994, 1995 Jochen Pohl
@@ -37,7 +37,7 @@
 
 #include <sys/cdefs.h>
 #if defined(__RCSID) && !defined(lint)
-__RCSID("$NetBSD: err.c,v 1.101 2021/04/02 22:41:53 rillig Exp $");
+__RCSID("$NetBSD: err.c,v 1.102 2021/04/05 02:05:47 rillig Exp $");
 #endif
 
 #include <sys/types.h>
@@ -395,6 +395,8 @@ const	char *msgs[] = {
 	"option '%c' should be handled in the switch",		      /* 338 */
 	"option '%c' should be listed in the options string",	      /* 339 */
 	"initialization with '[a...b]' is a GNU extension",	      /* 340 */
+	"argument to '%s' must be 'unsigned char' or EOF, not '%s'",  /* 341 */
+	"argument to '%s' must be cast to 'unsigned char', not to '%s'", /* 342 */
 };
 
 /*

Index: src/usr.bin/xlint/lint1/externs1.h
diff -u src/usr.bin/xlint/lint1/externs1.h:1.99 src/usr.bin/xlint/lint1/externs1.h:1.100
--- src/usr.bin/xlint/lint1/externs1.h:1.99	Fri Apr  2 11:53:25 2021
+++ src/usr.bin/xlint/lint1/externs1.h	Mon Apr  5 02:05:47 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: externs1.h,v 1.99 2021/04/02 11:53:25 rillig Exp $	*/
+/*	$NetBSD: externs1.h,v 1.100 2021/04/05 02:05:47 rillig Exp $	*/
 
 /*
  * Copyright (c) 1994, 1995 Jochen Pohl
@@ -196,6 +196,7 @@ extern	int	to_int_constant(tnode_t *, bo
 /*
  * tree.c
  */
+extern	const tnode_t *before_conversion(const tnode_t *);
 extern	type_t	*derive_type(type_t *, tspec_t);
 extern	type_t	*expr_derive_type(type_t *, tspec_t);
 extern	tnode_t	*expr_new_constant(type_t *, val_t *);
@@ -335,6 +336,12 @@ extern	int	lex_input(void);
 extern	char	*print_tnode(char *, size_t, const tnode_t *);
 
 /*
+ * ckctype.c
+ */
+extern	void	check_ctype_function_call(const tnode_t *, const tnode_t *);
+extern	void	check_ctype_macro_invocation(const tnode_t *, const tnode_t *);
+
+/*
  * ckgetopt.c
  */
 extern	void	check_getopt_begin_while(const tnode_t *);

Index: src/usr.bin/xlint/lint1/tree.c
diff -u src/usr.bin/xlint/lint1/tree.c:1.265 src/usr.bin/xlint/lint1/tree.c:1.266
--- src/usr.bin/xlint/lint1/tree.c:1.265	Fri Apr  2 22:41:53 2021
+++ src/usr.bin/xlint/lint1/tree.c	Mon Apr  5 02:05:47 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: tree.c,v 1.265 2021/04/02 22:41:53 rillig Exp $	*/
+/*	$NetBSD: tree.c,v 1.266 2021/04/05 02:05:47 rillig Exp $	*/
 
 /*
  * Copyright (c) 1994, 1995 Jochen Pohl
@@ -37,7 +37,7 @@
 
 #include <sys/cdefs.h>
 #if defined(__RCSID) && !defined(lint)
-__RCSID("$NetBSD: tree.c,v 1.265 2021/04/02 22:41:53 rillig Exp $");
+__RCSID("$NetBSD: tree.c,v 1.266 2021/04/05 02:05:47 rillig Exp $");
 #endif
 
 #include <float.h>
@@ -718,7 +718,7 @@ cconv(tnode_t *tn)
 	return tn;
 }
 
-static const tnode_t *
+const tnode_t *
 before_conversion(const tnode_t *tn)
 {
 	while (tn->tn_op == CVT && !tn->tn_cast)
@@ -2800,8 +2800,11 @@ build_plus_minus(op_t op, tnode_t *ln, t
 
 	if (ln->tn_type->t_tspec == PTR && rn->tn_type->t_tspec != PTR) {
 
+		/* XXX: this assertion should be easy to trigger */
 		lint_assert(is_integer(rn->tn_type->t_tspec));
 
+		check_ctype_macro_invocation(ln, rn);
+
 		ctn = plength(ln->tn_type);
 		if (rn->tn_type->t_tspec != ctn->tn_type->t_tspec)
 			rn = convert(NOOP, 0, ctn->tn_type, rn);
@@ -3585,6 +3588,8 @@ new_function_call_node(tnode_t *func, tn
 		fcop = ICALL;
 	}
 
+	check_ctype_function_call(func, args);
+
 	/*
 	 * after cconv() func will always be a pointer to a function
 	 * if it is a valid function designator.

Added files:

Index: src/tests/usr.bin/xlint/lint1/msg_341.c
diff -u /dev/null src/tests/usr.bin/xlint/lint1/msg_341.c:1.1
--- /dev/null	Mon Apr  5 02:05:47 2021
+++ src/tests/usr.bin/xlint/lint1/msg_341.c	Mon Apr  5 02:05:47 2021
@@ -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)));
+}
Index: src/tests/usr.bin/xlint/lint1/msg_341.exp
diff -u /dev/null src/tests/usr.bin/xlint/lint1/msg_341.exp:1.1
--- /dev/null	Mon Apr  5 02:05:47 2021
+++ src/tests/usr.bin/xlint/lint1/msg_341.exp	Mon Apr  5 02:05:47 2021
@@ -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]
Index: src/tests/usr.bin/xlint/lint1/msg_342.c
diff -u /dev/null src/tests/usr.bin/xlint/lint1/msg_342.c:1.1
--- /dev/null	Mon Apr  5 02:05:47 2021
+++ src/tests/usr.bin/xlint/lint1/msg_342.c	Mon Apr  5 02:05:47 2021
@@ -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)));
+}
Index: src/tests/usr.bin/xlint/lint1/msg_342.exp
diff -u /dev/null src/tests/usr.bin/xlint/lint1/msg_342.exp:1.1
--- /dev/null	Mon Apr  5 02:05:47 2021
+++ src/tests/usr.bin/xlint/lint1/msg_342.exp	Mon Apr  5 02:05:47 2021
@@ -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]

Reply via email to