@@ -381,21 +381,23 @@ IOAddress offsetAddress(const IOAddress& addr, uint64_t offset) {
381381
382382 // If this is IPv4 addrss we utilize the conversion to uint32_t.
383383 if (addr.isV4 ()) {
384- return (IOAddress (addr.toUint32 () + offset));
384+ auto addr_uint32 = static_cast <uint64_t >(addr.toUint32 ());
385+ // If the result would exceed the maximum possible IPv4 address, let's return
386+ // the maximum IPv4 address.
387+ if (static_cast <uint64_t >(std::numeric_limits<uint32_t >::max () - addr_uint32) < offset) {
388+ return (IOAddress (std::numeric_limits<uint32_t >::max ()));
389+ }
390+ return (IOAddress (static_cast <uint32_t >(addr_uint32 + offset)));
385391 }
386392
387393 // This is IPv6 address. Let's first convert the offset value to network
388394 // byte order and store within the vector.
389395 std::vector<uint8_t > offset_bytes (8 );
390396 int offset_idx = 0 ;
391- offset_bytes[offset_idx++] = static_cast <uint8_t >((offset & 0xff00000000000000 ) >> 56 );
392- offset_bytes[offset_idx++] = static_cast <uint8_t >((offset & 0x00ff000000000000 ) >> 48 );
393- offset_bytes[offset_idx++] = static_cast <uint8_t >((offset & 0x0000ff0000000000 ) >> 40 );
394- offset_bytes[offset_idx++] = static_cast <uint8_t >((offset & 0x000000ff00000000 ) >> 32 );
395- offset_bytes[offset_idx++] = static_cast <uint8_t >((offset & 0x00000000ff000000 ) >> 24 );
396- offset_bytes[offset_idx++] = static_cast <uint8_t >((offset & 0x0000000000ff0000 ) >> 16 );
397- offset_bytes[offset_idx++] = static_cast <uint8_t >((offset & 0x000000000000ff00 ) >> 8 );
398- offset_bytes[offset_idx++] = static_cast <uint8_t >(offset & 0x00000000000000ff );
397+ for (int offset_idx = offset_bytes.size () - 1 ; offset_idx >= 0 ; --offset_idx) {
398+ offset_bytes[offset_idx] = static_cast <uint8_t >(offset & 0x00000000000000ff );
399+ offset = offset >> 8 ;
400+ }
399401
400402 // Convert the IPv6 address to vector.
401403 auto addr_bytes = addr.toBytes ();
0 commit comments