Re: [PATCH 5/9] snmplib/transports: Unbreak the MSVC build

Bill Fenner <[email protected]>
Newsgroups gmane.network.net-snmp.devel
Message-ID <CAF4SogYcuqE7heBvgDOx1o9HUcBFjG39PbwZtcQ6m+1hOZ-vHg@mail.gmail.com>
Hi Bart,

I see that when it's a souce, you use "const char *", and when it's a
destination, you use "unsigned char *".  I understand the reason for the
constness difference, but is there a reason for the signedness difference?

  Bill


On Thu, Jun 21, 2018 at 10:23 PM Bart Van Assche <[email protected]> wrote:

> Adding an integer to a void pointer works fine with gcc but not with MSVC.
> Hence convert the expressions that add an integer to a void pointer into
> a construct that is standard C.
> ---
>  snmplib/transports/snmpIPXDomain.c      | 16 ++++++++++------
>  snmplib/transports/snmpIPv4BaseDomain.c | 12 ++++++++----
>  snmplib/transports/snmpIPv6BaseDomain.c | 12 ++++++++----
>  3 files changed, 26 insertions(+), 14 deletions(-)
>
> diff --git a/snmplib/transports/snmpIPXDomain.c
> b/snmplib/transports/snmpIPXDomain.c
> index 55070476ce1e..f5753f5a788a 100644
> --- a/snmplib/transports/snmpIPXDomain.c
> +++ b/snmplib/transports/snmpIPXDomain.c
> @@ -78,9 +78,11 @@ static void netsnmp_ipx_get_taddr(struct
> netsnmp_transport_s *t,
>      netsnmp_assert(t->remote_length == sizeof(*sa));
>      *addr_len = 12;
>      if ((*addr = malloc(*addr_len))) {
> -        memcpy(*addr + 0,  &sa->sipx_network, 4);
> -        memcpy(*addr + 4,  &sa->sipx_node,    6);
> -        memcpy(*addr + 10, &sa->sipx_port,    2);
> +        unsigned char *p = *addr;
> +
> +        memcpy(p + 0,  &sa->sipx_network, 4);
> +        memcpy(p + 4,  &sa->sipx_node,    6);
> +        memcpy(p + 10, &sa->sipx_port,    2);
>      }
>  }
>
> @@ -447,14 +449,16 @@ netsnmp_ipx_create_tstring(const char *str, int
> local,
>  static int netsnmp_ipx_ostring_to_sockaddr(struct sockaddr_ipx *sa,
>                                             const void *o, size_t o_len)
>  {
> +    const char *p = o;
> +
>      if (o_len != 12)
>          return 0;
>
>      memset(sa, 0, sizeof(*sa));
>      sa->sipx_family = AF_IPX;
> -    memcpy(&sa->sipx_network, o + 0, 4);
> -    memcpy(&sa->sipx_node,    o + 4, 6);
> -    memcpy(&sa->sipx_port,    o + 10, 2);
> +    memcpy(&sa->sipx_network, p + 0, 4);
> +    memcpy(&sa->sipx_node,    p + 4, 6);
> +    memcpy(&sa->sipx_port,    p + 10, 2);
>      return 1;
>  }
>
> diff --git a/snmplib/transports/snmpIPv4BaseDomain.c
> b/snmplib/transports/snmpIPv4BaseDomain.c
> index fafc1ab8045c..37ef72d4ed42 100644
> --- a/snmplib/transports/snmpIPv4BaseDomain.c
> +++ b/snmplib/transports/snmpIPv4BaseDomain.c
> @@ -251,20 +251,24 @@ void netsnmp_ipv4_get_taddr(struct
> netsnmp_transport_s *t, void **addr,
>
>      *addr_len = 6;
>      if ((*addr = malloc(*addr_len))) {
> -        memcpy(*addr,     &sin->sin_addr, 4);
> -        memcpy(*addr + 4, &sin->sin_port, 2);
> +        unsigned char *p = *addr;
> +
> +        memcpy(p,     &sin->sin_addr, 4);
> +        memcpy(p + 4, &sin->sin_port, 2);
>      }
>  }
>
>  int netsnmp_ipv4_ostring_to_sockaddr(struct sockaddr_in *sin, const void
> *o,
>                                       size_t o_len)
>  {
> +    const char *p = o;
> +
>      if (o_len != 6)
>          return 0;
>
>      memset(sin, 0, sizeof(*sin));
>      sin->sin_family = AF_INET;
> -    memcpy(&sin->sin_addr, o + 0, 4);
> -    memcpy(&sin->sin_port, o + 4, 2);
> +    memcpy(&sin->sin_addr, p + 0, 4);
> +    memcpy(&sin->sin_port, p + 4, 2);
>      return 1;
>  }
> diff --git a/snmplib/transports/snmpIPv6BaseDomain.c
> b/snmplib/transports/snmpIPv6BaseDomain.c
> index c4a801f87228..46f70c028cda 100644
> --- a/snmplib/transports/snmpIPv6BaseDomain.c
> +++ b/snmplib/transports/snmpIPv6BaseDomain.c
> @@ -160,21 +160,25 @@ void netsnmp_ipv6_get_taddr(struct
> netsnmp_transport_s *t, void **addr,
>
>      *addr_len = 18;
>      if ((*addr = malloc(*addr_len))) {
> -        memcpy(*addr,      &sin6->sin6_addr, 16);
> -        memcpy(*addr + 16, &sin6->sin6_port, 2);
> +        unsigned char *p = *addr;
> +
> +        memcpy(p,      &sin6->sin6_addr, 16);
> +        memcpy(p + 16, &sin6->sin6_port, 2);
>      }
>  }
>
>  int netsnmp_ipv6_ostring_to_sockaddr(struct sockaddr_in6 *sin6, const
> void *o,
>                                       size_t o_len)
>  {
> +    const char *p = o;
> +
>      if (o_len != 18)
>          return 0;
>
>      memset(sin6, 0, sizeof(*sin6));
>      sin6->sin6_family = AF_INET6;
> -    memcpy(&sin6->sin6_addr, o + 0,  16);
> -    memcpy(&sin6->sin6_port, o + 16, 2);
> +    memcpy(&sin6->sin6_addr, p + 0,  16);
> +    memcpy(&sin6->sin6_port, p + 16, 2);
>      return 1;
>  }
>
> --
> 2.17.1
>
>
>
> ------------------------------------------------------------------------------
> Check out the vibrant tech community on one of the world's most
> engaging tech sites, Slashdot.org! http://sdm.link/slashdot
> _______________________________________________
> Net-snmp-coders mailing list
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/net-snmp-coders
>

------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot

_______________________________________________
Net-snmp-coders mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/net-snmp-coders
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.