From 6e91136b53ff8acd6055e85bc0341e350f6b7111 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Thu, 31 Mar 2022 12:12:54 -0500 Subject: [PATCH] fix: three asan issues (#2851) * fixup! feat: add support for adding torrents by raw hash values (#2608) fix array-bounds-read issue found by asan * fixup! refactor: tr_completion (#2220) fix array-bounds-read issue in tests fixtures * fixup! refactor: tr_torrents (#2722) fix memory leak in torrent-tests --- libtransmission/magnet-metainfo.cc | 8 ++++++-- tests/libtransmission/completion-test.cc | 3 ++- tests/libtransmission/torrents-test.cc | 9 ++++++--- 3 files changed, 14 insertions(+), 6 deletions(-) diff --git a/libtransmission/magnet-metainfo.cc b/libtransmission/magnet-metainfo.cc index 4ac94607d..ad5d3ebd6 100644 --- a/libtransmission/magnet-metainfo.cc +++ b/libtransmission/magnet-metainfo.cc @@ -115,7 +115,11 @@ std::optional parseBase32Hash(std::string_view sv) return {}; } - if (!std::all_of(std::begin(sv), std::end(sv), [](unsigned char ch) { return bitzi::Base32Lookup[ch] - '0' != 0xFF; })) + if (!std::all_of( + std::begin(sv), + std::end(sv), + [](unsigned char ch) + { return '0' <= ch && ch <= '0' + std::size(bitzi::Base32Lookup) && bitzi::Base32Lookup[ch - '0'] != 0xFF; })) { return {}; } @@ -211,7 +215,7 @@ bool tr_magnet_metainfo::parseMagnet(std::string_view magnet_link, tr_error** er this->webseed_urls_.emplace_back(url_sv); } } - else if (auto constexpr ValPrefix = "urn:btih:"sv; key == "xt"sv && tr_strvStartsWith(value, ValPrefix)) + else if (static auto constexpr ValPrefix = "urn:btih:"sv; key == "xt"sv && tr_strvStartsWith(value, ValPrefix)) { if (auto const hash = parseHash(value.substr(std::size(ValPrefix))); hash) { diff --git a/tests/libtransmission/completion-test.cc b/tests/libtransmission/completion-test.cc index afca674e8..84eb19487 100644 --- a/tests/libtransmission/completion-test.cc +++ b/tests/libtransmission/completion-test.cc @@ -332,7 +332,8 @@ TEST_F(CompletionTest, createPieceBitfield) // make a completion object that has a random assortment of pieces auto completion = tr_completion(&torrent, &block_info); - auto buf = std::array{}; + auto buf = std::array{}; + ASSERT_EQ(std::size(buf), block_info.pieceCount()); EXPECT_TRUE(tr_rand_buffer(std::data(buf), std::size(buf))); for (uint64_t i = 0; i < block_info.n_pieces; ++i) { diff --git a/tests/libtransmission/torrents-test.cc b/tests/libtransmission/torrents-test.cc index e929ae6bf..979cc7ad7 100644 --- a/tests/libtransmission/torrents-test.cc +++ b/tests/libtransmission/torrents-test.cc @@ -66,15 +66,16 @@ TEST_F(TorrentsTest, rangedLoop) auto const path = tr_strvJoin(LIBTRANSMISSION_TEST_ASSETS_DIR, "/"sv, name); auto tm = tr_torrent_metainfo{}; EXPECT_TRUE(tm.parseTorrentFile(path)); - auto* const tor = new tr_torrent(std::move(tm)); + auto* const tor = new tr_torrent{ std::move(tm) }; tor->uniqueId = torrents.add(tor); EXPECT_EQ(tor, torrents.get(tor->uniqueId)); torrents_set.insert(tor); } - for (auto const* tor : torrents) + for (auto* const tor : torrents) { EXPECT_EQ(1U, torrents_set.erase(tor)); + delete tor; } EXPECT_EQ(0U, std::size(torrents_set)); EXPECT_EQ(0U, std::size(torrents_set)); @@ -96,7 +97,7 @@ TEST_F(TorrentsTest, removedSince) { auto const path = tr_strvJoin(LIBTRANSMISSION_TEST_ASSETS_DIR, "/"sv, name); auto tm = tr_torrent_metainfo{}; - auto* const tor = new tr_torrent(std::move(tm)); + auto* const tor = new tr_torrent{ std::move(tm) }; tor->uniqueId = torrents.add(tor); torrents_v.push_back(tor); } @@ -119,4 +120,6 @@ TEST_F(TorrentsTest, removedSince) EXPECT_EQ(remove, torrents.removedSince(200)); remove = { torrents_v[0]->uniqueId, torrents_v[1]->uniqueId, torrents_v[2]->uniqueId, torrents_v[3]->uniqueId }; EXPECT_EQ(remove, torrents.removedSince(50)); + + std::for_each(std::begin(torrents_v), std::end(torrents_v), [](auto* tor) { delete tor; }); }