{"thread":{"id":"4697","subject":"connect.c and errno","startedAt":"2006-06-28T16:56:12Z","lastAt":"2006-07-02T00:09:48Z","messageCount":3,"participants":["Morten Welinder","Petr Baudis","Junio C Hamano"],"isPatch":false,"patchVersion":null,"patchTotal":null},"messages":[{"id":"22746","messageId":"118833cc0606280956s4081029ci5b3cd1fdf4b10c97@mail.gmail.com","threadId":"4697","inReplyTo":null,"subject":"connect.c and errno","fromName":"Morten Welinder","fromEmail":"mwelinder@gmail.com","sentAt":"2006-06-28T16:56:12Z","receivedAt":"2006-06-28T16:56:12Z","isPatch":false,"sender":{"key":"mwelinder@gmail.com","avatar":null},"body":"It looks like connect.c waits too long before it uses errno in both copies\nof git_tcp_connect_sock.  Both close and freeaddrinfo can poke any\nnon-zero value in there.\n\nM.\n"},{"id":"22994","messageId":"20060701215626.GB29115@pasky.or.cz","threadId":"4697","inReplyTo":"118833cc0606280956s4081029ci5b3cd1fdf4b10c97@mail.gmail.com","subject":"[PATCH] Fix errno usage in connect.c","fromName":"Petr Baudis","fromEmail":"pasky@suse.cz","sentAt":"2006-07-01T21:56:26Z","receivedAt":"2006-07-01T21:56:26Z","isPatch":true,"sender":{"key":"pasky@ucw.cz","avatar":"https://avatars.githubusercontent.com/u/18439?v=4"},"body":"Dear diary, on Wed, Jun 28, 2006 at 06:56:12PM CEST, I got a letter\nwhere Morten Welinder <mwelinder@gmail.com> said that...\n> It looks like connect.c waits too long before it uses errno in both copies\n> of git_tcp_connect_sock.  Both close and freeaddrinfo can poke any\n> non-zero value in there.\n\nNice catch.\n\n->8-\n\nerrno was used after it could've been modified by a subsequent library call.\nSpotted by Morten Welinder.\n\nSigned-off-by: Petr Baudis <pasky@suse.cz>\n---\n\n connect.c |   18 ++++++++++++------\n 1 files changed, 12 insertions(+), 6 deletions(-)\n\ndiff --git a/connect.c b/connect.c\nindex cb4656d..9a87bd9 100644\n--- a/connect.c\n+++ b/connect.c\n@@ -328,7 +328,7 @@ #ifndef NO_IPV6\n  */\n static int git_tcp_connect_sock(char *host)\n {\n-\tint sockfd = -1;\n+\tint sockfd = -1, saved_errno = 0;\n \tchar *colon, *end;\n \tconst char *port = STR(DEFAULT_GIT_PORT);\n \tstruct addrinfo hints, *ai0, *ai;\n@@ -362,9 +362,12 @@ static int git_tcp_connect_sock(char *ho\n \tfor (ai0 = ai; ai; ai = ai->ai_next) {\n \t\tsockfd = socket(ai->ai_family,\n \t\t\t\tai->ai_socktype, ai->ai_protocol);\n-\t\tif (sockfd < 0)\n+\t\tif (sockfd < 0) {\n+\t\t\tsaved_errno = errno;\n \t\t\tcontinue;\n+\t\t}\n \t\tif (connect(sockfd, ai->ai_addr, ai->ai_addrlen) < 0) {\n+\t\t\tsaved_errno = errno;\n \t\t\tclose(sockfd);\n \t\t\tsockfd = -1;\n \t\t\tcontinue;\n@@ -375,7 +378,7 @@ static int git_tcp_connect_sock(char *ho\n \tfreeaddrinfo(ai0);\n \n \tif (sockfd < 0)\n-\t\tdie(\"unable to connect a socket (%s)\", strerror(errno));\n+\t\tdie(\"unable to connect a socket (%s)\", strerror(saved_errno));\n \n \treturn sockfd;\n }\n@@ -387,7 +390,7 @@ #else /* NO_IPV6 */\n  */\n static int git_tcp_connect_sock(char *host)\n {\n-\tint sockfd = -1;\n+\tint sockfd = -1, saved_errno = 0;\n \tchar *colon, *end;\n \tchar *port = STR(DEFAULT_GIT_PORT), *ep;\n \tstruct hostent *he;\n@@ -426,8 +429,10 @@ static int git_tcp_connect_sock(char *ho\n \n \tfor (ap = he->h_addr_list; *ap; ap++) {\n \t\tsockfd = socket(he->h_addrtype, SOCK_STREAM, 0);\n-\t\tif (sockfd < 0)\n+\t\tif (sockfd < 0) {\n+\t\t\tsaved_errno = errno;\n \t\t\tcontinue;\n+\t\t}\n \n \t\tmemset(&sa, 0, sizeof sa);\n \t\tsa.sin_family = he->h_addrtype;\n@@ -435,6 +440,7 @@ static int git_tcp_connect_sock(char *ho\n \t\tmemcpy(&sa.sin_addr, *ap, he->h_length);\n \n \t\tif (connect(sockfd, (struct sockaddr *)&sa, sizeof sa) < 0) {\n+\t\t\tsaved_errno = errno;\n \t\t\tclose(sockfd);\n \t\t\tsockfd = -1;\n \t\t\tcontinue;\n@@ -443,7 +449,7 @@ static int git_tcp_connect_sock(char *ho\n \t}\n \n \tif (sockfd < 0)\n-\t\tdie(\"unable to connect a socket (%s)\", strerror(errno));\n+\t\tdie(\"unable to connect a socket (%s)\", strerror(saved_errno));\n \n \treturn sockfd;\n }\n\n-- \n\t\t\t\tPetr \"Pasky\" Baudis\nStuff: http://pasky.or.cz/\nSnow falling on Perl. White noise covering line noise.\nHides all the bugs too. -- J. Putnam\n"},{"id":"23019","messageId":"7vd5codfzn.fsf@assigned-by-dhcp.cox.net","threadId":"4697","inReplyTo":"20060701215626.GB29115@pasky.or.cz","subject":"Re: [PATCH] Fix errno usage in connect.c","fromName":"Junio C Hamano","fromEmail":"junkio@cox.net","sentAt":"2006-07-02T00:09:48Z","receivedAt":"2006-07-02T00:09:48Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Petr Baudis <pasky@suse.cz> writes:\n\n> Dear diary, on Wed, Jun 28, 2006 at 06:56:12PM CEST, I got a letter\n> where Morten Welinder <mwelinder@gmail.com> said that...\n>> It looks like connect.c waits too long before it uses errno in both copies\n>> of git_tcp_connect_sock.  Both close and freeaddrinfo can poke any\n>> non-zero value in there.\n>\n> Nice catch.\n>\n> ->8-\n>\n> errno was used after it could've been modified by a subsequent library call.\n> Spotted by Morten Welinder.\n>\n> Signed-off-by: Petr Baudis <pasky@suse.cz>\n\nThanks.\n"}]}