diff --git a/Net/include/Poco/Net/UDPHandler.h b/Net/include/Poco/Net/UDPHandler.h index 49940db05..faf294aaa 100644 --- a/Net/include/Poco/Net/UDPHandler.h +++ b/Net/include/Poco/Net/UDPHandler.h @@ -225,13 +225,13 @@ public: AtomicCounter::ValueType setError(char* pBuf, const std::string& err) /// Sets the error into the buffer. { - std::size_t availLen = S - sizeof(MsgSizeT); - std::memset(pBuf + sizeof(MsgSizeT), 0, availLen); + std::size_t availLen = S - errorOffset(); + std::memset(pBuf + errorOffset(), 0, availLen); std::size_t msgLen = err.length(); if (msgLen) { if (msgLen >= availLen) msgLen = availLen; - std::memcpy(pBuf + sizeof(MsgSizeT), err.data(), msgLen); + std::memcpy(pBuf + errorOffset(), err.data(), msgLen); } setStatus(pBuf, BUF_STATUS_ERROR); return --_errorBacklog; @@ -251,10 +251,28 @@ public: return *reinterpret_cast(pBuf) == BUF_STATUS_ERROR; } - static Poco::UInt16 offset() + static constexpr Poco::UInt16 errorOffset() + /// Returns offset of address length. + { + return sizeof(MsgSizeT); + } + + static constexpr Poco::UInt16 addressLengthOffset() + /// Returns offset of address length. + { + return sizeof(MsgSizeT); + } + + static constexpr Poco::UInt16 addressOffset() + /// Returns offset of address. + { + return addressLengthOffset() + sizeof(poco_socklen_t); + } + + static constexpr Poco::UInt16 payloadOffset() /// Returns buffer data offset. { - return sizeof(MsgSizeT) + sizeof(poco_socklen_t) + SocketAddress::MAX_ADDRESS_LENGTH; + return addressOffset() + SocketAddress::MAX_ADDRESS_LENGTH; } static MsgSizeT payloadSize(const char* buf) @@ -264,8 +282,10 @@ public: static SocketAddress address(const char* buf) { - const auto* len = reinterpret_cast(buf + sizeof(MsgSizeT)); - const auto* pSA = reinterpret_cast(buf + sizeof(MsgSizeT) + sizeof(poco_socklen_t)); + const auto* len = reinterpret_cast(buf + addressLengthOffset()); + const auto* pSA = reinterpret_cast(buf + addressOffset()); + poco_assert(*len <= SocketAddress::MAX_ADDRESS_LENGTH); + return SocketAddress(pSA, *len); } @@ -280,7 +300,7 @@ public: /// | sizeof(MsgSizeT) bytes | sizeof(poco_socklen_t) | SocketAddress::MAX_ADDRESS_LENGTH | payload | /// +------------------------+------------------------+-----------------------------------+--------- ~ ---+ { - return buf + offset(); + return buf + payloadOffset(); } static Poco::StringTokenizer payload(const char* buf, char delimiter) diff --git a/Net/include/Poco/Net/UDPSocketReader.h b/Net/include/Poco/Net/UDPSocketReader.h index 62331ceea..b1105cfbd 100644 --- a/Net/include/Poco/Net/UDPSocketReader.h +++ b/Net/include/Poco/Net/UDPSocketReader.h @@ -112,10 +112,10 @@ public: p = handler().next(sockfd); if (p) { - Poco::UInt16 off = handler().offset(); - poco_socklen_t* pAL = reinterpret_cast(p + sizeof(RT)); + Poco::UInt16 off = handler().payloadOffset(); + auto* pAL = reinterpret_cast(p + handler().addressLengthOffset()); *pAL = SocketAddress::MAX_ADDRESS_LENGTH; - struct sockaddr* pSA = reinterpret_cast(p + sizeof(RT) + sizeof(poco_socklen_t)); + struct sockaddr* pSA = reinterpret_cast(p + handler().addressOffset()); RT ret = sock.receiveFrom(p + off, S - off - 1, &pSA, &pAL); if (ret < 0) {