From 5dabc6515c409b34a21e7779688a629ed14e1866 Mon Sep 17 00:00:00 2001 From: Andrew Auclair Date: Wed, 24 Apr 2024 20:44:57 -0400 Subject: [PATCH] Throw error exceptions if the recvfrom within SocketImpl::available causes an error. --- Net/src/SocketImpl.cpp | 2 ++ Net/testsuite/src/DatagramSocketTest.cpp | 28 ++++++++++++++++++++++++ Net/testsuite/src/DatagramSocketTest.h | 2 ++ 3 files changed, 32 insertions(+) diff --git a/Net/src/SocketImpl.cpp b/Net/src/SocketImpl.cpp index 34cad7d07..7c352060d 100644 --- a/Net/src/SocketImpl.cpp +++ b/Net/src/SocketImpl.cpp @@ -653,6 +653,8 @@ int SocketImpl::available() { std::vector buf(result); result = recvfrom(sockfd(), &buf[0], result, MSG_PEEK, nullptr, nullptr); + + if (result < 0) error(); } #endif return result; diff --git a/Net/testsuite/src/DatagramSocketTest.cpp b/Net/testsuite/src/DatagramSocketTest.cpp index e765de29c..124159990 100644 --- a/Net/testsuite/src/DatagramSocketTest.cpp +++ b/Net/testsuite/src/DatagramSocketTest.cpp @@ -804,6 +804,32 @@ void DatagramSocketTest::testGatherScatterVariableUNIX() } +void DatagramSocketTest::testLocalUDPConnectionResetWin() +{ +#if defined(POCO_OS_FAMILY_WINDOWS) + // create a local UDP socket and connect to another local port which doesn't have a UDP socket + DatagramSocket socket(SocketAddress("127.0.0.1", 0), true, true); + + socket.connect(SocketAddress("127.0.0.1", 2345)); + + // send some data to the socket + unsigned char values[5]{}; + socket.sendBytes(values, 5); + + try + { + // available calls recvfrom which can cause a "Connection Reset by Peer" error for UDP sockets in Windows due to ICMP packets for remote destination not existing + socket.available(); + + fail(); + } + catch (const Poco::Net::ConnectionResetException&) + { + } +#endif +} + + void DatagramSocketTest::setUp() { } @@ -832,5 +858,7 @@ CppUnit::Test* DatagramSocketTest::suite() CppUnit_addTest(pSuite, DatagramSocketTest, testGatherScatterFixed); CppUnit_addTest(pSuite, DatagramSocketTest, testGatherScatterVariable); + CppUnit_addTest(pSuite, DatagramSocketTest, testLocalUDPConnectionResetWin); + return pSuite; } diff --git a/Net/testsuite/src/DatagramSocketTest.h b/Net/testsuite/src/DatagramSocketTest.h index 10bb5e2d8..ea2c13cde 100644 --- a/Net/testsuite/src/DatagramSocketTest.h +++ b/Net/testsuite/src/DatagramSocketTest.h @@ -55,6 +55,8 @@ private: void testGatherScatterSTRFFixedUNIX(); void testGatherScatterVariableUNIX(); void testGatherScatterSTRFVariableUNIX(); + + void testLocalUDPConnectionResetWin(); };