{"thread":{"id":"7432","subject":"Fix \"getaddrinfo()\" buglet","startedAt":"2007-03-27T16:50:20Z","lastAt":"2007-03-27T16:50:20Z","messageCount":1,"participants":["Linus Torvalds"],"isPatch":false,"patchVersion":null,"patchTotal":null},"messages":[{"id":"38162","messageId":"Pine.LNX.4.64.0703270938380.6730@woody.linux-foundation.org","threadId":"7432","inReplyTo":null,"subject":"Fix \"getaddrinfo()\" buglet","fromName":"Linus Torvalds","fromEmail":"torvalds@linux-foundation.org","sentAt":"2007-03-27T16:50:20Z","receivedAt":"2007-03-27T16:50:20Z","isPatch":false,"sender":{"key":"torvalds@linux-foundation.org","avatar":"https://avatars.githubusercontent.com/u/1024025?v=4"},"body":"\nAt least in Linux glibc, \"getaddrinfo()\" has a very irritating feature (or \nbug, who knows..).\n\nNamely if you pass it in an empty string for the service name, it will \nhappily and quietly consider it identical to a NULL port pointer, and \nreturn port number zero and no errors. Which obviously will not work.\n\nMaybe that's what it's really expected to do, although the man-page for \ngetaddrinfo() certainly implies that it's a bug.\n\nSo when somebody passes me a \"please pull\" request pointing to something \nlike the following\n\n\tgit://git.kernel.org:/pub/scm/linux/kernel/git/mchehab/v4l-dvb.git\n\n(note the extraneous colon at the end of the host name), git would happily \ntry to connect to port 0, which would generally just cause the remote to \nnot even answer, and the \"connect()\" will take a long time to time out.\n\nSo to work around the glibc feature/bug, just notice this empty port case \nautomatically. Also, add the port information to the error information \nwhen it fails to look up (maybe it's the host-name that fails, maybe it's \nthe port-name - we should print out both).\n\nSigned-off-by: Linus Torvalds <torvalds@linux-foundation.org>\n---\n\n connect.c |    4 +++-\n 1 files changed, 3 insertions(+), 1 deletions(-)\n\ndiff --git a/connect.c b/connect.c\nindex 5048653..da89c9c 100644\n--- a/connect.c\n+++ b/connect.c\n@@ -417,6 +417,8 @@ static int git_tcp_connect_sock(char *host)\n \tif (colon) {\n \t\t*colon = 0;\n \t\tport = colon + 1;\n+\t\tif (!*port)\n+\t\t\tport = \"<none>\";\n \t}\n \n \tmemset(&hints, 0, sizeof(hints));\n@@ -425,7 +427,7 @@ static int git_tcp_connect_sock(char *host)\n \n \tgai = getaddrinfo(host, port, &hints, &ai);\n \tif (gai)\n-\t\tdie(\"Unable to look up %s (%s)\", host, gai_strerror(gai));\n+\t\tdie(\"Unable to look up %s (port %s) (%s)\", host, port, gai_strerror(gai));\n \n \tfor (ai0 = ai; ai; ai = ai->ai_next) {\n \t\tsockfd = socket(ai->ai_family,\n"}]}