Fix handling of large len argument.
authorBruno Haible <bruno@clisp.org>
Sun, 2 Aug 2009 10:05:11 +0000 (12:05 +0200)
committerBruno Haible <bruno@clisp.org>
Sun, 2 Aug 2009 10:05:11 +0000 (12:05 +0200)
ChangeLog
lib/gethostname.c

index 68506e2..7bab960 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,8 @@
+2009-08-02  Bruno Haible  <bruno@clisp.org>
+
+       * lib/gethostname.c (gethostname): Fix handling of large len argument.
+       Add comments.
+
 2009-03-31  Simon Josefsson  <simon@josefsson.org>
 
        * lib/gethostname.c: Add Windows wrapper.
index 782c402..ef58a40 100644 (file)
@@ -21,6 +21,7 @@
 #include <config.h>
 
 #if !((defined _WIN32 || defined __WIN32__) && !defined __CYGWIN__)
+/* Unix API.  */
 
 /* Specification.  */
 #include <unistd.h>
@@ -59,6 +60,17 @@ gethostname (char *name, size_t len)
 }
 
 #else
+/* Native Windows API.  Which primitive to choose?
+   - gethostname() requires linking with -lws2_32.
+   - GetComputerName() does not return the right kind of hostname.
+   - GetComputerNameEx(ComputerNameDnsHostname,...) returns the right hostname,
+     but it hard to use portably:
+       - It requires defining _WIN32_WINNT to at least 0x0500.
+       - With mingw, it also requires
+         "#define GetComputerNameEx GetComputerNameExA".
+       - With older versions of mingw, none of the declarations are present at
+         all, not even of the enum value ComputerNameDnsHostname.
+   So we use gethostname().  Linking with -lws2_32 is the least evil.  */
 
 #define WIN32_LEAN_AND_MEAN
 /* Get winsock2.h. */
@@ -70,9 +82,13 @@ gethostname (char *name, size_t len)
 #undef gethostname
 
 int
-rpl_gethostname (char *name, size_t namelen)
+rpl_gethostname (char *name, size_t len)
 {
-  int r = gethostname (name, (int) namelen);
+  int r;
+
+  if (len > INT_MAX)
+    len = INT_MAX;
+  r = gethostname (name, (int) len);
   if (r < 0)
     set_winsock_errno ();