Module Name:    src
Committed By:   rillig
Date:           Fri Mar 26 16:19:43 UTC 2021

Modified Files:
        src/tests/usr.bin/xlint/lint1: msg_247.c msg_247.exp

Log Message:
tests/lint: add tests for warning about cast to character types


To generate a diff of this commit:
cvs rdiff -u -r1.5 -r1.6 src/tests/usr.bin/xlint/lint1/msg_247.c
cvs rdiff -u -r1.3 -r1.4 src/tests/usr.bin/xlint/lint1/msg_247.exp

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_247.c
diff -u src/tests/usr.bin/xlint/lint1/msg_247.c:1.5 src/tests/usr.bin/xlint/lint1/msg_247.c:1.6
--- src/tests/usr.bin/xlint/lint1/msg_247.c:1.5	Sun Mar 14 22:24:24 2021
+++ src/tests/usr.bin/xlint/lint1/msg_247.c	Fri Mar 26 16:19:43 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: msg_247.c,v 1.5 2021/03/14 22:24:24 rillig Exp $	*/
+/*	$NetBSD: msg_247.c,v 1.6 2021/03/26 16:19:43 rillig Exp $	*/
 # 3 "msg_247.c"
 
 // Test for message: pointer cast from '%s' to '%s' may be troublesome [247]
@@ -30,3 +30,48 @@ example(struct Other *arg)
 	 */
 	display = (PDisplay)arg;	/* expect: 247 */
 }
+
+/*
+ * C code with a long history that has existed in pre-C90 times already often
+ * uses 'pointer to char' where modern code would use 'pointer to void'.
+ * Since 'char' is the most general underlying type, there is nothing wrong
+ * with casting to it.  An example for this type of code is X11.
+ *
+ * Casting to 'pointer to char' may also be used by programmers who don't know
+ * about endianness, but that's not something lint can do anything about.  The
+ * code for these two use cases looks exactly the same, so lint errs on the
+ * side of fewer false positive warnings here. (after fixing the FIXME below)
+ */
+char *
+cast_to_char_pointer(struct Other *arg)
+{
+	return (char *)arg;		/* expect: 247 *//* FIXME */
+}
+
+/*
+ * In traditional C there was 'unsigned char' as well, so the same reasoning
+ * as for plain 'char' applies here.
+ */
+unsigned char *
+cast_to_unsigned_char_pointer(struct Other *arg)
+{
+	return (unsigned char *)arg;	/* expect: 247 *//* FIXME */
+}
+
+/*
+ * Traditional C does not have the type specifier 'signed', which means that
+ * this type cannot be used by old code.  Therefore warn about this.  All code
+ * that triggers this warning should do the intermediate cast via 'void
+ * pointer'.
+ */
+signed char *
+cast_to_signed_char_pointer(struct Other *arg)
+{
+	return (signed char *)arg;	/* expect: 247 */
+}
+
+char *
+cast_to_void_pointer_then_to_char_pointer(struct Other *arg)
+{
+	return (char *)(void *)arg;
+}

Index: src/tests/usr.bin/xlint/lint1/msg_247.exp
diff -u src/tests/usr.bin/xlint/lint1/msg_247.exp:1.3 src/tests/usr.bin/xlint/lint1/msg_247.exp:1.4
--- src/tests/usr.bin/xlint/lint1/msg_247.exp:1.3	Sun Mar 14 22:24:24 2021
+++ src/tests/usr.bin/xlint/lint1/msg_247.exp	Fri Mar 26 16:19:43 2021
@@ -1 +1,4 @@
 msg_247.c(31): warning: pointer cast from 'pointer to struct Other' to 'pointer to struct <unnamed>' may be troublesome [247]
+msg_247.c(48): warning: pointer cast from 'pointer to struct Other' to 'pointer to char' may be troublesome [247]
+msg_247.c(58): warning: pointer cast from 'pointer to struct Other' to 'pointer to unsigned char' may be troublesome [247]
+msg_247.c(70): warning: pointer cast from 'pointer to struct Other' to 'pointer to signed char' may be troublesome [247]

Reply via email to