Fix bugs in msgpackstream.cpp, add PVS Studio headers and how to, add stream test to Qt project.

This commit is contained in:
Roman Isaikin
2017-07-10 16:02:38 +03:00
parent aa613f658f
commit b7284725e8
21 changed files with 112 additions and 34 deletions

8
PVS_HOWTO Normal file
View File

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

View File

@ -1,4 +1,5 @@
TEMPLATE = subdirs
SUBDIRS += \
src
src \
tests

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -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>
@ -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() {}

View File

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

View File

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

33
tests/stream/stream.pro Normal file
View File

@ -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/\\\"

View File

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

2
tests/tests.pri Normal file
View File

@ -0,0 +1,2 @@
INCLUDEPATH += ../../src
LIBS += -L"$$PWD/../bin" -lqmsgpackd

4
tests/tests.pro Normal file
View File

@ -0,0 +1,4 @@
TEMPLATE = subdirs
SUBDIRS += \
stream

View File

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