git: 484c2ce9752f - stable/14 - netlink: Fix C++ compile errors

From: Warner Losh <imp_at_FreeBSD.org>
Date: Wed, 12 Jun 2024 21:55:22 UTC
The branch stable/14 has been updated by imp:

URL: https://cgit.FreeBSD.org/src/commit/?id=484c2ce9752f610ab082a002341a290d683b8343

commit 484c2ce9752f610ab082a002341a290d683b8343
Author:     cnbatch <cnbatch@gmail.com>
AuthorDate: 2024-05-17 13:09:44 +0000
Commit:     Warner Losh <imp@FreeBSD.org>
CommitDate: 2024-06-12 21:46:24 +0000

    netlink: Fix C++ compile errors
    
    Allow these files to be included in C++ programs with careful casting to
    the proper type, like C++ wants (and in a way that also works for C).
    
    MFC After: 1 week
    Reviewed by: imp
    Pull Request: https://github.com/freebsd/freebsd-src/pull/1245
    
    (cherry picked from commit ff92493a4f6504c49a6c84ec65053f493ff5d708)
---
 sys/netlink/netlink_snl.h               | 33 +++++++++++++++++----------------
 sys/netlink/netlink_snl_route.h         |  2 +-
 sys/netlink/netlink_snl_route_parsers.h | 10 +++++-----
 3 files changed, 23 insertions(+), 22 deletions(-)

diff --git a/sys/netlink/netlink_snl.h b/sys/netlink/netlink_snl.h
index f8387132f78a..03f2b7dc5c38 100644
--- a/sys/netlink/netlink_snl.h
+++ b/sys/netlink/netlink_snl.h
@@ -63,7 +63,7 @@
 
 #define	_NLA_END(_start, _len)	((char *)(_start) + (_len))
 #define NLA_FOREACH(_attr, _start, _len)      \
-        for (_attr = (_start);		\
+        for (_attr = (struct nlattr *)(_start);		\
 		((char *)_attr < _NLA_END(_start, _len)) && \
 		((char *)NLA_NEXT(_attr) <= _NLA_END(_start, _len));	\
 		_attr =  NLA_NEXT(_attr))
@@ -80,7 +80,7 @@ struct linear_buffer {
 static inline struct linear_buffer *
 lb_init(uint32_t size)
 {
-	struct linear_buffer *lb = calloc(1, size);
+	struct linear_buffer *lb = (struct linear_buffer *)calloc(1, size);
 
 	if (lb != NULL) {
 		lb->base = (char *)(lb + 1);
@@ -102,7 +102,7 @@ lb_allocz(struct linear_buffer *lb, int len)
 	len = roundup2(len, alignof(__max_align_t));
 	if (lb->offset + len > lb->size)
 		return (NULL);
-	void *data = (void *)(lb->base + lb->offset);
+	char *data = (lb->base + lb->offset);
 	lb->offset += len;
 	return (data);
 }
@@ -275,7 +275,7 @@ snl_init(struct snl_state *ss, int netlink_family)
 	}
 
 	ss->bufsize = rcvbuf;
-	ss->buf = malloc(ss->bufsize);
+	ss->buf = (char *)malloc(ss->bufsize);
 	if (ss->buf == NULL) {
 		snl_free(ss);
 		return (false);
@@ -495,7 +495,8 @@ snl_parse_header(struct snl_state *ss, void *hdr, int len,
 	struct nlattr *nla_head;
 
 	/* Extract fields first (if any) */
-	snl_parse_fields(ss, hdr, parser->in_hdr_size, parser->fp, parser->fp_size, target);
+	snl_parse_fields(ss, (struct nlmsghdr *)hdr, parser->in_hdr_size,
+	    parser->fp, parser->fp_size, target);
 
 	nla_head = (struct nlattr *)(void *)((char *)hdr + parser->in_hdr_size);
 	bool result = snl_parse_attrs_raw(ss, nla_head, len - parser->in_hdr_size,
@@ -616,7 +617,7 @@ snl_attr_get_stringn(struct snl_state *ss, struct nlattr *nla,
 {
 	int maxlen = NLA_DATA_LEN(nla);
 
-	char *buf = snl_allocz(ss, maxlen + 1);
+	char *buf = (char *)snl_allocz(ss, maxlen + 1);
 	if (buf == NULL)
 		return (false);
 	buf[maxlen] = '\0';
@@ -633,7 +634,7 @@ snl_attr_copy_string(struct snl_state *ss, struct nlattr *nla,
 	char *tmp;
 
 	if (snl_attr_get_string(ss, nla, NULL, &tmp)) {
-		strlcpy(target, tmp, (size_t)arg);
+		strlcpy((char *)target, tmp, (size_t)arg);
 		return (true);
 	}
 	return (false);
@@ -646,7 +647,7 @@ snl_attr_dup_string(struct snl_state *ss __unused, struct nlattr *nla,
 	size_t maxlen = NLA_DATA_LEN(nla);
 
 	if (strnlen((char *)NLA_DATA(nla), maxlen) < maxlen) {
-		char *buf = snl_allocz(ss, maxlen);
+		char *buf = (char *)snl_allocz(ss, maxlen);
 		if (buf == NULL)
 			return (false);
 		memcpy(buf, NLA_DATA(nla), maxlen);
@@ -675,14 +676,14 @@ snl_attr_get_parray_sz(struct snl_state *ss, struct nlattr *container_nla,
     uint32_t start_size, const void *arg, void *target)
 {
 	const struct snl_hdr_parser *p = (const struct snl_hdr_parser *)arg;
-	struct snl_parray *array = target;
+	struct snl_parray *array = (struct snl_parray *)target;
 	struct nlattr *nla;
 	uint32_t count = 0, size = start_size;
 
 	if (p->out_size == 0)
 		return (false);
 
-	array->items = snl_allocz(ss, size * sizeof(void *));
+	array->items = (void **)snl_allocz(ss, size * sizeof(void *));
 	if (array->items == NULL)
 		return (false);
 
@@ -712,7 +713,7 @@ snl_attr_get_parray_sz(struct snl_state *ss, struct nlattr *container_nla,
 
 		if (count == size) {
 			uint32_t new_size = size * 2;
-			void **new_array = snl_allocz(ss, new_size *sizeof(void *));
+			void **new_array = (void **)snl_allocz(ss, new_size *sizeof(void *));
 
 			memcpy(new_array, array->items, size * sizeof(void *));
 			array->items = new_array;
@@ -825,7 +826,7 @@ static const struct snl_attr_parser _nla_p_bitset[] = {
 static inline bool
 _cb_p_bitset(struct snl_state *ss __unused, void *_target)
 {
-	struct snl_attr_bitset *target = _target;
+	struct snl_attr_bitset *target = (struct snl_attr_bitset *)_target;
 
 	uint32_t sz_bytes = _roundup2(target->nla_bitset_size, 32) / 8;
 
@@ -861,7 +862,7 @@ snl_attr_get_bitset_c(struct snl_state *ss, struct nlattr *nla,
     const void *arg __unused, void *_target)
 {
 	const struct snl_hdr_parser *p = &_nla_bitset_parser;
-	struct snl_attr_bitset *target = _target;
+	struct snl_attr_bitset *target = (struct snl_attr_bitset *)_target;
 
 	/* Assumes target points to the beginning of the structure */
 	if (!snl_parse_header(ss, NLA_DATA(nla), NLA_DATA_LEN(nla), p, _target))
@@ -981,7 +982,7 @@ parse_cmsg(struct snl_state *ss, const struct msghdr *msg, struct snl_msg_info *
 		int len = cmsg->cmsg_len - ((char *)data - (char *)cmsg);
 		const struct snl_hdr_parser *ps = &snl_msg_info_parser;
 
-		return (snl_parse_attrs_raw(ss, data, len, ps->np, ps->np_size, attrs));
+		return (snl_parse_attrs_raw(ss, (struct nlattr *)data, len, ps->np, ps->np_size, attrs));
 	}
 
 	return (false);
@@ -1023,7 +1024,7 @@ static inline void
 snl_init_writer(struct snl_state *ss, struct snl_writer *nw)
 {
 	nw->size = SNL_WRITER_BUFFER_SIZE;
-	nw->base = snl_allocz(ss, nw->size);
+	nw->base = (char *)snl_allocz(ss, nw->size);
 	if (nw->base == NULL) {
 		nw->error = true;
 		nw->size = 0;
@@ -1061,7 +1062,7 @@ snl_realloc_msg_buffer(struct snl_writer *nw, size_t sz)
 			nw->hdr = (struct nlmsghdr *)
 			    (void *)((char *)new_base + hdr_off);
 		}
-		nw->base = new_base;
+		nw->base = (char *)new_base;
 	}
 
 	return (true);
diff --git a/sys/netlink/netlink_snl_route.h b/sys/netlink/netlink_snl_route.h
index 619e769b1496..62055b2db417 100644
--- a/sys/netlink/netlink_snl_route.h
+++ b/sys/netlink/netlink_snl_route.h
@@ -100,7 +100,7 @@ snl_attr_get_ip(struct snl_state *ss, struct nlattr *nla,
 static inline struct sockaddr *
 parse_rta_via(struct snl_state *ss, struct rtattr *rta, int *perror)
 {
-	struct rtvia *via = NL_RTA_DATA(rta);
+	struct rtvia *via = (struct rtvia *)NL_RTA_DATA(rta);
 
 	switch (via->rtvia_family) {
 	case AF_INET:
diff --git a/sys/netlink/netlink_snl_route_parsers.h b/sys/netlink/netlink_snl_route_parsers.h
index 7e4bcad4010b..256edfec297d 100644
--- a/sys/netlink/netlink_snl_route_parsers.h
+++ b/sys/netlink/netlink_snl_route_parsers.h
@@ -78,7 +78,7 @@ static const struct snl_field_parser _fp_p_mp_nh[] = {
 static inline bool
 _cb_p_mp_nh(struct snl_state *ss __unused, void *_target)
 {
-	struct rta_mpath_nh *target = _target;
+	struct rta_mpath_nh *target = (struct rta_mpath_nh *)_target;
 
 	finalize_sockaddr(target->gw, target->ifindex);
 	return (true);
@@ -158,7 +158,7 @@ static const struct snl_field_parser _fp_p_route[] = {
 static inline bool
 _cb_p_route(struct snl_state *ss __unused, void *_target)
 {
-	struct snl_parsed_route *target = _target;
+	struct snl_parsed_route *target = (struct snl_parsed_route *)_target;
 
 	finalize_sockaddr(target->rta_dst, target->rta_oif);
 	finalize_sockaddr(target->rta_gw, target->rta_oif);
@@ -277,7 +277,7 @@ static struct snl_field_parser _fp_p_neigh_s[] = {
 static inline bool
 _cb_p_neigh(struct snl_state *ss __unused, void *_target)
 {
-	struct snl_parsed_neigh *target = _target;
+	struct snl_parsed_neigh *target = (struct snl_parsed_neigh *)_target;
 
 	finalize_sockaddr(target->nda_dst, target->nda_ifindex);
 	return (true);
@@ -326,7 +326,7 @@ static const struct snl_field_parser _fp_p_addr_s[] = {
 static inline bool
 _cb_p_addr(struct snl_state *ss __unused, void *_target)
 {
-	struct snl_parsed_addr *target = _target;
+	struct snl_parsed_addr *target = (struct snl_parsed_addr *)_target;
 
 	finalize_sockaddr(target->ifa_address, target->ifa_index);
 	finalize_sockaddr(target->ifa_local, target->ifa_index);
@@ -379,7 +379,7 @@ static const struct snl_attr_parser _nla_p_nh[] = {
 static inline bool
 _cb_p_nh(struct snl_state *ss __unused, void *_target)
 {
-	struct snl_parsed_nhop *target = _target;
+	struct snl_parsed_nhop *target = (struct snl_parsed_nhop *)_target;
 
 	finalize_sockaddr(target->nha_gw, target->nha_oif);
 	return (true);