diff --git a/src/enum_net.cpp b/src/enum_net.cpp index bab1b1154..e27d7b735 100644 --- a/src/enum_net.cpp +++ b/src/enum_net.cpp @@ -37,6 +37,7 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/enum_net.hpp" #include "libtorrent/broadcast_socket.hpp" #include "libtorrent/error_code.hpp" +#include "libtorrent/assert.hpp" #if BOOST_VERSION < 103500 #include #else @@ -444,9 +445,10 @@ namespace libtorrent return ret; } ifconf ifc; - char buf[1024]; + // make sure the buffer is aligned to hold ifreq structs + ifreq buf[30]; ifc.ifc_len = sizeof(buf); - ifc.ifc_buf = buf; + ifc.ifc_buf = (char*)buf; if (ioctl(s, SIOCGIFCONF, &ifc) < 0) { ec = error_code(errno, asio::error::system_category); @@ -457,7 +459,7 @@ namespace libtorrent char *ifr = (char*)ifc.ifc_req; int remaining = ifc.ifc_len; - while (remaining) + while (remaining > sizeof(ifreq)) { ifreq const& item = *reinterpret_cast(ifr); @@ -508,7 +510,9 @@ namespace libtorrent ret.push_back(iface); } -#if defined TORRENT_BSD +#ifdef _SIZEOF_ADDR_IFREQ + int current_size = _SIZEOF_ADDR_IFREQ(item); +#elif defined TORRENT_BSD int current_size = item.ifr_addr.sa_len + IFNAMSIZ; #else int current_size = sizeof(ifreq); @@ -516,6 +520,7 @@ namespace libtorrent ifr += current_size; remaining -= current_size; } + TORRENT_ASSERT(remaining == 0); close(s); #elif TORRENT_USE_GETADAPTERSADDRESSES