From 3a4f2d31550037c96ea0113fe294e4fdc69a1f99 Mon Sep 17 00:00:00 2001 From: liuxiaobo Date: Mon, 4 Jul 2016 17:08:46 +0800 Subject: [PATCH] unix: fix errno and retval conflict MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit We not only check the return value, but also check the errno != 0. Because in rare cases connect() will return -1 but the errno is 0 (for example, on Android 4.3, OnePlus phone A0001_12_150227) and actually the tcp three-way handshake is completed. PR-URL: https://github.com/libuv/libuv/pull/936 Reviewed-By: Ben Noordhuis Reviewed-By: Imran Iqbal Reviewed-By: Saúl Ibarra Corretgé --- src/unix/tcp.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/unix/tcp.c b/src/unix/tcp.c index 793e4c7d..46d8cd25 100644 --- a/src/unix/tcp.c +++ b/src/unix/tcp.c @@ -158,11 +158,17 @@ int uv__tcp_connect(uv_connect_t* req, handle->delayed_error = 0; - do + do { + errno = 0; r = connect(uv__stream_fd(handle), addr, addrlen); - while (r == -1 && errno == EINTR); + } while (r == -1 && errno == EINTR); - if (r == -1) { + /* We not only check the return value, but also check the errno != 0. + * Because in rare cases connect() will return -1 but the errno + * is 0 (for example, on Android 4.3, OnePlus phone A0001_12_150227) + * and actually the tcp three-way handshake is completed. + */ + if (r == -1 && errno != 0) { if (errno == EINPROGRESS) ; /* not an error */ else if (errno == ECONNREFUSED)