dial up cppcheck (#663)

increase cppcheck coverage and make sure there are no warnings
This commit is contained in:
Arvid Norberg 2016-04-30 00:24:45 -04:00
parent 05626e0b0f
commit f856b6bbe3
16 changed files with 47 additions and 37 deletions

View File

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

View File

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

View File

@ -87,9 +87,10 @@ namespace libtorrent
struct socket_entry
{
socket_entry(boost::shared_ptr<udp::socket> const& s)
: socket(s), broadcast(false) {}
: socket(s), broadcast(false) { memset(buffer, 0, sizeof(buffer)); }
socket_entry(boost::shared_ptr<udp::socket> 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<udp::socket> socket;
char buffer[1500];
udp::endpoint remote;

View File

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

View File

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

View File

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

View File

@ -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

View File

@ -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<boost::uint32_t*>(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<boost::uint32_t*>(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);
}

View File

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

View File

@ -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<fingerprint>();

View File

@ -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) :

View File

@ -75,7 +75,7 @@ int receive_buffer::reserve(boost::array<boost::asio::mutable_buffer, 2>& 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)

View File

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

View File

@ -126,7 +126,7 @@ TORRENT_TEST(recv_buffer_disk_buffer)
b.cut(0, 1000); // packet size = 1000
boost::array<boost::asio::mutable_buffer, 2> 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);
}

View File

@ -47,7 +47,7 @@ void check_chain(tailqueue<test_node>& chain, char const* expected)
while (i.get())
{
TEST_EQUAL(((test_node*)i.get())->name, *expected);
TEST_EQUAL(static_cast<test_node*>(i.get())->name, *expected);
i.next();
++expected;
}
@ -56,11 +56,11 @@ void check_chain(tailqueue<test_node>& chain, char const* expected)
void free_chain(tailqueue<test_node>& q)
{
test_node* chain = (test_node*)q.get_all();
test_node* chain = static_cast<test_node*>(q.get_all());
while(chain)
{
test_node* del = (test_node*)chain;
chain = (test_node*)chain->next;
test_node* del = static_cast<test_node*>(chain);
chain = static_cast<test_node*>(chain->next);
delete del;
}
}

View File

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