Re: mu_str_to_c test fails (endianness problem)

Sergey Poznyakoff <[email protected]>
Newsgroups gmane.comp.gnu.mailutils.bugs
Organization GNU.org.ua
Message-ID <[email protected]>
Hi Eray,

> failing mu_str_to_c test on sparc64

Thanks for reporting. Please try the attached patch. Please let me know
if it works for you.

Regards,
Sergey

_______________________________________________
Bug-mailutils mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/bug-mailutils
0001-Fix-endianness-bug-in-string-to-IP-conversion.patch (text/x-diff, 2.8 KB)
From 32b9ab6f3f789bb30e511eca9b071797619fe826 Mon Sep 17 00:00:00 2001
From: Sergey Poznyakoff <[email protected]>
Date: Thu, 21 Jun 2018 09:46:43 +0300
Subject: [PATCH] Fix endianness bug in string to IP conversion

* libmailutils/cidr/fromsa.c (_mu_inaddr_to_bytes)
(_mu_sockaddr_to_bytes): Fix improper endianness conversion.
* libmailutils/cidr/tosa.c (mu_cidr_to_sockaddr): Simplify conversion.
---
 libmailutils/cidr/fromsa.c | 45 ++++++++++++++++++++-------------------------
 libmailutils/cidr/tosa.c   |  8 ++------
 2 files changed, 22 insertions(+), 31 deletions(-)

diff --git a/libmailutils/cidr/fromsa.c b/libmailutils/cidr/fromsa.c
index f57aadc..39d24fd 100644
--- a/libmailutils/cidr/fromsa.c
+++ b/libmailutils/cidr/fromsa.c
@@ -29,55 +29,50 @@
 #include <mailutils/cidr.h>
 #include <mailutils/errno.h>
 
-static void
-uint32_to_bytes (unsigned char *bytes, uint32_t u)
-{
-  int i;
-  
-  for (i = 0; i < 4; i++)
-    {
-      bytes[i] = u & 0xff;
-      u >>= 8;
-    }
-}
-
 int
 _mu_inaddr_to_bytes (int af, void *buf, unsigned char *bytes)
 {
-  uint32_t u;
+  size_t len;
   
   switch (af)
     {
     case AF_INET:
-      memcpy (&u, buf, sizeof u);
-      uint32_to_bytes (bytes, u);
-      return 4;
-
+      len = 4;
+      break;
+      
 #ifdef MAILUTILS_IPV6
     case AF_INET6:
-      memcpy (bytes, buf, 16);
-      return 16;
+      len = 16;
+      break;
 #endif
+
+    default:
+      len = 0;
     }
-  return 0;
+  memcpy (bytes, buf, len);
+  return len;
 }
 
 int
 _mu_sockaddr_to_bytes (unsigned char *bytes, struct sockaddr const *sa)
 {
+  void *buf;
   switch (sa->sa_family)
     {
     case AF_INET:
-      uint32_to_bytes (bytes, ((struct sockaddr_in*)sa)->sin_addr.s_addr);
-      return 4;
+      buf = &(((struct sockaddr_in*)sa)->sin_addr.s_addr);
+      break;
 
 #ifdef MAILUTILS_IPV6
     case AF_INET6:
-      memcpy (bytes, &((struct sockaddr_in6*)sa)->sin6_addr, 16);
-      return 16;
+      buf = &(((struct sockaddr_in6*)sa)->sin6_addr);
+      break;
 #endif
+      
+    default:
+      return 0;
     }
-  return 0;
+  return _mu_inaddr_to_bytes (sa->sa_family, buf, bytes);
 }
 
 int
diff --git a/libmailutils/cidr/tosa.c b/libmailutils/cidr/tosa.c
index 33715e1..9256391 100644
--- a/libmailutils/cidr/tosa.c
+++ b/libmailutils/cidr/tosa.c
@@ -43,18 +43,14 @@ mu_cidr_to_sockaddr (struct mu_cidr *cidr, struct sockaddr **psa)
   struct sockaddr *sa;
   int socklen;
   int i;
-  
+
   memset (&addr, 0, sizeof (addr));
   addr.sa.sa_family = cidr->family;
   switch (cidr->family)
     {
     case AF_INET:
       socklen = sizeof (addr.s_in);
-      for (i = 0; i < cidr->len; i++)
-	{
-	  addr.s_in.sin_addr.s_addr <<= 8;
-	  addr.s_in.sin_addr.s_addr |= cidr->address[i];
-	}
+      memcpy (&addr.s_in.sin_addr.s_addr, cidr->address, 4);
       break;
 
 #ifdef MAILUTILS_IPV6
-- 
1.8.4
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.