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: demonstrate pointer ca...



details:   https://anonhg.NetBSD.org/src/rev/b5adc8474be5
branches:  trunk
changeset: 368113:b5adc8474be5
user:      rillig <rillig%NetBSD.org@localhost>
date:      Fri Jun 24 20:02:58 2022 +0000

description:
tests/lint: demonstrate pointer casts between sockaddr variants

diffstat:

 tests/usr.bin/xlint/lint1/msg_247.c |  62 ++++++++++++++++++++++++++++++++++++-
 1 files changed, 61 insertions(+), 1 deletions(-)

diffs (73 lines):

diff -r b5d9f8c7a789 -r b5adc8474be5 tests/usr.bin/xlint/lint1/msg_247.c
--- a/tests/usr.bin/xlint/lint1/msg_247.c       Fri Jun 24 19:27:43 2022 +0000
+++ b/tests/usr.bin/xlint/lint1/msg_247.c       Fri Jun 24 20:02:58 2022 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: msg_247.c,v 1.20 2022/06/24 19:27:43 rillig Exp $      */
+/*     $NetBSD: msg_247.c,v 1.21 2022/06/24 20:02:58 rillig Exp $      */
 # 3 "msg_247.c"
 
 // Test for message: pointer cast from '%s' to '%s' may be troublesome [247]
@@ -207,3 +207,63 @@
        /* expect+1: warning: pointer cast from 'pointer to signed char' to 'pointer to struct counter' may be troublesome [247] */
        sink((struct counter *)(signed char *)ptr);
 }
+
+
+// The following data types are simplified from various system headers.
+
+typedef unsigned char uint8_t;
+typedef unsigned short uint16_t;
+typedef unsigned int uint32_t;
+
+typedef uint16_t in_port_t;
+typedef uint8_t sa_family_t;
+
+struct sockaddr {
+       uint8_t sa_len;
+       sa_family_t sa_family;
+       char sa_data[14];
+};
+
+struct in_addr {
+       uint32_t s_addr;
+};
+
+struct sockaddr_in {
+       uint8_t sin_len;
+       sa_family_t sin_family;
+       in_port_t sin_port;
+       struct in_addr sin_addr;
+       uint8_t sin_zero[8];
+};
+
+struct sockaddr_in6 {
+       uint8_t sin6_len;
+       sa_family_t sin6_family;
+       in_port_t sin6_port;
+       uint32_t sin6_flowinfo;
+       union {
+               uint8_t u6_addr8[16];
+               uint16_t u6_addr16[8];
+               uint32_t u6_addr32[4];
+       } sin6_addr;
+       uint32_t sin6_scope_id;
+};
+
+void *
+cast_between_sockaddr_variants(void *ptr)
+{
+
+       /* expect+1: warning: pointer cast from 'pointer to struct sockaddr' to 'pointer to struct sockaddr_in' may be troublesome [247] */
+       void *t1 = (struct sockaddr_in *)(struct sockaddr *)ptr;
+
+       /* expect+1: warning: pointer cast from 'pointer to struct sockaddr_in' to 'pointer to struct sockaddr' may be troublesome [247] */
+       void *t2 = (struct sockaddr *)(struct sockaddr_in *)t1;
+
+       /* expect+1: warning: pointer cast from 'pointer to struct sockaddr' to 'pointer to struct sockaddr_in6' may be troublesome [247] */
+       void *t3 = (struct sockaddr_in6 *)(struct sockaddr *)t2;
+
+       /* expect+1: warning: pointer cast from 'pointer to struct sockaddr_in6' to 'pointer to struct sockaddr' may be troublesome [247] */
+       void *t4 = (struct sockaddr *)(struct sockaddr_in6 *)t3;
+
+       return t4;
+}



Home | Main Index | Thread Index | Old Index