@@ -225,13 +225,13 @@ class UDPHandlerImpl: public Runnable, public RefCountedObject
225225 AtomicCounter::ValueType setError (char * pBuf, const std::string& err)
226226 // / Sets the error into the buffer.
227227 {
228- std::size_t availLen = S - sizeof (MsgSizeT );
229- std::memset (pBuf + sizeof (MsgSizeT ), 0 , availLen);
228+ std::size_t availLen = S - errorOffset ( );
229+ std::memset (pBuf + errorOffset ( ), 0 , availLen);
230230 std::size_t msgLen = err.length ();
231231 if (msgLen)
232232 {
233233 if (msgLen >= availLen) msgLen = availLen;
234- std::memcpy (pBuf + sizeof (MsgSizeT ), err.data (), msgLen);
234+ std::memcpy (pBuf + errorOffset ( ), err.data (), msgLen);
235235 }
236236 setStatus (pBuf, BUF_STATUS_ERROR);
237237 return --_errorBacklog;
@@ -251,10 +251,28 @@ class UDPHandlerImpl: public Runnable, public RefCountedObject
251251 return *reinterpret_cast <const MsgSizeT*>(pBuf) == BUF_STATUS_ERROR;
252252 }
253253
254- static Poco::UInt16 offset ()
254+ static constexpr Poco::UInt16 errorOffset ()
255+ // / Returns offset of address length.
256+ {
257+ return sizeof (MsgSizeT);
258+ }
259+
260+ static constexpr Poco::UInt16 addressLengthOffset ()
261+ // / Returns offset of address length.
262+ {
263+ return sizeof (MsgSizeT);
264+ }
265+
266+ static constexpr Poco::UInt16 addressOffset ()
267+ // / Returns offset of address.
268+ {
269+ return addressLengthOffset () + sizeof (poco_socklen_t );
270+ }
271+
272+ static constexpr Poco::UInt16 payloadOffset ()
255273 // / Returns buffer data offset.
256274 {
257- return sizeof (MsgSizeT) + sizeof ( poco_socklen_t ) + SocketAddress::MAX_ADDRESS_LENGTH;
275+ return addressOffset ( ) + SocketAddress::MAX_ADDRESS_LENGTH;
258276 }
259277
260278 static MsgSizeT payloadSize (const char * buf)
@@ -264,8 +282,10 @@ class UDPHandlerImpl: public Runnable, public RefCountedObject
264282
265283 static SocketAddress address (const char * buf)
266284 {
267- const auto * len = reinterpret_cast <const poco_socklen_t *>(buf + sizeof (MsgSizeT));
268- const auto * pSA = reinterpret_cast <const struct sockaddr *>(buf + sizeof (MsgSizeT) + sizeof (poco_socklen_t ));
285+ const auto * len = reinterpret_cast <const poco_socklen_t *>(buf + addressLengthOffset ());
286+ const auto * pSA = reinterpret_cast <const struct sockaddr *>(buf + addressOffset ());
287+ poco_assert (*len <= SocketAddress::MAX_ADDRESS_LENGTH);
288+
269289 return SocketAddress (pSA, *len);
270290 }
271291
@@ -280,7 +300,7 @@ class UDPHandlerImpl: public Runnable, public RefCountedObject
280300 // / | sizeof(MsgSizeT) bytes | sizeof(poco_socklen_t) | SocketAddress::MAX_ADDRESS_LENGTH | payload |
281301 // / +------------------------+------------------------+-----------------------------------+--------- ~ ---+
282302 {
283- return buf + offset ();
303+ return buf + payloadOffset ();
284304 }
285305
286306 static Poco::StringTokenizer payload (const char * buf, char delimiter)
0 commit comments