public inbox for cygwin-patches@cygwin.com
 help / color / mirror / Atom feed
* Fix error mapping in gethostname
@ 2015-03-31 18:24 Renato Silva
  2015-03-31 19:20 ` Corinna Vinschen
  0 siblings, 1 reply; 2+ messages in thread
From: Renato Silva @ 2015-03-31 18:24 UTC (permalink / raw)
  To: Cygwin Patches

The gethostname function has a problem where a small buffer size will
not produce an accurate errno. This is because the Windows error is
not being appropriately mapped. This causes programs such as hostname
from coreutils to fail because they are not informed about the long
name.

Changelog entry:
2015-03-31  Renato Silva  <br.renatosilva@gmail.com>
    * net.cc: Fix buffer size error handling in cygwin_gethostname.

-----

/* Test case */

#include <errno.h>
#include <stdio.h>
#include <windows.h>

int main(int argc, char **argv) {

    if (argc < 2) {
        printf("Please provide a buffer length.\n");
        return 1;
    }
    DWORD HOSTNAME_LENGTH = atoi(argv[1]);
    char hostname[HOSTNAME_LENGTH];
    char error_message[256];
    int return_value;

    printf("gethostname %s\n", gethostname(hostname, HOSTNAME_LENGTH)?
"failed" : "succeeded");
    if (errno) printf("error is %d, %s\n\n", errno, strerror(errno));
          else printf("hostname is %s\n\n", hostname);

    printf("GetComputerNameEx %s\n", (return_value =
GetComputerNameEx(ComputerNameDnsFullyQualified, hostname,
&HOSTNAME_LENGTH))? "succeeded": "failed");
    FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM, NULL, GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), error_message, 256, NULL);
    if (!return_value) printf("error is %d, %s\n", GetLastError(),
error_message);
                  else printf("hostname is %s\n", hostname);
}

-----

From d691d4b2ac75f00a752c5dc86ab63a4ba425beda Mon Sep 17 00:00:00 2001
From: Renato Silva <br.renatosilva@gmail.com>
Date: Mon, 30 Mar 2015 20:20:49 -0300
Subject: [PATCH] Fix buffer size error handling in gethostname.

GetComputerNameEx sets a generic ERROR_MORE_DATA when buffer is too small. This
is now more accurately mapped into ENAMETOOLONG instead of the generic EPERM.
---
 winsup/cygwin/net.cc | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/winsup/cygwin/net.cc b/winsup/cygwin/net.cc
index f9b317c..02fa142 100644
--- a/winsup/cygwin/net.cc
+++ b/winsup/cygwin/net.cc
@@ -1076,7 +1076,10 @@ cygwin_gethostname (char *name, size_t len)
       if (!GetComputerNameExA (ComputerNameDnsFullyQualified, name,
                    &local_len))
         {
-          set_winsock_errno ();
+          if (GetLastError () == ERROR_MORE_DATA)
+            set_errno (ENAMETOOLONG);
+          else
+            set_winsock_errno ();
           __leave;
         }
     }
-- 
2.3.4

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: Fix error mapping in gethostname
  2015-03-31 18:24 Fix error mapping in gethostname Renato Silva
@ 2015-03-31 19:20 ` Corinna Vinschen
  0 siblings, 0 replies; 2+ messages in thread
From: Corinna Vinschen @ 2015-03-31 19:20 UTC (permalink / raw)
  To: cygwin-patches

[-- Attachment #1: Type: text/plain, Size: 661 bytes --]

On Mar 31 15:23, Renato Silva wrote:
> The gethostname function has a problem where a small buffer size will
> not produce an accurate errno. This is because the Windows error is
> not being appropriately mapped. This causes programs such as hostname
> from coreutils to fail because they are not informed about the long
> name.
> 
> Changelog entry:
> 2015-03-31  Renato Silva
>     * net.cc: Fix buffer size error handling in cygwin_gethostname.

Good catch.  Patch applied.


Thanks,
Corinna

-- 
Corinna Vinschen                  Please, send mails regarding Cygwin to
Cygwin Maintainer                 cygwin AT cygwin DOT com
Red Hat

[-- Attachment #2: Type: application/pgp-signature, Size: 819 bytes --]

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2015-03-31 19:20 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-03-31 18:24 Fix error mapping in gethostname Renato Silva
2015-03-31 19:20 ` Corinna Vinschen

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).