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
This commit is contained in:
Charles Kerr 2022-03-31 12:12:54 -05:00 committed by GitHub
parent d943f069f5
commit 6e91136b53
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 14 additions and 6 deletions

View File

@ -115,7 +115,11 @@ std::optional<tr_sha1_digest_t> 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)
{

View File

@ -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<char, 64>{};
auto buf = std::array<char, 65>{};
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)
{

View File

@ -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; });
}