diff --git a/PVS_HOWTO b/PVS_HOWTO new file mode 100644 index 0000000..86907bf --- /dev/null +++ b/PVS_HOWTO @@ -0,0 +1,8 @@ +Execute following: +cd qmsgpack +mkdir build +cd build +cmake -DCMAKE_EXPORT_COMPILE_COMMANDS=On -DBUILD_TESTS=True .. +pvs-studio-analyzer analyze -o ../qmsgpack.log +plog-converter -a GA:1,2 -t tasklist -o ../qmsgpack.tasks ../qmsgpack.log +See qmsgpack.tasks then \ No newline at end of file diff --git a/qmsgpack.pro b/qmsgpack.pro index 9593051..3426635 100644 --- a/qmsgpack.pro +++ b/qmsgpack.pro @@ -1,4 +1,5 @@ TEMPLATE = subdirs SUBDIRS += \ - src + src \ + tests diff --git a/src/msgpack.cpp b/src/msgpack.cpp index 4924970..3bc8e8e 100644 --- a/src/msgpack.cpp +++ b/src/msgpack.cpp @@ -1,3 +1,5 @@ +// This is an open source non-commercial project. Dear PVS-Studio, please check it. +// PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com #include "msgpack.h" #include "private/unpack_p.h" #include "private/pack_p.h" @@ -15,15 +17,15 @@ QVariant MsgPack::unpack(const QByteArray &data) QByteArray MsgPack::pack(const QVariant &variant) { - quint8 *p = 0; QVector user_data; - quint8 *end = MsgPackPrivate::pack(variant, p, false, user_data); - quint32 size = end - p; - //qDebug() << "size probe:" << size; - + // first run, calculate size + ptrdiff_t size = MsgPackPrivate::pack(variant, nullptr, false, user_data) - + static_cast(nullptr); QByteArray arr; arr.resize(size); - end = MsgPackPrivate::pack(variant, (quint8 *)arr.data(), true, user_data); + + // second run, pack it + MsgPackPrivate::pack(variant, (quint8 *)arr.data(), true, user_data); return arr; } diff --git a/src/msgpackcommon.cpp b/src/msgpackcommon.cpp index c87d99b..36425bd 100644 --- a/src/msgpackcommon.cpp +++ b/src/msgpackcommon.cpp @@ -1,3 +1,5 @@ +// This is an open source non-commercial project. Dear PVS-Studio, please check it. +// PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com #include "msgpackcommon.h" QString MsgPack::version() diff --git a/src/msgpackstream.cpp b/src/msgpackstream.cpp index d4d28b3..05374dd 100644 --- a/src/msgpackstream.cpp +++ b/src/msgpackstream.cpp @@ -1,3 +1,5 @@ +// This is an open source non-commercial project. Dear PVS-Studio, please check it. +// PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com #include "msgpackstream.h" #include "private/pack_p.h" @@ -105,7 +107,7 @@ MsgPackStream &MsgPackStream::operator>>(bool &b) b = false; setStatus(ReadPastEnd); } else { - if (p[0] != MsgPack::FirstByte::MTRUE || + if (p[0] != MsgPack::FirstByte::MTRUE && p[0] != MsgPack::FirstByte::MFALSE) setStatus(ReadCorruptData); b = (p[0] == MsgPack::FirstByte::MTRUE); @@ -329,11 +331,11 @@ MsgPackStream &MsgPackStream::operator>>(QByteArray &array) return *this; } -bool MsgPackStream::readBytes(char *data, uint len) +bool MsgPackStream::readBytes(char *data, qint64 len) { CHECK_STREAM_PRECOND(false); - uint readed = 0; - uint thisRead = 0; + qint64 readed = 0; + qint64 thisRead = 0; while (readed < len) { thisRead = dev->read(data, (len - readed)); @@ -395,7 +397,7 @@ MsgPackStream &MsgPackStream::operator<<(bool b) CHECK_STREAM_WRITE_PRECOND(*this); quint8 m = b == true ? MsgPack::FirstByte::MTRUE : MsgPack::FirstByte::MFALSE; - if (writeBytes((char *)&m, 1) != 1) + if (!writeBytes((char *)&m, 1)) setStatus(WriteFailed); return *this; } @@ -405,7 +407,7 @@ MsgPackStream &MsgPackStream::operator<<(quint32 u32) CHECK_STREAM_WRITE_PRECOND(*this); quint8 p[5]; quint8 sz = MsgPackPrivate::pack_uint(u32, p, true) - p; - if (writeBytes((char *)p, sz) != sz) + if (!writeBytes((char *)p, sz)) setStatus(WriteFailed); return *this; } @@ -415,7 +417,7 @@ MsgPackStream &MsgPackStream::operator<<(quint64 u64) CHECK_STREAM_WRITE_PRECOND(*this); quint8 p[9]; quint8 sz = MsgPackPrivate::pack_ulonglong(u64, p, true) - p; - if (writeBytes((char *)p, sz) != sz) + if (!writeBytes((char *)p, sz)) setStatus(WriteFailed); return *this; } @@ -425,7 +427,7 @@ MsgPackStream &MsgPackStream::operator<<(qint32 i32) CHECK_STREAM_WRITE_PRECOND(*this); quint8 p[5]; quint8 sz = MsgPackPrivate::pack_int(i32, p, true) - p; - if (writeBytes((char *)p, sz) != sz) + if (!writeBytes((char *)p, sz)) setStatus(WriteFailed); return *this; } @@ -435,7 +437,7 @@ MsgPackStream &MsgPackStream::operator<<(qint64 i64) CHECK_STREAM_WRITE_PRECOND(*this); quint8 p[9]; quint8 sz = MsgPackPrivate::pack_longlong(i64, p, true) - p; - if (writeBytes((char *)p, sz) != sz) + if (!writeBytes((char *)p, sz)) setStatus(WriteFailed); return *this; } @@ -445,7 +447,7 @@ MsgPackStream &MsgPackStream::operator<<(float f) CHECK_STREAM_WRITE_PRECOND(*this); quint8 p[5]; quint8 sz = MsgPackPrivate::pack_float(f, p, true) - p; - if (writeBytes((char *)p, sz) != sz) + if (!writeBytes((char *)p, sz)) setStatus(WriteFailed); return *this; } @@ -455,7 +457,7 @@ MsgPackStream &MsgPackStream::operator<<(double d) CHECK_STREAM_WRITE_PRECOND(*this); quint8 p[9]; quint8 sz = MsgPackPrivate::pack_double(d, p, true) - p; - if (writeBytes((char *)p, sz) != sz) + if (!writeBytes((char *)p, sz)) setStatus(WriteFailed); return *this; } @@ -463,11 +465,11 @@ MsgPackStream &MsgPackStream::operator<<(double d) MsgPackStream &MsgPackStream::operator<<(QString str) { CHECK_STREAM_WRITE_PRECOND(*this); - quint8 *p = (quint8 *)0; - quint32 sz = MsgPackPrivate::pack_string(str, p, false) - p; + ptrdiff_t sz = MsgPackPrivate::pack_string(str, nullptr, false) - + static_cast(nullptr); quint8 *data = new quint8[sz]; MsgPackPrivate::pack_string(str, data, true); - if (writeBytes((char *)data, sz) != sz) + if (!writeBytes((char *)data, sz)) setStatus(WriteFailed); delete[] data; return *this; @@ -476,12 +478,12 @@ MsgPackStream &MsgPackStream::operator<<(QString str) MsgPackStream &MsgPackStream::operator<<(const char *str) { CHECK_STREAM_WRITE_PRECOND(*this); - quint8 *p = (quint8 *)0; quint32 str_len = strlen(str); - quint32 sz = MsgPackPrivate::pack_string_raw(str, str_len, p, false) - p; + ptrdiff_t sz = MsgPackPrivate::pack_string_raw(str, str_len, nullptr, false) - + static_cast(nullptr); quint8 *data = new quint8[sz]; MsgPackPrivate::pack_string_raw(str, str_len, data, true); - if (writeBytes((char *)data, sz) != sz) + if (!writeBytes((char *)data, sz)) setStatus(WriteFailed); delete[] data; return *this; @@ -493,20 +495,20 @@ MsgPackStream &MsgPackStream::operator<<(QByteArray array) quint8 p[5]; quint32 len = array.length(); quint8 header_len = MsgPackPrivate::pack_bin_header(len, p, true) - p; - if (writeBytes((char *)p, header_len) != header_len) { + if (!writeBytes((char *)p, header_len)) { setStatus(WriteFailed); return *this; } - if (writeBytes(array.data(), len) != len) + if (!writeBytes(array.data(), len)) setStatus(WriteFailed); return *this; } -bool MsgPackStream::writeBytes(const char *data, uint len) +bool MsgPackStream::writeBytes(const char *data, qint64 len) { CHECK_STREAM_WRITE_PRECOND(false); - uint written = 0; - uint thisWrite; + qint64 written = 0; + qint64 thisWrite; while (written < len) { thisWrite = dev->write(data, len - written); if (thisWrite < 0) { @@ -563,7 +565,7 @@ bool MsgPackStream::writeExtHeader(quint32 len, qint8 msgpackType) d[5] = msgpackType; sz = 6; } - if (writeBytes((const char *)d, sz) != sz) { + if (!writeBytes((const char *)d, sz)) { setStatus(WriteFailed); return false; } diff --git a/src/msgpackstream.h b/src/msgpackstream.h index 61cd440..cfe81d9 100644 --- a/src/msgpackstream.h +++ b/src/msgpackstream.h @@ -42,7 +42,7 @@ public: MsgPackStream &operator>>(double &d); MsgPackStream &operator>>(QString &str); MsgPackStream &operator>>(QByteArray &array); - bool readBytes(char *data, uint len); + bool readBytes(char *data, qint64 len); bool readExtHeader(quint32 &len); MsgPackStream &operator<<(bool b); @@ -55,7 +55,7 @@ public: MsgPackStream &operator<<(QString str); MsgPackStream &operator<<(const char *str); MsgPackStream &operator<<(QByteArray array); - bool writeBytes(const char *data, uint len); + bool writeBytes(const char *data, qint64 len); bool writeExtHeader(quint32 len, qint8 msgpackType); private: @@ -97,7 +97,7 @@ MsgPackStream& operator>>(MsgPackStream& s, QList &list) { list.clear(); quint8 p[5]; - quint32 len; + quint32 len = 0; s.readBytes((char *)p, 1); if (p[0] >= 0x90 && p[0] <= 0x9f) { len = p[0] & 0xf; diff --git a/src/private/pack_p.cpp b/src/private/pack_p.cpp index a0d4080..da890d6 100644 --- a/src/private/pack_p.cpp +++ b/src/private/pack_p.cpp @@ -1,3 +1,5 @@ +// This is an open source non-commercial project. Dear PVS-Studio, please check it. +// PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com #include "pack_p.h" #include "../endianhelper.h" diff --git a/src/private/qt_types_p.cpp b/src/private/qt_types_p.cpp index 3b70192..b5e5299 100644 --- a/src/private/qt_types_p.cpp +++ b/src/private/qt_types_p.cpp @@ -1,3 +1,5 @@ +// This is an open source non-commercial project. Dear PVS-Studio, please check it. +// PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com #include "qt_types_p.h" #include "pack_p.h" #include "unpack_p.h" diff --git a/src/private/unpack_p.cpp b/src/private/unpack_p.cpp index 45b4be6..56d8100 100644 --- a/src/private/unpack_p.cpp +++ b/src/private/unpack_p.cpp @@ -1,3 +1,5 @@ +// This is an open source non-commercial project. Dear PVS-Studio, please check it. +// PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com #include "unpack_p.h" #include "../endianhelper.h" diff --git a/src/stream/geometry.cpp b/src/stream/geometry.cpp index 2754452..beb711b 100644 --- a/src/stream/geometry.cpp +++ b/src/stream/geometry.cpp @@ -1,3 +1,5 @@ +// This is an open source non-commercial project. Dear PVS-Studio, please check it. +// PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com #include "geometry.h" #include "../msgpack.h" #include "../msgpackstream.h" diff --git a/src/stream/location.cpp b/src/stream/location.cpp index f47f25e..16ba2bf 100644 --- a/src/stream/location.cpp +++ b/src/stream/location.cpp @@ -1,3 +1,5 @@ +// This is an open source non-commercial project. Dear PVS-Studio, please check it. +// PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com #ifdef QT_LOCATION_LIB #include "location.h" diff --git a/src/stream/time.cpp b/src/stream/time.cpp index b6c04b9..cc24baa 100644 --- a/src/stream/time.cpp +++ b/src/stream/time.cpp @@ -1,3 +1,5 @@ +// This is an open source non-commercial project. Dear PVS-Studio, please check it. +// PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com #include "time.h" #include "../msgpack.h" diff --git a/tests/big/big.cpp b/tests/big/big.cpp index 1f8ddac..93507cf 100644 --- a/tests/big/big.cpp +++ b/tests/big/big.cpp @@ -1,3 +1,5 @@ +// This is an open source non-commercial project. Dear PVS-Studio, please check it. +// PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com #include #include #include diff --git a/tests/mixed/mixed_test.cpp b/tests/mixed/mixed_test.cpp index 0f17b8d..651bfd5 100644 --- a/tests/mixed/mixed_test.cpp +++ b/tests/mixed/mixed_test.cpp @@ -1,3 +1,5 @@ +// This is an open source non-commercial project. Dear PVS-Studio, please check it. +// PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com #include #include #include @@ -126,7 +128,7 @@ void MixedTest::test_map() class CustomType { public: - CustomType() {} + CustomType() : m_size(777) {} CustomType(const CustomType &other) { m_size = other.m_size; } ~CustomType() {} diff --git a/tests/pack/pack_test.cpp b/tests/pack/pack_test.cpp index 2a96287..ca0eb4e 100644 --- a/tests/pack/pack_test.cpp +++ b/tests/pack/pack_test.cpp @@ -1,3 +1,5 @@ +// This is an open source non-commercial project. Dear PVS-Studio, please check it. +// PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com #include #include #include diff --git a/tests/qttypes/qttypes_test.cpp b/tests/qttypes/qttypes_test.cpp index 015a1b0..7684e32 100644 --- a/tests/qttypes/qttypes_test.cpp +++ b/tests/qttypes/qttypes_test.cpp @@ -1,3 +1,5 @@ +// This is an open source non-commercial project. Dear PVS-Studio, please check it. +// PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com #include #include #include diff --git a/tests/stream/stream.pro b/tests/stream/stream.pro new file mode 100644 index 0000000..cdf1737 --- /dev/null +++ b/tests/stream/stream.pro @@ -0,0 +1,33 @@ +#------------------------------------------------- +# +# Project created by QtCreator 2017-07-10T14:58:18 +# +#------------------------------------------------- + +QT += testlib + +QT -= gui + +TARGET = tst_streamtest +CONFIG += console +CONFIG -= app_bundle + +TEMPLATE = app + +include(../tests.pri) + +# The following define makes your compiler emit warnings if you use +# any feature of Qt which as been marked as deprecated (the exact warnings +# depend on your compiler). Please consult the documentation of the +# deprecated API in order to know how to port your code away from it. +DEFINES += QT_DEPRECATED_WARNINGS + +# You can also make your code fail to compile if you use deprecated APIs. +# In order to do so, uncomment the following line. +# You can also select to disable deprecated APIs only up to a certain version of Qt. +#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 + + +SOURCES += \ + stream_test.cpp +DEFINES += SRCDIR=\\\"$$PWD/\\\" diff --git a/tests/stream/stream_test.cpp b/tests/stream/stream_test.cpp index cb917d9..94c44ad 100644 --- a/tests/stream/stream_test.cpp +++ b/tests/stream/stream_test.cpp @@ -1,3 +1,5 @@ +// This is an open source non-commercial project. Dear PVS-Studio, please check it. +// PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com #include #include #include diff --git a/tests/tests.pri b/tests/tests.pri new file mode 100644 index 0000000..9a83f26 --- /dev/null +++ b/tests/tests.pri @@ -0,0 +1,2 @@ +INCLUDEPATH += ../../src +LIBS += -L"$$PWD/../bin" -lqmsgpackd diff --git a/tests/tests.pro b/tests/tests.pro new file mode 100644 index 0000000..8fc7f92 --- /dev/null +++ b/tests/tests.pro @@ -0,0 +1,4 @@ +TEMPLATE = subdirs + +SUBDIRS += \ + stream diff --git a/tests/unpack/unpack_test.cpp b/tests/unpack/unpack_test.cpp index 35cd7a1..8bd79c4 100644 --- a/tests/unpack/unpack_test.cpp +++ b/tests/unpack/unpack_test.cpp @@ -1,3 +1,5 @@ +// This is an open source non-commercial project. Dear PVS-Studio, please check it. +// PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com #include #include #include