On mån, 2007-01-08 at 08:15 +0100, Thomas Anders wrote:
> Magnus Fromreide wrote:
> > I wasn't aware that we should write the implementations in the common
> > subset of C and C++ but if that is the case then I could do so.
> > 
> > All of the reports below are the result of code like
> > 
> > aType* foo = malloc(sizeof(aType));
> > 
> > and that is perfectly valid C, but it could be changed to
> > 
> > aType* foo = (aType*)malloc(sizeof(aType));
> > 
> > to move it into the common subset of C and C++.
> 
> As most of the code already builds fine with g++, /me thinks it should
> be done.

The attached patch makes it possible to build snmplib/ with a c++
compiler as well as a c89 compiler.

The important parts of this patch are:
      * chunk 2 in snmpUDPDomain.c which makes it stop using C99 struct
        initialization at the cost of doing actual assignments.
      * chunk 4 in snmpSTDDomain.c where t->data_length was initialized
        from sizeof(netsnmp_transport_free). The C++ compiler disliked
        sizeof(function). I'd appreciate if Wes could comment on this
        chunk.

/MF
Index: snmplib/snmpAAL5PVCDomain.c
===================================================================
RCS file: /cvsroot/net-snmp/net-snmp/snmplib/snmpAAL5PVCDomain.c,v
retrieving revision 5.6
diff -u -r5.6 snmpAAL5PVCDomain.c
--- snmplib/snmpAAL5PVCDomain.c	19 Sep 2006 14:45:29 -0000	5.6
+++ snmplib/snmpAAL5PVCDomain.c	8 Jan 2007 20:36:26 -0000
@@ -216,7 +216,7 @@
     }
 
     if (local) {
-        t->local = malloc(8);
+        t->local = (unsigned char*)malloc(8);
         if (t->local == NULL) {
             netsnmp_transport_free(t);
             return NULL;
@@ -240,7 +240,7 @@
             return NULL;
         }
     } else {
-        t->remote = malloc(8);
+        t->remote = (unsigned char*)malloc(8);
         if (t->remote == NULL) {
             netsnmp_transport_free(t);
             return NULL;
@@ -346,7 +346,7 @@
 {
     aal5pvcDomain.name = netsnmp_AAL5PVCDomain;
     aal5pvcDomain.name_length = sizeof(netsnmp_AAL5PVCDomain) / sizeof(oid);
-    aal5pvcDomain.prefix = calloc(3, sizeof(char *));
+    aal5pvcDomain.prefix = (const char**)calloc(3, sizeof(char *));
     aal5pvcDomain.prefix[0] = "aal5pvc";
     aal5pvcDomain.prefix[1] = "pvc";
 
Index: snmplib/snmpIPXDomain.c
===================================================================
RCS file: /cvsroot/net-snmp/net-snmp/snmplib/snmpIPXDomain.c,v
retrieving revision 5.4
diff -u -r5.4 snmpIPXDomain.c
--- snmplib/snmpIPXDomain.c	19 Sep 2006 14:45:29 -0000	5.4
+++ snmplib/snmpIPXDomain.c	8 Jan 2007 20:36:26 -0000
@@ -76,7 +76,8 @@
 netsnmp_ipx_recv(netsnmp_transport *t, void *buf, int size,
 		 void **opaque, int *olength)
 {
-    int rc = -1, fromlen = sizeof(struct sockaddr);
+    int		     rc = -1;
+    socklen_t	     fromlen = sizeof(struct sockaddr);
     struct sockaddr *from;
 
     if (t != NULL && t->sock >= 0) {
@@ -203,7 +204,7 @@
     }
 
     if (local) {
-        t->local = malloc(12);
+        t->local = (unsigned char*)malloc(12);
         if (t->local == NULL) {
             netsnmp_transport_free(t);
             return NULL;
@@ -229,7 +230,7 @@
         t->data = NULL;
         t->data_length = 0;
     } else {
-        t->remote = malloc(12);
+        t->remote = (unsigned char*)malloc(12);
         if (t->remote == NULL) {
             netsnmp_transport_free(t);
             return NULL;
@@ -442,7 +443,7 @@
 {
     ipxDomain.name = netsnmpIPXDomain;
     ipxDomain.name_length = netsnmpIPXDomain_len;
-    ipxDomain.prefix = calloc(2, sizeof(char *));
+    ipxDomain.prefix = (const char**)calloc(2, sizeof(char *));
     ipxDomain.prefix[0] = "ipx";
 
     ipxDomain.f_create_from_tstring_new = netsnmp_ipx_create_tstring;
Index: snmplib/snmpSTDDomain.c
===================================================================
RCS file: /cvsroot/net-snmp/net-snmp/snmplib/snmpSTDDomain.c,v
retrieving revision 1.4
diff -u -r1.4 snmpSTDDomain.c
--- snmplib/snmpSTDDomain.c	19 Sep 2006 14:45:29 -0000	1.4
+++ snmplib/snmpSTDDomain.c	8 Jan 2007 20:36:26 -0000
@@ -42,8 +42,8 @@
     char *buf;
     DEBUGMSGTL(("domain:std","formatting addr.  data=%x\n",t->data));
     if (t->data) {
-        netsnmp_std_data *data = t->data;
-        buf = malloc(SNMP_MAXBUF_MEDIUM);
+        netsnmp_std_data *data = (netsnmp_std_data*)t->data;
+        buf = (char*)malloc(SNMP_MAXBUF_MEDIUM);
         if (!buf)
             return strdup("STDInOut");
         snprintf(buf, SNMP_MAXBUF_MEDIUM, "STD:%s", data->prog);
@@ -97,7 +97,7 @@
     DEBUGMSGTL(("domain:std","send on sock.  data=%x\n", t->data));
     while (rc < 0) {
         if (t->data) {
-            netsnmp_std_data *data = t->data;
+            netsnmp_std_data *data = (netsnmp_std_data*)t->data;
             rc = write(data->outfd, buf, size);
         } else {
             /* straight to stdout */
@@ -115,7 +115,7 @@
 {
     DEBUGMSGTL(("domain:std","close.  data=%x\n", t->data));
     if (t->data) {
-        netsnmp_std_data *data = t->data;
+        netsnmp_std_data *data = (netsnmp_std_data*)t->data;
         close(data->outfd);
         close(t->sock);
 
@@ -217,7 +217,7 @@
                 return NULL;
             }
             t->data = data;
-            t->data_length = sizeof(netsnmp_transport_free);
+            t->data_length = sizeof(netsnmp_std_data);
             t->sock = outfd[0];
             data->prog = strdup(instring);
             data->outfd = infd[1];
@@ -266,7 +266,7 @@
 netsnmp_transport *
 netsnmp_std_create_ostring(const u_char * o, size_t o_len, int local)
 {
-    return netsnmp_std_transport(o, o_len, NULL);
+    return netsnmp_std_transport((const char*)o, o_len, NULL);
 }
 
 void
@@ -274,7 +274,7 @@
 {
     stdDomain.name = netsnmp_snmpSTDDomain;
     stdDomain.name_length = sizeof(netsnmp_snmpSTDDomain) / sizeof(oid);
-    stdDomain.prefix = calloc(2, sizeof(char *));
+    stdDomain.prefix = (const char **)calloc(2, sizeof(char *));
     stdDomain.prefix[0] = "std";
 
     stdDomain.f_create_from_tstring_new = netsnmp_std_create_tstring;
Index: snmplib/snmpTCPDomain.c
===================================================================
RCS file: /cvsroot/net-snmp/net-snmp/snmplib/snmpTCPDomain.c,v
retrieving revision 5.18
diff -u -r5.18 snmpTCPDomain.c
--- snmplib/snmpTCPDomain.c	15 Oct 2006 21:43:16 -0000	5.18
+++ snmplib/snmpTCPDomain.c	8 Jan 2007 20:36:26 -0000
@@ -196,7 +196,7 @@
         DEBUGMSGTL(("netsnmp_tcp", "accept: malloc failed\n"));
         return -1;
     }
-    farend = (struct sockaddr_in *) &(addr_pair->remote_addr);
+    farend = (struct sockaddr *) &(addr_pair->remote_addr);
 
     if (t != NULL && t->sock >= 0) {
         newsock = accept(t->sock, farend, &farendlen);
Index: snmplib/snmpTCPIPv6Domain.c
===================================================================
RCS file: /cvsroot/net-snmp/net-snmp/snmplib/snmpTCPIPv6Domain.c,v
retrieving revision 5.17
diff -u -r5.17 snmpTCPIPv6Domain.c
--- snmplib/snmpTCPIPv6Domain.c	19 Sep 2006 14:45:30 -0000	5.17
+++ snmplib/snmpTCPIPv6Domain.c	8 Jan 2007 20:36:27 -0000
@@ -322,7 +322,7 @@
 #endif
 
         t->flags |= NETSNMP_TRANSPORT_FLAG_LISTEN;
-        t->local = malloc(18);
+        t->local = (unsigned char*)malloc(18);
         if (t->local == NULL) {
             netsnmp_tcp6_close(t);
             netsnmp_transport_free(t);
@@ -379,7 +379,7 @@
          */
 
     } else {
-        t->remote = malloc(18);
+        t->remote = (unsigned char*)malloc(18);
         if (t->remote == NULL) {
             netsnmp_tcp6_close(t);
             netsnmp_transport_free(t);
@@ -484,7 +484,7 @@
     tcp6Domain.name_length = sizeof(netsnmp_TCPIPv6Domain) / sizeof(oid);
     tcp6Domain.f_create_from_tstring_new = netsnmp_tcp6_create_tstring;
     tcp6Domain.f_create_from_ostring = netsnmp_tcp6_create_ostring;
-    tcp6Domain.prefix = calloc(4, sizeof(char *));
+    tcp6Domain.prefix = (const char**)calloc(4, sizeof(char *));
     tcp6Domain.prefix[0] = "tcp6";
     tcp6Domain.prefix[1] = "tcpv6";
     tcp6Domain.prefix[2] = "tcpipv6";
Index: snmplib/snmpUDPDomain.c
===================================================================
RCS file: /cvsroot/net-snmp/net-snmp/snmplib/snmpUDPDomain.c,v
retrieving revision 5.46
diff -u -r5.46 snmpUDPDomain.c
--- snmplib/snmpUDPDomain.c	2 Jan 2007 16:00:27 -0000	5.46
+++ snmplib/snmpUDPDomain.c	8 Jan 2007 20:36:28 -0000
@@ -116,7 +116,7 @@
 
 # define netsnmp_dstaddr(x) (&(((struct in_pktinfo *)(CMSG_DATA(x)))->ipi_addr))
 
-static int netsnmp_udp_recvfrom(int s, char *buf, int len, struct sockaddr *from, socklen_t *fromlen, struct in_addr *dstip)
+static int netsnmp_udp_recvfrom(int s, void *buf, int len, struct sockaddr *from, socklen_t *fromlen, struct in_addr *dstip)
 {
     int r;
     struct iovec iov[1];
@@ -153,32 +153,30 @@
 }
 
 static int netsnmp_udp_sendto(int fd, struct in_addr *srcip, struct sockaddr *remote,
-			char *data, int len)
+			void *data, int len)
 {
     struct iovec iov = { data, len };
     struct {
         struct cmsghdr cm;
         struct in_pktinfo ipi;
-    } cmsg = {
-        .cm = {
-            .cmsg_len	= sizeof(struct cmsghdr) + sizeof(struct in_pktinfo),
-            .cmsg_level	= SOL_IP,
-            .cmsg_type	= IP_PKTINFO,
-        },
-        .ipi = {
-            .ipi_ifindex	= 0,
-            .ipi_spec_dst	= srcip ? srcip->s_addr : 0,
-        },
-    };
-    struct msghdr m = {
-        .msg_name	= remote,
-        .msg_namelen	= sizeof(struct sockaddr_in),
-        .msg_iov	= &iov,
-        .msg_iovlen	= 1,
-        .msg_control	= &cmsg,
-        .msg_controllen	= sizeof(cmsg),
-        .msg_flags	= 0,
-    };
+    } cmsg;
+
+    cmsg.cm.cmsg_len = sizeof(struct cmsghdr) + sizeof(struct in_pktinfo);
+    cmsg.cm.cmsg_level = SOL_IP;
+    cmsg.cm.cmsg_type = IP_PKTINFO;
+    cmsg.ipi.ipi_ifindex = 0;
+    cmsg.ipi.ipi_spec_dst.s_addr = (srcip ? srcip->s_addr : INADDR_ANY);
+
+    struct msghdr m;
+
+    m.msg_name		= remote;
+    m.msg_namelen	= sizeof(struct sockaddr_in);
+    m.msg_iov		= &iov;
+    m.msg_iovlen	= 1;
+    m.msg_control	= &cmsg;
+    m.msg_controllen	= sizeof(cmsg);
+    m.msg_flags		= 0;
+
     return sendmsg(fd, &m, MSG_NOSIGNAL|MSG_DONTWAIT);
 }
 #endif /* linux && IP_PKTINFO */
Index: snmplib/snmpUDPIPv6Domain.c
===================================================================
RCS file: /cvsroot/net-snmp/net-snmp/snmplib/snmpUDPIPv6Domain.c,v
retrieving revision 5.38
diff -u -r5.38 snmpUDPIPv6Domain.c
--- snmplib/snmpUDPIPv6Domain.c	2 Jan 2007 16:00:27 -0000	5.38
+++ snmplib/snmpUDPIPv6Domain.c	8 Jan 2007 20:36:29 -0000
@@ -287,7 +287,7 @@
             netsnmp_transport_free(t);
             return NULL;
         }
-        t->local = malloc(18);
+        t->local = (unsigned char*)malloc(18);
         if (t->local == NULL) {
             netsnmp_udp6_close(t);
             netsnmp_transport_free(t);
@@ -313,7 +313,7 @@
         }
         memcpy(t->data, addr, sizeof(struct sockaddr_in6));
         t->data_length = sizeof(struct sockaddr_in6);
-        t->remote = malloc(18);
+        t->remote = (unsigned char*)malloc(18);
         if (t->remote == NULL) {
             netsnmp_udp6_close(t);
             netsnmp_transport_free(t);
@@ -1363,7 +1363,7 @@
     udp6Domain.name_length = sizeof(netsnmp_UDPIPv6Domain) / sizeof(oid);
     udp6Domain.f_create_from_tstring_new = netsnmp_udp6_create_tstring;
     udp6Domain.f_create_from_ostring = netsnmp_udp6_create_ostring;
-    udp6Domain.prefix = calloc(5, sizeof(char *));
+    udp6Domain.prefix = (const char**)calloc(5, sizeof(char *));
     udp6Domain.prefix[0] = "udp6";
     udp6Domain.prefix[1] = "ipv6";
     udp6Domain.prefix[2] = "udpv6";
Index: snmplib/snmp_service.c
===================================================================
RCS file: /cvsroot/net-snmp/net-snmp/snmplib/snmp_service.c,v
retrieving revision 5.2
diff -u -r5.2 snmp_service.c
--- snmplib/snmp_service.c	1 Jan 2007 14:38:41 -0000	5.2
+++ snmplib/snmp_service.c	8 Jan 2007 20:36:33 -0000
@@ -32,7 +32,7 @@
 	  res = 1;
       }
     } else {
-	run = malloc(sizeof(struct netsnmp_lookup_domain));
+	run = SNMP_MALLOC_STRUCT(netsnmp_lookup_domain);
 	run->application = strdup(application);
 	run->userDomain = NULL;
 	if (prev) {
@@ -74,8 +74,8 @@
 {
     struct netsnmp_lookup_domain *run = domains, *prev = NULL;
     size_t len = strlen(cptr) + 1;
-    char* application = malloc(len);
-    char* domain = malloc(len);
+    char* application = (char*)malloc(len);
+    char* domain = (char*)malloc(len);
 
     {
 	char* cp = copy_nword(cptr, application, len);
@@ -95,7 +95,7 @@
 	    goto done;
 	}
     } else {
-	run = malloc(sizeof(struct netsnmp_lookup_domain));
+	run = SNMP_MALLOC_STRUCT(netsnmp_lookup_domain);
 	run->application = strdup(application);
 	run->domain = NULL;
 	if (prev) {
@@ -192,7 +192,7 @@
 	    res = 1;
       }
     } else {
-	run = malloc(sizeof(struct netsnmp_lookup_target));
+	run = SNMP_MALLOC_STRUCT(netsnmp_lookup_target);
 	run->application = strdup(application);
 	run->domain = strdup(domain);
 	run->userTarget = NULL;
@@ -237,9 +237,9 @@
 {
     struct netsnmp_lookup_target *run = targets, *prev = NULL;
     size_t len = strlen(cptr) + 1;
-    char* application = malloc(len);
-    char* domain = malloc(len);
-    char* target = malloc(len);
+    char* application = (char*)malloc(len);
+    char* domain = (char*)malloc(len);
+    char* target = (char*)malloc(len);
     int i;
 
     {
@@ -262,7 +262,7 @@
 	    goto done;
 	}
     } else {
-	run = malloc(sizeof(struct netsnmp_lookup_target));
+	run = SNMP_MALLOC_STRUCT(netsnmp_lookup_target);
 	run->application = strdup(application);
 	run->domain = strdup(domain);
 	run->target = NULL;
Index: snmplib/snmp_transport.c
===================================================================
RCS file: /cvsroot/net-snmp/net-snmp/snmplib/snmp_transport.c,v
retrieving revision 5.13
diff -u -r5.13 snmp_transport.c
--- snmplib/snmp_transport.c	19 Sep 2006 14:45:30 -0000	5.13
+++ snmplib/snmp_transport.c	8 Jan 2007 20:36:34 -0000
@@ -24,6 +24,9 @@
 #ifdef NETSNMP_TRANSPORT_TLS_DOMAIN
 #include <net-snmp/library/snmpTLSDomain.h>
 #endif
+#ifdef NETSNMP_TRANSPORT_STD_DOMAIN
+#include <net-snmp/library/snmpSTDDomain.h>
+#endif
 #ifdef NETSNMP_TRANSPORT_TCP_DOMAIN
 #include <net-snmp/library/snmpTCPDomain.h>
 #endif
-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys - and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
Net-snmp-coders mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/net-snmp-coders

Reply via email to