From 392dfe186bf6d12df5aeceeb6857b2794a720908 Mon Sep 17 00:00:00 2001 From: Andy Green Date: Thu, 12 Dec 2019 18:01:36 +0000 Subject: [PATCH] LWS_ERRNO: audit uses for case logging may destroy errno On some platforms, it's possible that logging flow may reset errno. In the case where we try to log errno on those platforms and afterwards try to query it, we will get a nasty surprise that the logged errno is destroyed by the time we come to test it. In the two cases of this in the tree at the moment, sample errno into a temp and log and test the temp. Thanks to Sakthi Ramabadran for finding this. --- lib/core-net/network.c | 6 ++++-- lib/roles/http/client/client-handshake.c | 12 +++++++----- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/lib/core-net/network.c b/lib/core-net/network.c index feab9fa91..c53d4fb08 100644 --- a/lib/core-net/network.c +++ b/lib/core-net/network.c @@ -297,12 +297,14 @@ lws_socket_bind(struct lws_vhost *vhost, lws_sockfd_type sockfd, int port, } else #endif if (n < 0) { + int _lws_errno = LWS_ERRNO; + lwsl_err("ERROR on binding fd %d to port %d (%d %d)\n", - sockfd, port, n, LWS_ERRNO); + sockfd, port, n, _lws_errno); /* if something already listening, tell caller to fail permanently */ - if (LWS_ERRNO == LWS_EADDRINUSE) + if (_lws_errno == LWS_EADDRINUSE) return LWS_ITOSA_BUSY; /* otherwise ask caller to retry later */ diff --git a/lib/roles/http/client/client-handshake.c b/lib/roles/http/client/client-handshake.c index d891c6d6f..efc3d563c 100644 --- a/lib/roles/http/client/client-handshake.c +++ b/lib/roles/http/client/client-handshake.c @@ -582,13 +582,15 @@ ads_known: m = connect(wsi->desc.sockfd, (const struct sockaddr *)psa, n); if (m == -1) { - lwsl_debug("%s: connect says errno: %d\n", __func__, LWS_ERRNO); + int errno_copy = LWS_ERRNO; - if (LWS_ERRNO != LWS_EALREADY && - LWS_ERRNO != LWS_EINPROGRESS && - LWS_ERRNO != LWS_EWOULDBLOCK + lwsl_debug("%s: connect says errno: %d\n", __func__, errno_copy); + + if (errno_copy != LWS_EALREADY && + errno_copy != LWS_EINPROGRESS && + errno_copy != LWS_EWOULDBLOCK #ifdef _WIN32 - && LWS_ERRNO != WSAEINVAL + && errno_copy != WSAEINVAL #endif ) { #if defined(_DEBUG)