diff --git a/.travis.yml b/.travis.yml index 97032013f..5d9bb10b1 100644 --- a/.travis.yml +++ b/.travis.yml @@ -130,14 +130,17 @@ script: - 'if [[ "$analyze" == "1" ]]; then if [[ $TRAVIS_OS_NAME == "linux" ]]; then cppcheck --version && - find src -type f | grep ".*\.cpp$" >cppcheck.files; + find src test examples tools -type f | grep ".*\.cpp$" >cppcheck.files && + find simulation -type f -depth -2 | grep ".*\.cpp$" >>cppcheck.files && cppcheck -DTORRENT_USE_IPV6="1" -DTORRENT_USE_I2P="1" -DTORRENT_USE_OPENSSL="1" -DTORRENT_USE_INVARIANT_CHECKS="1" -DTORRENT_EXPENSIVE_INVARIANT_CHECKS="1" -DTORRENT_USE_ASSERTS="1" -UTORRENT_DISABLE_MUTABLE_TORRENTS -UTORRENT_DISABLE_LOGGING -UTORRENT_DEBUG_BUFFERS -UTORRENT_EXPORT_EXTRA --suppress=preprocessorErrorDirective + --suppress=noConstructor --suppress=syntaxError + --enable=style --template "{file}({line}): {severity} ({id}): {message}" --inline-suppr --force --std=c++11 -j2 -I include -I /usr/local/include --file-list=cppcheck.files 2> cppcheck.txt; diff --git a/examples/print.cpp b/examples/print.cpp index 4aa09e6db..6b3f09eb2 100644 --- a/examples/print.cpp +++ b/examples/print.cpp @@ -169,9 +169,6 @@ std::string const& piece_bar(libtorrent::bitfield const& p, int width) for (int k = int(piece); k < end; ++k, ++num_pieces) if (p[k]) ++num_have; int const c = int(std::ceil(num_have / float((std::max)(num_pieces, 1)) * (table_size - 1))); -#ifndef _WIN32 - char buf[40]; -#endif color[i & 1] = c; #ifndef _WIN32 @@ -184,6 +181,7 @@ std::string const& piece_bar(libtorrent::bitfield const& p, int width) { if (color[i] != last_color[i]) { + char buf[40]; snprintf(buf, sizeof(buf), "\x1b[%d;5;%dm", bg[i & 1], 232 + color[i]); last_color[i] = color[i]; bar += buf; diff --git a/include/libtorrent/broadcast_socket.hpp b/include/libtorrent/broadcast_socket.hpp index 24babb8f2..434e85124 100644 --- a/include/libtorrent/broadcast_socket.hpp +++ b/include/libtorrent/broadcast_socket.hpp @@ -87,9 +87,10 @@ namespace libtorrent struct socket_entry { socket_entry(boost::shared_ptr const& s) - : socket(s), broadcast(false) {} + : socket(s), broadcast(false) { memset(buffer, 0, sizeof(buffer)); } socket_entry(boost::shared_ptr const& s - , address_v4 const& mask): socket(s), netmask(mask), broadcast(false) {} + , address_v4 const& mask): socket(s), netmask(mask), broadcast(false) + { memset(buffer, 0, sizeof(buffer)); } boost::shared_ptr socket; char buffer[1500]; udp::endpoint remote; diff --git a/include/libtorrent/copy_ptr.hpp b/include/libtorrent/copy_ptr.hpp index 1149f4b87..084f41a64 100644 --- a/include/libtorrent/copy_ptr.hpp +++ b/include/libtorrent/copy_ptr.hpp @@ -44,6 +44,7 @@ namespace libtorrent void reset(T* t = 0) { delete m_ptr; m_ptr = t; } copy_ptr& operator=(copy_ptr const& p) { + if (m_ptr == p.m_ptr) return *this; delete m_ptr; m_ptr = p.m_ptr ? new T(*p.m_ptr) : 0; return *this; diff --git a/include/libtorrent/io_service.hpp b/include/libtorrent/io_service.hpp index 4fc3931cd..9c82dd690 100644 --- a/include/libtorrent/io_service.hpp +++ b/include/libtorrent/io_service.hpp @@ -61,9 +61,9 @@ POSSIBILITY OF SUCH DAMAGE. namespace libtorrent { #if defined TORRENT_BUILD_SIMULATOR - typedef sim::asio::io_service io_service; + using sim::asio::io_service; #else - typedef boost::asio::io_service io_service; + using boost::asio::io_service; #endif } diff --git a/include/libtorrent/piece_picker.hpp b/include/libtorrent/piece_picker.hpp index 5f726f2f4..dadbe3abf 100644 --- a/include/libtorrent/piece_picker.hpp +++ b/include/libtorrent/piece_picker.hpp @@ -112,7 +112,12 @@ namespace libtorrent struct block_info { - block_info(): peer(0), num_peers(0), state(state_none) {} + block_info(): peer(0), num_peers(0), state(state_none) + { +#if TORRENT_USE_ASSERTS + piece_index = -1; +#endif + } // the peer this block was requested or // downloaded from. torrent_peer* peer; diff --git a/include/libtorrent/session_settings.hpp b/include/libtorrent/session_settings.hpp index 85fd35b2e..b2796f1a2 100644 --- a/include/libtorrent/session_settings.hpp +++ b/include/libtorrent/session_settings.hpp @@ -48,7 +48,7 @@ namespace libtorrent #ifndef TORRENT_NO_DEPRECATE - typedef aux::proxy_settings proxy_settings; + using aux::proxy_settings; // This holds most of the session-wide settings in libtorrent. Pass this // to session::set_settings() to change the settings, initialize it from diff --git a/src/bitfield.cpp b/src/bitfield.cpp index 089491b1c..27277e4ce 100644 --- a/src/bitfield.cpp +++ b/src/bitfield.cpp @@ -128,8 +128,17 @@ namespace libtorrent if (bits == size()) return; TORRENT_ASSERT(bits >= 0); - // +1 because the first word is the size (in bits) const int b = (bits + 31) / 32; + if (bits == 0) + { + if (m_buf != NULL) + { + std::free(m_buf-1); + m_buf = NULL; + } + return; + } + if (m_buf) { boost::uint32_t* tmp = static_cast(std::realloc(m_buf-1, (b+1) * 4)); @@ -139,8 +148,9 @@ namespace libtorrent m_buf = tmp + 1; m_buf[-1] = bits; } - else if (bits > 0) + else { + // +1 because the first word is the size (in bits) boost::uint32_t* tmp = static_cast(std::malloc((b+1) * 4)); #ifndef BOOST_NO_EXCEPTIONS if (tmp == NULL) throw std::bad_alloc(); @@ -148,11 +158,6 @@ namespace libtorrent m_buf = tmp + 1; m_buf[-1] = bits; } - else if (m_buf != NULL) - { - std::free(m_buf-1); - m_buf = NULL; - } clear_trailing_bits(); TORRENT_ASSERT(size() == bits); } diff --git a/src/file.cpp b/src/file.cpp index a3bff0815..f90d26f47 100644 --- a/src/file.cpp +++ b/src/file.cpp @@ -2166,16 +2166,12 @@ typedef struct _FILE_ALLOCATED_RANGE_BUFFER { #endif // F_ALLOCSP64 -#if defined TORRENT_LINUX || TORRENT_HAS_FALLOCATE - int ret; -#endif - #if TORRENT_HAS_FALLOCATE // if fallocate failed, we have to use posix_fallocate // which can be painfully slow // if you get a compile error here, you might want to // define TORRENT_HAS_FALLOCATE to 0. - ret = posix_fallocate(native_handle(), 0, s); + int const ret = posix_fallocate(native_handle(), 0, s); // posix_allocate fails with EINVAL in case the underlying // filesystem does not support this operation if (ret != 0 && ret != EINVAL) diff --git a/src/identify_client.cpp b/src/identify_client.cpp index cc12ae8aa..9a6a92022 100644 --- a/src/identify_client.cpp +++ b/src/identify_client.cpp @@ -123,7 +123,7 @@ namespace fingerprint ret("..", 0, 0, 0, 0); ret.name[1] = 0; ret.tag_version = 0; - if (sscanf(ids, "%c%3d-%3d-%3d--", &ret.name[0], &ret.major_version, &ret.minor_version + if (sscanf(ids, "%1c%3d-%3d-%3d--", &ret.name[0], &ret.major_version, &ret.minor_version , &ret.revision_version) != 4 || !is_print(ret.name[0])) return boost::optional(); diff --git a/src/puff.cpp b/src/puff.cpp index dcaef91bb..8e875cf58 100644 --- a/src/puff.cpp +++ b/src/puff.cpp @@ -666,13 +666,13 @@ local int dynamic(struct state *s) index = 0; while (index < nlen + ndist) { int symbol; /* decoded value */ - int len; /* last length to repeat */ symbol = decode(s, &lencode); if (symbol < 16) /* length in 0..15 */ lengths[index++] = symbol; else { /* repeat instruction */ - len = 0; /* assume repeating zeros */ + int len = 0; /* last length to repeat */ + /* assume repeating zeros */ if (symbol == 16) { /* repeat last length 3..6 times */ if (index == 0) return -5; /* no last length! */ len = lengths[index - 1]; /* last length */ @@ -752,7 +752,6 @@ int puff(unsigned char *dest, /* pointer to destination pointer */ boost::uint32_t *sourcelen) /* amount of input available */ { struct state s; /* input/output state */ - int last, type; /* block information */ int err; /* return value */ /* initialize output state */ @@ -772,9 +771,10 @@ int puff(unsigned char *dest, /* pointer to destination pointer */ err = 2; /* then skip do-loop, return error */ else { /* process blocks until last block or error */ + int last = 0; do { - last = bits(&s, 1); /* one if last block */ - type = bits(&s, 2); /* block type 0..3 */ + last = bits(&s, 1); /* one if last block */ + int type = bits(&s, 2); /* block type 0..3 */ err = type == 0 ? stored(&s) : (type == 1 ? fixed(&s) : (type == 2 ? dynamic(&s) : diff --git a/src/receive_buffer.cpp b/src/receive_buffer.cpp index d163b0b48..25bbdf78b 100644 --- a/src/receive_buffer.cpp +++ b/src/receive_buffer.cpp @@ -75,7 +75,7 @@ int receive_buffer::reserve(boost::array& vec, i // we should have used m_recv_end. perhaps they always happen to be equal TORRENT_ASSERT(m_recv_pos == m_recv_end); - int num_bufs; + int num_bufs = -1; int const regular_buf_size = regular_buffer_size(); if (int(m_recv_buffer.size()) < regular_buf_size) diff --git a/test/bittorrent_peer.cpp b/test/bittorrent_peer.cpp index 2bdb4abb4..29516c5f5 100644 --- a/test/bittorrent_peer.cpp +++ b/test/bittorrent_peer.cpp @@ -325,7 +325,7 @@ void peer_conn::on_msg_length(error_code const& ec, size_t bytes_transferred) unsigned int length = read_uint32(ptr); if (length > sizeof(buffer)) { - fprintf(stderr, "len: %d\n", length); + fprintf(stderr, "len: %u\n", length); close("ERROR RECEIVE MESSAGE PREFIX: packet too big", error_code()); return; } diff --git a/test/test_receive_buffer.cpp b/test/test_receive_buffer.cpp index 7059864a0..20c114b6a 100644 --- a/test/test_receive_buffer.cpp +++ b/test/test_receive_buffer.cpp @@ -126,7 +126,7 @@ TORRENT_TEST(recv_buffer_disk_buffer) b.cut(0, 1000); // packet size = 1000 boost::array vec; b.assign_disk_buffer(&disk_buffer, 137); - int num_bufs = b.reserve(vec, 1000); + int const num_bufs = b.reserve(vec, 1000); TEST_EQUAL(num_bufs, 2); // regular buffer disk buffer @@ -137,6 +137,7 @@ TORRENT_TEST(recv_buffer_disk_buffer) // |----------------| 863 TEST_EQUAL(boost::asio::buffer_size(vec[0]), 863); + // cppcheck-suppress arrayIndexOutOfBounds TEST_EQUAL(boost::asio::buffer_size(vec[1]), 137); } diff --git a/test/test_tailqueue.cpp b/test/test_tailqueue.cpp index fe6d7143e..b838a1943 100644 --- a/test/test_tailqueue.cpp +++ b/test/test_tailqueue.cpp @@ -47,7 +47,7 @@ void check_chain(tailqueue& chain, char const* expected) while (i.get()) { - TEST_EQUAL(((test_node*)i.get())->name, *expected); + TEST_EQUAL(static_cast(i.get())->name, *expected); i.next(); ++expected; } @@ -56,11 +56,11 @@ void check_chain(tailqueue& chain, char const* expected) void free_chain(tailqueue& q) { - test_node* chain = (test_node*)q.get_all(); + test_node* chain = static_cast(q.get_all()); while(chain) { - test_node* del = (test_node*)chain; - chain = (test_node*)chain->next; + test_node* del = static_cast(chain); + chain = static_cast(chain->next); delete del; } } diff --git a/tools/parse_access_log.cpp b/tools/parse_access_log.cpp index 02045cdf9..a75eda335 100644 --- a/tools/parse_access_log.cpp +++ b/tools/parse_access_log.cpp @@ -98,9 +98,9 @@ int main(int argc, char* argv[]) bool write = (op.event & 1) != 0; bool complete = (op.event & 2) != 0; - FILE* out_file = 0; if (complete) { + FILE* out_file = 0; op_map::iterator i = outstanding_ops.find(event_id); if (i != outstanding_ops.end()) { @@ -111,7 +111,7 @@ int main(int argc, char* argv[]) , op.offset, double(i->second.timestamp) / 1000000.f); i->second.timestamp = op.timestamp; } - + out_file = write ? writes_file : reads_file; double start_time = double(i->second.timestamp - first_timestamp) / 1000000.0; double end_time = double(op.timestamp - first_timestamp) / 1000000.0;