From 8b7bee2d6f36f2faee67f4be77161ad9fd2796bc Mon Sep 17 00:00:00 2001 From: Daniel Caujolle-Bert Date: Wed, 31 Mar 2021 13:40:02 +0200 Subject: [PATCH] Fix nasty bug in UDPSocket::close(). --- NXDNGateway/UDPSocket.cpp | 8 +++++--- NXDNParrot/UDPSocket.cpp | 8 +++++--- NXDNReflector/UDPSocket.cpp | 6 ++++-- 3 files changed, 14 insertions(+), 8 deletions(-) diff --git a/NXDNGateway/UDPSocket.cpp b/NXDNGateway/UDPSocket.cpp index 510c8a2..e083477 100644 --- a/NXDNGateway/UDPSocket.cpp +++ b/NXDNGateway/UDPSocket.cpp @@ -187,6 +187,8 @@ bool CUDPSocket::open(const unsigned int index, const unsigned int af, const std return false; } + close(index); + int fd = ::socket(addr.ss_family, SOCK_DGRAM, 0); if (fd < 0) { #if defined(_WIN32) || defined(_WIN64) @@ -343,13 +345,13 @@ bool CUDPSocket::write(const unsigned char* buffer, unsigned int length, const s void CUDPSocket::close() { - for (int i = 0; i < UDP_SOCKET_MAX; i++) - close(m_fd[i]); + for (unsigned int i = 0; i < UDP_SOCKET_MAX; i++) + close(i); } void CUDPSocket::close(const unsigned int index) { - if (m_fd[index] >= 0) { + if ((index < UDP_SOCKET_MAX) && (m_fd[index] >= 0)) { #if defined(_WIN32) || defined(_WIN64) ::closesocket(m_fd[index]); #else diff --git a/NXDNParrot/UDPSocket.cpp b/NXDNParrot/UDPSocket.cpp index ad8a0eb..7b819fe 100644 --- a/NXDNParrot/UDPSocket.cpp +++ b/NXDNParrot/UDPSocket.cpp @@ -188,6 +188,8 @@ bool CUDPSocket::open(const unsigned int index, const unsigned int af, const std return false; } + close(index); + int fd = ::socket(addr.ss_family, SOCK_DGRAM, 0); if (fd < 0) { #if defined(_WIN32) || defined(_WIN64) @@ -344,13 +346,13 @@ bool CUDPSocket::write(const unsigned char* buffer, unsigned int length, const s void CUDPSocket::close() { - for (int i = 0; i < UDP_SOCKET_MAX; i++) - close(m_fd[i]); + for (unsigned int i = 0; i < UDP_SOCKET_MAX; i++) + close(i); } void CUDPSocket::close(const unsigned int index) { - if (m_fd[index] >= 0) { + if ((index < UDP_SOCKET_MAX) && (m_fd[index] >= 0)) { #if defined(_WIN32) || defined(_WIN64) ::closesocket(m_fd[index]); #else diff --git a/NXDNReflector/UDPSocket.cpp b/NXDNReflector/UDPSocket.cpp index f52249d..e192dc6 100644 --- a/NXDNReflector/UDPSocket.cpp +++ b/NXDNReflector/UDPSocket.cpp @@ -215,6 +215,8 @@ bool CUDPSocket::open(const unsigned int index, const unsigned int af, const std return false; } + close(index); + int fd = ::socket(addr.ss_family, SOCK_DGRAM, 0); if (fd < 0) { #if defined(_WIN32) || defined(_WIN64) @@ -372,12 +374,12 @@ bool CUDPSocket::write(const unsigned char* buffer, unsigned int length, const s void CUDPSocket::close() { for (int i = 0; i < UDP_SOCKET_MAX; i++) - close(m_fd[i]); + close(i); } void CUDPSocket::close(const unsigned int index) { - if (m_fd[index] >= 0) { + if ((index < UDP_SOCKET_MAX) && (m_fd[index] >= 0)) { #if defined(_WIN32) || defined(_WIN64) ::closesocket(m_fd[index]); #else