fix: new warnings (#2270)
* fix: avoid potential resource leak in metainfo test Xref: https://scan5.coverity.com/reports.htm#v48014/p10174/fileInstanceId=205022335&defectInstanceId=52337396&mergedDefectId=1494638 * fix: use-init-statement warning Xref: https://sonarcloud.io/project/issues?id=transmission_transmission&issues=AX2MsJ_lWODkx6cuASq6&open=AX2MsJ_lWODkx6cuASq6 * fix: omit-redundant-override-specifier warning Xref: https://sonarcloud.io/project/issues?id=transmission_transmission&issues=AX2MsJ5GWODkx6cuASq5&open=AX2MsJ5GWODkx6cuASq5 * fix: improper-use-of-negative-value warning Xref: https://scan5.coverity.com/reports.htm#v48014/p10174/fileInstanceId=205020121&defectInstanceId=52337394&mergedDefectId=1494639 * fix: unchecked return value warning Xref: https://scan5.coverity.com/reports.htm#v48014/p10174/fileInstanceId=205020168&defectInstanceId=52304887&mergedDefectId=1491438 * fix: potential-divide-by-zero warning Xref: https://scan5.coverity.com/reports.htm#v48014/p10174/fileInstanceId=205020106&defectInstanceId=52337395&mergedDefectId=1494438
This commit is contained in:
parent
1bc10e3706
commit
e2be142ad6
|
@ -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
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
||||
/***
|
||||
|
|
|
@ -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()));
|
||||
|
|
Loading…
Reference in New Issue