diff --git a/libtransmission/block-info.h b/libtransmission/block-info.h index a4f9ceaec..c7a6bc6d2 100644 --- a/libtransmission/block-info.h +++ b/libtransmission/block-info.h @@ -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 diff --git a/libtransmission/torrent.h b/libtransmission/torrent.h index f40a609a5..1a6fee87d 100644 --- a/libtransmission/torrent.h +++ b/libtransmission/torrent.h @@ -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); } diff --git a/libtransmission/tr-utp.cc b/libtransmission/tr-utp.cc index b518287f6..f7d0b36d1 100644 --- a/libtransmission/tr-utp.cc +++ b/libtransmission/tr-utp.cc @@ -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(buf), buflen, 0, to, tolen); + (void)sendto(ss->udp_socket, reinterpret_cast(buf), buflen, 0, to, tolen); } else if (to->sa_family == AF_INET6 && ss->udp6_socket != TR_BAD_SOCKET) { - sendto(ss->udp6_socket, reinterpret_cast(buf), buflen, 0, to, tolen); + (void)sendto(ss->udp6_socket, reinterpret_cast(buf), buflen, 0, to, tolen); } } diff --git a/libtransmission/utils.cc b/libtransmission/utils.cc index 4667fffcd..b478640b9 100644 --- a/libtransmission/utils.cc +++ b/libtransmission/utils.cc @@ -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); diff --git a/libtransmission/variant.cc b/libtransmission/variant.cc index 539234131..8b8f7b904 100644 --- a/libtransmission/variant.cc +++ b/libtransmission/variant.cc @@ -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; } /*** diff --git a/tests/libtransmission/metainfo-test.cc b/tests/libtransmission/metainfo-test.cc index e2932db39..1e11ebc2a 100644 --- a/tests/libtransmission/metainfo-test.cc +++ b/tests/libtransmission/metainfo-test.cc @@ -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()));