From 192a7bec9660663733bf9fcb9038b385fb1187df Mon Sep 17 00:00:00 2001 From: Pierre Ossman Date: Sun, 20 Nov 2022 13:25:23 +0100 Subject: [PATCH] Avoid redefining errno This can have unexpected consequences as some code may rely on it being a simple variable. Instead to what we do in Socket, which is to define a unique name for getting socket error numbers. --- common/rdr/FdInStream.cxx | 12 ++++++------ common/rdr/FdOutStream.cxx | 12 ++++++------ 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/common/rdr/FdInStream.cxx b/common/rdr/FdInStream.cxx index 13d3a5a8..4ddd1221 100644 --- a/common/rdr/FdInStream.cxx +++ b/common/rdr/FdInStream.cxx @@ -26,14 +26,14 @@ #include #ifdef _WIN32 #include +#define errorNumber WSAGetLastError() #define close closesocket -#undef errno -#define errno WSAGetLastError() #include #else #include #include #include +#define errorNumber errno #endif /* Old systems have select() in sys/time.h */ @@ -89,20 +89,20 @@ size_t FdInStream::readFd(void* buf, size_t len) FD_ZERO(&fds); FD_SET(fd, &fds); n = select(fd+1, &fds, 0, 0, &tv); - } while (n < 0 && errno == EINTR); + } while (n < 0 && errorNumber == EINTR); if (n < 0) - throw SystemException("select",errno); + throw SystemException("select", errorNumber); if (n == 0) return 0; do { n = ::recv(fd, (char*)buf, len, 0); - } while (n < 0 && errno == EINTR); + } while (n < 0 && errorNumber == EINTR); if (n < 0) - throw SystemException("read",errno); + throw SystemException("read", errorNumber); if (n == 0) throw EndOfStream(); diff --git a/common/rdr/FdOutStream.cxx b/common/rdr/FdOutStream.cxx index f89fd345..e630a4d7 100644 --- a/common/rdr/FdOutStream.cxx +++ b/common/rdr/FdOutStream.cxx @@ -27,8 +27,7 @@ #include #ifdef _WIN32 #include -#undef errno -#define errno WSAGetLastError() +#define errorNumber WSAGetLastError() #include #else #include @@ -37,6 +36,7 @@ #include #include #include +#define errorNumber errno #endif /* Old systems have select() in sys/time.h */ @@ -109,10 +109,10 @@ size_t FdOutStream::writeFd(const void* data, size_t length) FD_ZERO(&fds); FD_SET(fd, &fds); n = select(fd+1, 0, &fds, 0, &tv); - } while (n < 0 && errno == EINTR); + } while (n < 0 && errorNumber == EINTR); if (n < 0) - throw SystemException("select", errno); + throw SystemException("select", errorNumber); if (n == 0) return 0; @@ -126,10 +126,10 @@ size_t FdOutStream::writeFd(const void* data, size_t length) #else n = ::send(fd, (const char*)data, length, MSG_DONTWAIT); #endif - } while (n < 0 && (errno == EINTR)); + } while (n < 0 && (errorNumber == EINTR)); if (n < 0) - throw SystemException("write", errno); + throw SystemException("write", errorNumber); gettimeofday(&lastWrite, NULL); -- 2.39.5