Source-Changes-HG archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]
[src/trunk]: src/tests/usr.bin/xlint/lint1 tests/lint: add tests for warning ...
details: https://anonhg.NetBSD.org/src/rev/612d7948535a
branches: trunk
changeset: 960684:612d7948535a
user: rillig <rillig%NetBSD.org@localhost>
date: Fri Mar 26 16:19:43 2021 +0000
description:
tests/lint: add tests for warning about cast to character types
diffstat:
tests/usr.bin/xlint/lint1/msg_247.c | 47 ++++++++++++++++++++++++++++++++++-
tests/usr.bin/xlint/lint1/msg_247.exp | 3 ++
2 files changed, 49 insertions(+), 1 deletions(-)
diffs (66 lines):
diff -r 229b9b5097c2 -r 612d7948535a tests/usr.bin/xlint/lint1/msg_247.c
--- a/tests/usr.bin/xlint/lint1/msg_247.c Fri Mar 26 16:05:19 2021 +0000
+++ b/tests/usr.bin/xlint/lint1/msg_247.c Fri Mar 26 16:19:43 2021 +0000
@@ -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 @@
*/
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;
+}
diff -r 229b9b5097c2 -r 612d7948535a tests/usr.bin/xlint/lint1/msg_247.exp
--- a/tests/usr.bin/xlint/lint1/msg_247.exp Fri Mar 26 16:05:19 2021 +0000
+++ b/tests/usr.bin/xlint/lint1/msg_247.exp Fri Mar 26 16:19:43 2021 +0000
@@ -1,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]
Home |
Main Index |
Thread Index |
Old Index