Charles Kerr 2021-12-05 21:12:21 -06:00 committed by GitHub
parent 1bc10e3706
commit e2be142ad6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 45 additions and 13 deletions

View File

@ -39,7 +39,13 @@ struct tr_block_info
constexpr tr_piece_index_t pieceForBlock(tr_block_index_t block) const
{
return n_blocks_in_piece ? block / n_blocks_in_piece : 0;
// if not initialized yet, don't divide by zero
if (n_blocks_in_piece == 0)
{
return 0;
}
return block / n_blocks_in_piece;
}
constexpr uint32_t pieceSize(tr_piece_index_t piece) const
@ -56,14 +62,36 @@ struct tr_block_info
constexpr tr_piece_index_t pieceOf(uint64_t offset) const
{
// if not initialized yet, don't divide by zero
if (piece_size == 0)
{
return 0;
}
// handle 0-byte files at the end of a torrent
return offset == total_size ? n_pieces - 1 : offset / piece_size;
if (offset == total_size)
{
return n_pieces - 1;
}
return offset / piece_size;
}
constexpr tr_block_index_t blockOf(uint64_t offset) const
{
// if not initialized yet, don't divide by zero
if (block_size == 0)
{
return 0;
}
// handle 0-byte files at the end of a torrent
return offset == total_size ? n_blocks - 1 : offset / block_size;
if (offset == total_size)
{
return n_blocks - 1;
}
return offset / block_size;
}
constexpr uint64_t offset(tr_piece_index_t piece, uint32_t offset, uint32_t length = 0) const

View File

@ -248,7 +248,7 @@ public:
/// WANTED
bool pieceIsWanted(tr_piece_index_t piece) const final override
bool pieceIsWanted(tr_piece_index_t piece) const final
{
return files_wanted_.pieceWanted(piece);
}

View File

@ -134,11 +134,11 @@ void tr_utpSendTo(void* closure, unsigned char const* buf, size_t buflen, struct
if (to->sa_family == AF_INET && ss->udp_socket != TR_BAD_SOCKET)
{
sendto(ss->udp_socket, reinterpret_cast<char const*>(buf), buflen, 0, to, tolen);
(void)sendto(ss->udp_socket, reinterpret_cast<char const*>(buf), buflen, 0, to, tolen);
}
else if (to->sa_family == AF_INET6 && ss->udp6_socket != TR_BAD_SOCKET)
{
sendto(ss->udp6_socket, reinterpret_cast<char const*>(buf), buflen, 0, to, tolen);
(void)sendto(ss->udp6_socket, reinterpret_cast<char const*>(buf), buflen, 0, to, tolen);
}
}

View File

@ -384,8 +384,7 @@ bool tr_saveFile(char const* filename_in, std::string_view contents, tr_error**
// follow symlinks to find the "real" file, to make sure the temporary
// we build with tr_sys_file_open_temp() is created on the right partition
char* real_filename = tr_sys_path_resolve(filename.c_str(), nullptr);
if (real_filename != nullptr)
if (char* real_filename = tr_sys_path_resolve(filename.c_str(), nullptr); real_filename != nullptr)
{
filename = real_filename;
tr_free(real_filename);

View File

@ -1207,16 +1207,18 @@ char* tr_variantToStr(tr_variant const* v, tr_variant_fmt fmt, size_t* len)
int tr_variantToFile(tr_variant const* v, tr_variant_fmt fmt, char const* filename)
{
auto error_code = int{ 0 };
auto contents_len = size_t{};
auto const* contents = tr_variantToStr(v, fmt, &contents_len);
tr_error* error = nullptr;
auto const saved = tr_saveFile(filename, { contents, contents_len }, &error);
tr_saveFile(filename, { contents, contents_len }, &error);
if (error != nullptr)
{
tr_logAddError(_("Error saving \"%s\": %s (%d)"), filename, error->message, error->code);
error_code = error->code;
tr_error_clear(&error);
}
return saved ? 0 : -1;
return error_code;
}
/***

View File

@ -195,9 +195,12 @@ TEST(Metainfo, ctorSaveContents)
auto* ctor = tr_ctorNew(nullptr);
tr_error* error = nullptr;
EXPECT_FALSE(tr_ctorSaveContents(ctor, tgt_filename.c_str(), &error));
ASSERT_NE(nullptr, error);
EXPECT_EQ(EINVAL, error->code);
tr_error_clear(&error);
EXPECT_NE(nullptr, error);
if (error != nullptr)
{
EXPECT_EQ(EINVAL, error->code);
tr_error_clear(&error);
}
// now try saving _with_ metainfo
EXPECT_EQ(0, tr_ctorSetMetainfoFromFile(ctor, src_filename.c_str()));