From 94eeae203bdd80d579cc46117a939b21cec25544 Mon Sep 17 00:00:00 2001 From: SweetPPro Date: Thu, 3 Nov 2022 01:41:05 +1100 Subject: [PATCH] fix: 4.0.0-beta.1 crash when updating blocklist (#4011) --- libtransmission/blocklist.cc | 40 ++++++++++++++++++++++++------------ 1 file changed, 27 insertions(+), 13 deletions(-) diff --git a/libtransmission/blocklist.cc b/libtransmission/blocklist.cc index 62c43ddb9..a7d74a94f 100644 --- a/libtransmission/blocklist.cc +++ b/libtransmission/blocklist.cc @@ -445,9 +445,32 @@ size_t BlocklistFile::setContent(char const* filename) return {}; } - size_t keep = 0; // index in ranges + //separate before sorting + auto ipv4_ranges = std::vector{}; + auto ipv6_ranges = std::vector{}; - std::sort(std::begin(ranges), std::end(ranges), BlocklistFile::compareAddressRangesByFirstAddress); + for (auto const& range : ranges) + { + if (range.begin_ == 0 && range.end_ == 0) + { + // IPv6 + ipv6_ranges.emplace_back(range); + } + else + { + ipv4_ranges.emplace_back(range); + } + } + + std::sort(std::begin(ipv4_ranges), std::end(ipv4_ranges), BlocklistFile::compareAddressRangesByFirstAddress); + std::sort(std::begin(ipv6_ranges), std::end(ipv6_ranges), BlocklistFile::compareAddressRangesByFirstAddress); + + // combine sorted + ranges.clear(); + ranges.insert(ranges.end(), ipv4_ranges.begin(), ipv4_ranges.end()); + ranges.insert(ranges.end(), ipv6_ranges.begin(), ipv6_ranges.end()); + + size_t keep = 0; // index in ranges // merge for (auto const& range : ranges) @@ -574,16 +597,7 @@ void BlocklistFile::assertValidRules(std::vector const& ranges) } } - for (size_t i = 1; i < std::size(ranges_ipv4); ++i) - { - TR_ASSERT(ranges_ipv4[i - 1].end_ < ranges_ipv4[i].begin_); - } - - for (size_t i = 1; i < std::size(ranges_ipv6); ++i) - { - auto const& last_end_address = ranges_ipv6[i - 1].end6_; - auto const& start_address = ranges_ipv6[i].begin6_; - TR_ASSERT(memcmp(&last_end_address, &start_address, sizeof(start_address)) > 0); - } + TR_ASSERT(is_sorted(std::begin(ranges_ipv4), std::end(ranges_ipv4), BlocklistFile::compareAddressRangesByFirstAddress)); + TR_ASSERT(is_sorted(std::begin(ranges_ipv6), std::end(ranges_ipv6), BlocklistFile::compareAddressRangesByFirstAddress)); } #endif