From 45ba61a18c1427acf1603ad97e8f49b579fa1ab6 Mon Sep 17 00:00:00 2001 From: Bruno Haible Date: Sun, 2 Aug 2009 12:05:11 +0200 Subject: [PATCH] Fix handling of large len argument. --- ChangeLog | 5 +++++ lib/gethostname.c | 20 ++++++++++++++++++-- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/ChangeLog b/ChangeLog index 68506e242..7bab96028 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +2009-08-02 Bruno Haible + + * lib/gethostname.c (gethostname): Fix handling of large len argument. + Add comments. + 2009-03-31 Simon Josefsson * lib/gethostname.c: Add Windows wrapper. diff --git a/lib/gethostname.c b/lib/gethostname.c index 782c4028f..ef58a4031 100644 --- a/lib/gethostname.c +++ b/lib/gethostname.c @@ -21,6 +21,7 @@ #include #if !((defined _WIN32 || defined __WIN32__) && !defined __CYGWIN__) +/* Unix API. */ /* Specification. */ #include @@ -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 (); -- 2.11.0