Skip to content

Commit

Permalink
Fix bugs in msgpackstream.cpp, add PVS Studio headers and how to, add…
Browse files Browse the repository at this point in the history
… stream test to Qt project.
  • Loading branch information
romixlab committed Jul 10, 2017
1 parent aa613f6 commit b728472
Show file tree
Hide file tree
Showing 21 changed files with 112 additions and 34 deletions.
8 changes: 8 additions & 0 deletions PVS_HOWTO
Original file line number Diff line number Diff line change
@@ -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
3 changes: 2 additions & 1 deletion qmsgpack.pro
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
TEMPLATE = subdirs

SUBDIRS += \
src
src \
tests
14 changes: 8 additions & 6 deletions src/msgpack.cpp
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -15,15 +17,15 @@ QVariant MsgPack::unpack(const QByteArray &data)

QByteArray MsgPack::pack(const QVariant &variant)
{
quint8 *p = 0;
QVector<QByteArray> 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<quint8 *>(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;
}
Expand Down
2 changes: 2 additions & 0 deletions src/msgpackcommon.cpp
Original file line number Diff line number Diff line change
@@ -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()
Expand Down
48 changes: 25 additions & 23 deletions src/msgpackstream.cpp
Original file line number Diff line number Diff line change
@@ -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"

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand All @@ -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;
}
Expand All @@ -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;
}
Expand All @@ -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;
}
Expand All @@ -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;
}
Expand All @@ -455,19 +457,19 @@ 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;
}

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<quint8 *>(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;
Expand All @@ -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<quint8 *>(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;
Expand All @@ -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) {
Expand Down Expand Up @@ -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;
}
Expand Down
6 changes: 3 additions & 3 deletions src/msgpackstream.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class MSGPACK_EXPORT MsgPackStream
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);
Expand All @@ -55,7 +55,7 @@ class MSGPACK_EXPORT MsgPackStream
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:
Expand Down Expand Up @@ -97,7 +97,7 @@ MsgPackStream& operator>>(MsgPackStream& s, QList<T> &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;
Expand Down
2 changes: 2 additions & 0 deletions src/private/pack_p.cpp
Original file line number Diff line number Diff line change
@@ -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"

Expand Down
2 changes: 2 additions & 0 deletions src/private/qt_types_p.cpp
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
2 changes: 2 additions & 0 deletions src/private/unpack_p.cpp
Original file line number Diff line number Diff line change
@@ -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"

Expand Down
2 changes: 2 additions & 0 deletions src/stream/geometry.cpp
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
2 changes: 2 additions & 0 deletions src/stream/location.cpp
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
2 changes: 2 additions & 0 deletions src/stream/time.cpp
Original file line number Diff line number Diff line change
@@ -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"

Expand Down
2 changes: 2 additions & 0 deletions tests/big/big.cpp
Original file line number Diff line number Diff line change
@@ -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 <QString>
#include <QtTest>
#include <QDebug>
Expand Down
4 changes: 3 additions & 1 deletion tests/mixed/mixed_test.cpp
Original file line number Diff line number Diff line change
@@ -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 <QString>
#include <QtTest>
#include <msgpack.h>
Expand Down Expand Up @@ -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() {}

Expand Down
2 changes: 2 additions & 0 deletions tests/pack/pack_test.cpp
Original file line number Diff line number Diff line change
@@ -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 <QString>
#include <QByteArray>
#include <QtTest>
Expand Down
2 changes: 2 additions & 0 deletions tests/qttypes/qttypes_test.cpp
Original file line number Diff line number Diff line change
@@ -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 <QString>
#include <QtTest>
#include <QDebug>
Expand Down
33 changes: 33 additions & 0 deletions tests/stream/stream.pro
Original file line number Diff line number Diff line change
@@ -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/\\\"
2 changes: 2 additions & 0 deletions tests/stream/stream_test.cpp
Original file line number Diff line number Diff line change
@@ -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 <QString>
#include <QtTest>
#include <QDebug>
Expand Down
2 changes: 2 additions & 0 deletions tests/tests.pri
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
INCLUDEPATH += ../../src
LIBS += -L"$$PWD/../bin" -lqmsgpackd
4 changes: 4 additions & 0 deletions tests/tests.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
TEMPLATE = subdirs

SUBDIRS += \
stream
2 changes: 2 additions & 0 deletions tests/unpack/unpack_test.cpp
Original file line number Diff line number Diff line change
@@ -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 <QString>
#include <QtTest>
#include <QDebug>
Expand Down

0 comments on commit b728472

Please sign in to comment.