about summary refs log tree commit diff
path: root/inet
diff options
context:
space:
mode:
Diffstat (limited to 'inet')
-rw-r--r--inet/Makefile2
-rw-r--r--inet/getnameinfo.c2
-rw-r--r--inet/inet_ntoa.c29
-rw-r--r--inet/tst-ntoa.c36
4 files changed, 54 insertions, 15 deletions
diff --git a/inet/Makefile b/inet/Makefile
index 9a70c316b5..18a990ef71 100644
--- a/inet/Makefile
+++ b/inet/Makefile
@@ -46,7 +46,7 @@ routines := htonl htons		\
 	    getaliasent_r getaliasent getaliasname getaliasname_r \
 	    in6_addr getnameinfo if_index
 
-tests := htontest test_ifindex
+tests := htontest test_ifindex tst-ntoa
 
 # No warnings about losing BSD code.
 CFLAGS-rcmd.c = -w
diff --git a/inet/getnameinfo.c b/inet/getnameinfo.c
index e6f4cbfc94..f090f0b32e 100644
--- a/inet/getnameinfo.c
+++ b/inet/getnameinfo.c
@@ -80,7 +80,7 @@ struct hostent *_addr2hostname_hosts (const char *, int, int);
 static const char *
 nrl_domainname (void)
 {
-  static const char *domain = NULL;
+  static char *domain = NULL;
   static int first = 1;
 
   if (first)
diff --git a/inet/inet_ntoa.c b/inet/inet_ntoa.c
index 5dfec626a0..fc2c3dd9b8 100644
--- a/inet/inet_ntoa.c
+++ b/inet/inet_ntoa.c
@@ -43,7 +43,7 @@ static void free_key_mem (void *mem);
 char *
 inet_ntoa (struct in_addr in)
 {
-  __libc_once_define (once);
+  __libc_once_define (static, once);
   char *buffer;
   unsigned char *bytes;
 
@@ -51,20 +51,22 @@ inet_ntoa (struct in_addr in)
   __libc_once (once, init);
 
   if (static_buf != NULL)
-    return static_buf;
-
-  /* We don't use the static buffer and so we have a key.  Use it to
-     get the thread-specific buffer.  */
-  buffer = __libc_getspecific (key);
-  if (buffer == NULL)
+    buffer = static_buf;
+  else
     {
-      /* No buffer allocated so far.  */
-      buffer = malloc (18);
+      /* We don't use the static buffer and so we have a key.  Use it
+	 to get the thread-specific buffer.  */
+      buffer = __libc_getspecific (key);
       if (buffer == NULL)
-	/* No more memory available.  We use the static buffer.  */
-	buffer = local_buf;
-      else
-	__libc_setspecific (key, buffer);
+	{
+	  /* No buffer allocated so far.  */
+	  buffer = malloc (18);
+	  if (buffer == NULL)
+	    /* No more memory available.  We use the static buffer.  */
+	    buffer = local_buf;
+	  else
+	    __libc_setspecific (key, buffer);
+	}
     }
 
   bytes = (unsigned char *) ∈
@@ -91,4 +93,5 @@ static void
 free_key_mem (void *mem)
 {
   free (mem);
+  __libc_setspecific (key, NULL);
 }
diff --git a/inet/tst-ntoa.c b/inet/tst-ntoa.c
new file mode 100644
index 0000000000..9be91eb511
--- /dev/null
+++ b/inet/tst-ntoa.c
@@ -0,0 +1,36 @@
+#include <stdio.h>
+#include <string.h>
+#include <arpa/inet.h>
+#include <netinet/in.h>
+
+
+static int
+test (unsigned int inaddr, const char *expected)
+{
+  struct in_addr addr;
+  char *res;
+  int fail;
+
+  addr.s_addr = htonl (inaddr);
+  res = inet_ntoa (addr);
+  fail = strcmp (res, expected);
+
+  printf ("%#010x -> \"%s\" -> %s%s\n", inaddr, res,
+	  fail ? "fail, expected" : "ok", fail ? expected : "");
+
+  return fail;
+}
+
+
+int
+main (void)
+{
+  int result = 0;
+
+  result |= test (INADDR_LOOPBACK, "127.0.0.1");
+  result |= test (INADDR_BROADCAST, "255.255.255.255");
+  result |= test (INADDR_ANY, "0.0.0.0");
+  result |= test (0xc0060746, "192.6.7.70");
+
+  return result;
+}