changeset 11752:3ddd2d3e5d19

Fix handling of large len argument.
author Bruno Haible <bruno@clisp.org>
date Sun, 02 Aug 2009 12:05:11 +0200
parents 8153fe87d193
children b465c20bb96c
files ChangeLog lib/gethostname.c
diffstat 2 files changed, 23 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- 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.
--- a/lib/gethostname.c
+++ b/lib/gethostname.c
@@ -21,6 +21,7 @@
 #include <config.h>
 
 #if !((defined _WIN32 || defined __WIN32__) && !defined __CYGWIN__)
+/* Unix API.  */
 
 /* Specification.  */
 #include <unistd.h>
@@ -59,6 +60,17 @@
 }
 
 #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 @@
 #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 ();