SYNERGY-323 "No configuration available" error. Sonar issues fix.

This commit is contained in:
Serhii Hadzhilov 2020-10-13 22:38:50 +03:00
parent 75efede1e3
commit 6c37cfad3b
3 changed files with 123 additions and 136 deletions

View file

@ -16,6 +16,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#include <array>
#include "synergy/ProtocolUtil.h" #include "synergy/ProtocolUtil.h"
#include "io/IStream.h" #include "io/IStream.h"
#include "base/Log.h" #include "base/Log.h"
@ -60,7 +61,7 @@ ProtocolUtil::readf(synergy::IStream* stream, const char* fmt, ...)
catch (XIO&) { catch (XIO&) {
result = false; result = false;
} }
catch (std::bad_alloc & exception) { catch (std::bad_alloc&) {
result = false; result = false;
} }
va_end(args); va_end(args);
@ -113,14 +114,52 @@ ProtocolUtil::vreadf(synergy::IStream* stream, const char* fmt, va_list args)
switch (*fmt) { switch (*fmt) {
case 'i': { case 'i': {
void* destination = va_arg(args, void*); void* destination = va_arg(args, void*);
readInt(stream, len, destination); switch (len) {
case 1:
// 1 byte integer
*static_cast<UInt8*>(destination) = read1ByteInt(stream);
break;
case 2:
// 2 byte integer
*static_cast<UInt16*>(destination) = read2BytesInt(stream);
break;
case 4:
// 4 byte integer
*static_cast<UInt32*>(destination) = read4BytesInt(stream);
break;
default:
//the length is wrong
LOG((CLOG_ERR "read: length to be read is wrong: '%d' should be 1,2, or 4", len));
assert(false); //assert for debugging
break;
}
break; break;
} }
case 'I': { case 'I': {
void* destination = va_arg(args, void*); void* destination = va_arg(args, void*);
readVectorInt(stream, len, destination); switch (len) {
case 1:
// 1 byte integer
readVector1ByteInt(stream, *static_cast<std::vector<UInt8>*>(destination));
break;
case 2:
// 2 byte integer
readVector2BytesInt(stream, *static_cast<std::vector<UInt16>*>(destination));
break;
case 4:
// 4 byte integer
readVector4BytesInt(stream, *static_cast<std::vector<UInt32>*>(destination));
break;
default:
//the length is wrong
LOG((CLOG_ERR "read: length to be read is wrong: '%d' should be 1,2, or 4", len));
assert(false); //assert for debugging
break;
}
break; break;
} }
case 's': { case 's': {
String* destination = va_arg(args, String*); String* destination = va_arg(args, String*);
readBytes(stream, len, destination); readBytes(stream, len, destination);
@ -416,115 +455,67 @@ ProtocolUtil::read(synergy::IStream* stream, void* vbuffer, UInt32 count)
} }
} }
void ProtocolUtil::readInt(synergy::IStream * stream, UInt32 len, void* destination) { UInt8 ProtocolUtil::read1ByteInt(synergy::IStream * stream)
// check for valid length {
if (len == 4 || len == 2 || len == 1) { const UInt32 BufferSize = 1;
std::array<UInt8, 1> buffer = {};
read(stream, buffer.data(), BufferSize);
static const int buffer_size = 4; UInt8 Result = buffer[0];
// read the data LOG((CLOG_DEBUG2 "readf: read 1 byte integer: %d (0x%x)", Result, Result));
UInt8 buffer[buffer_size];
//Read the buffer till the len or buffers_size, which ever is smaller
read(stream, buffer, len > buffer_size ? buffer_size : len);
switch (len) { return Result;
case 1: }
// 1 byte integer
*static_cast<UInt8*>(destination) = buffer[0];
LOG((CLOG_DEBUG2 "readf: read %d byte integer: %d (0x%x)",
len,
*static_cast<UInt8*>(destination),
*static_cast<UInt8*>(destination)));
break;
case 2: UInt16 ProtocolUtil::read2BytesInt(synergy::IStream * stream)
// 2 byte integer {
*static_cast<UInt16*>(destination) = const UInt32 BufferSize = 2;
static_cast<UInt16>( std::array<UInt8, BufferSize> buffer = {};
(static_cast<UInt16>(buffer[0]) << 8) | read(stream, buffer.data(), BufferSize);
static_cast<UInt16>(buffer[1]));
LOG((CLOG_DEBUG2 "readf: read %d byte integer: %d (0x%x)",
len,
*static_cast<UInt16*>(destination),
*static_cast<UInt16*>(destination)));
break;
case 4: UInt16 Result = (static_cast<UInt16>(buffer[0]) << 8) | static_cast<UInt16>(buffer[1]);
// 4 byte integer LOG((CLOG_DEBUG2 "readf: read 2 byte integer: %d (0x%x)", Result, Result));
*static_cast<UInt32*>(destination) =
(static_cast<UInt32>(buffer[0]) << 24) | return Result;
(static_cast<UInt32>(buffer[1]) << 16) | }
(static_cast<UInt32>(buffer[2]) << 8) |
static_cast<UInt32>(buffer[3]); UInt32 ProtocolUtil::read4BytesInt(synergy::IStream * stream)
LOG((CLOG_DEBUG2 "readf: read %d byte integer: %d (0x%x)", {
len, const int BufferSize = 4;
*static_cast<UInt32*>(destination), std::array<UInt8, BufferSize> buffer = {};
*static_cast<UInt32*>(destination))); read(stream, buffer.data(), BufferSize);
break;
} UInt32 Result = (static_cast<UInt32>(buffer[0]) << 24) |
} (static_cast<UInt32>(buffer[1]) << 16) |
else { (static_cast<UInt32>(buffer[2]) << 8) |
//the length is wrong (static_cast<UInt32>(buffer[3]));
LOG((CLOG_ERR "read: length to be read is wrong: '%d' should be 1,2, or 4", len));
assert(false); //assert for debugging LOG((CLOG_DEBUG2 "readf: read 4 byte integer: %d (0x%x)", Result, Result));
return Result;
}
void ProtocolUtil::readVector1ByteInt(synergy::IStream* stream, std::vector<UInt8>& destination)
{
UInt32 size = read4BytesInt(stream);
for (UInt32 i = 0; i < size; ++i) {
destination.push_back(read1ByteInt(stream));
} }
} }
void ProtocolUtil::readVectorInt(synergy::IStream * stream, UInt32 len, void* destination) { void ProtocolUtil::readVector2BytesInt(synergy::IStream* stream, std::vector<UInt16>& destination)
// check for valid length {
assert(len == 1 || len == 2 || len == 4); UInt32 size = read4BytesInt(stream);
for (UInt32 i = 0; i < size; ++i) {
destination.push_back(read2BytesInt(stream));
}
}
// read the vector length void ProtocolUtil::readVector4BytesInt(synergy::IStream* stream, std::vector<UInt32>& destination)
UInt8 buffer[4]; {
read(stream, buffer, 4); UInt32 size = read4BytesInt(stream);
UInt32 n = (static_cast<UInt32>(buffer[0]) << 24) | for (UInt32 i = 0; i < size; ++i) {
(static_cast<UInt32>(buffer[1]) << 16) | destination.push_back(read4BytesInt(stream));
(static_cast<UInt32>(buffer[2]) << 8) |
static_cast<UInt32>(buffer[3]);
// convert it
switch (len) {
case 1:
// 1 byte integer
for (UInt32 i = 0; i < n; ++i) {
read(stream, buffer, 1);
static_cast<std::vector<UInt8>*>(destination)->push_back(buffer[0]);
LOG((CLOG_DEBUG2 "readf: read %d byte integer[%d]: %d (0x%x)",
len, i,
static_cast<std::vector<UInt8>*>(destination)->back(),
static_cast<std::vector<UInt8>*>(destination)->back()));
}
break;
case 2:
// 2 byte integer
for (UInt32 i = 0; i < n; ++i) {
read(stream, buffer, 2);
static_cast<std::vector<UInt16>*>(destination)->push_back(
static_cast<UInt16>(
(static_cast<UInt16>(buffer[0]) << 8) |
static_cast<UInt16>(buffer[1])));
LOG((CLOG_DEBUG2 "readf: read %d byte integer[%d]: %d (0x%x)",
len, i,
static_cast<std::vector<UInt16>*>(destination)->back(),
static_cast<std::vector<UInt16>*>(destination)->back()));
}
break;
case 4:
// 4 byte integer
for (UInt32 i = 0; i < n; ++i) {
read(stream, buffer, 4);
static_cast<std::vector<UInt32>*>(destination)->push_back(
(static_cast<UInt32>(buffer[0]) << 24) |
(static_cast<UInt32>(buffer[1]) << 16) |
(static_cast<UInt32>(buffer[2]) << 8) |
static_cast<UInt32>(buffer[3]));
LOG((CLOG_DEBUG2 "readf: read %d byte integer[%d]: %d (0x%x)",
len, i,
static_cast<std::vector<UInt32>*>(destination)->back(),
static_cast<std::vector<UInt32>*>(destination)->back()));
}
break;
} }
} }
@ -533,12 +524,7 @@ void ProtocolUtil::readBytes(synergy::IStream * stream, UInt32 len, String* dest
// read the string length // read the string length
UInt8 buffer[128]; UInt8 buffer[128];
read(stream, buffer, 4); len = read4BytesInt(stream);
len = (static_cast<UInt32>(buffer[0]) << 24) |
(static_cast<UInt32>(buffer[1]) << 16) |
(static_cast<UInt32>(buffer[2]) << 8) |
static_cast<UInt32>(buffer[3]);
// use a fixed size buffer if its big enough // use a fixed size buffer if its big enough
const bool useFixed = (len <= sizeof(buffer)); const bool useFixed = (len <= sizeof(buffer));

View file

@ -86,12 +86,16 @@ private:
/** /**
* @brief Handles 1,2, or 4 byte Integers * @brief Handles 1,2, or 4 byte Integers
*/ */
static void readInt(synergy::IStream*, UInt32, void*); static UInt8 read1ByteInt(synergy::IStream * stream);
static UInt16 read2BytesInt(synergy::IStream * stream);
static UInt32 read4BytesInt(synergy::IStream * stream);
/** /**
* @brief Handles a Vector of integers * @brief Handles a Vector of integers
*/ */
static void readVectorInt(synergy::IStream*, UInt32, void*); static void readVector1ByteInt(synergy::IStream*, std::vector<UInt8>&);
static void readVector2BytesInt(synergy::IStream*, std::vector<UInt16>&);
static void readVector4BytesInt(synergy::IStream*, std::vector<UInt32>&);
/** /**
* @brief Handles an array of bytes * @brief Handles an array of bytes

View file

@ -124,15 +124,12 @@ TEST_F(ProtocolUtilTests, readf_string)
class ReadfIntTestFixture : public ::testing::TestWithParam< std::tuple<const char*, int> > class ReadfIntTestFixture : public ::testing::TestWithParam< std::tuple<const char*, int> >
{ {
private: public:
MockStream stream;
UInt8 StreamData1Byte = 10; UInt8 StreamData1Byte = 10;
std::array<UInt8, 2> StreamData2Bytes = {0, 10}; std::array<UInt8, 2> StreamData2Bytes = {0, 10};
std::array<UInt8, 4> StreamData4Bytes = {0, 0, 0, 10}; std::array<UInt8, 4> StreamData4Bytes = {0, 0, 0, 10};
public:
MockStream stream;
UInt8* getStreamData(int size) UInt8* getStreamData(int size)
{ {
UInt8* StreamData = nullptr; UInt8* StreamData = nullptr;
@ -240,8 +237,8 @@ class ReadfIntAndStringTest : public ReadfIntTestFixture
{ {
public: public:
UInt8 ActualInt8 = 0; UInt8 ActualInt8 = 0;
UInt8 ActualInt16 = 0; UInt16 ActualInt16 = 0;
UInt8 ActualInt32 = 32; UInt32 ActualInt32 = 32;
std::string ActualString; std::string ActualString;
}; };
@ -307,20 +304,20 @@ TEST_F(ProtocolUtilTests, readf_string_and_int4bytes)
const UInt8 ExpectedInt = 10; const UInt8 ExpectedInt = 10;
std::array<UInt8, 4> StreamIntData = {0,0,0,ExpectedInt}; std::array<UInt8, 4> StreamIntData = {0,0,0,ExpectedInt};
const std::string ExpectedString(32768, 'x'); const std::string ExpectedStr(32768, 'x');
std::array<UInt8, 4> StringSize = {0,0,128,0}; std::array<UInt8, 4> Size = {0,0,128,0};
EXPECT_CALL(stream, read(_, _)) EXPECT_CALL(stream, read(_, _))
.WillOnce( .WillOnce(
DoAll( DoAll(
SetValueToVoidPointerArg0(StringSize.data(), StringSize.size()), SetValueToVoidPointerArg0(Size.data(), Size.size()),
Return(StringSize.size()) Return(Size.size())
) )
) )
.WillOnce( .WillOnce(
DoAll( DoAll(
SetValueToVoidPointerArg0(ExpectedString.c_str(), ExpectedString.length()), SetValueToVoidPointerArg0(ExpectedStr.c_str(), ExpectedStr.length()),
Return(ExpectedString.length()) Return(ExpectedStr.length())
) )
) )
.WillOnce( .WillOnce(
@ -331,31 +328,31 @@ TEST_F(ProtocolUtilTests, readf_string_and_int4bytes)
); );
EXPECT_TRUE(ProtocolUtil::readf(&stream, "%s%4i", &ActualString, &ActualInt32)); EXPECT_TRUE(ProtocolUtil::readf(&stream, "%s%4i", &ActualString, &ActualInt32));
EXPECT_EQ(ExpectedString, ActualString); EXPECT_EQ(ExpectedStr, ActualString);
EXPECT_EQ(ExpectedInt, ActualInt32); EXPECT_EQ(ExpectedInt, ActualInt32);
} }
TEST_F(ProtocolUtilTests, readf_string_and_vector_int4bytes) TEST_F(ProtocolUtilTests, readf_string_and_vector_int4bytes)
{ {
std::vector<UInt32> Actual4Bytes = {}; std::vector<UInt32> Actual = {};
const std::vector<UInt32> Expected4Bytes = {10,10}; const std::vector<UInt32> Expected4Bytes = {10,10};
std::array<UInt8, 4> StreamVectorSize = {0,0,0,2}; std::array<UInt8, 4> StreamVectorSize = {0,0,0,2};
std::array<UInt8, 4> StreamData4Bytes = {0, 0, 0, 10}; std::array<UInt8, 4> StreamData4Bytes = {0, 0, 0, 10};
const std::string ExpectedString(32768, 'x'); const std::string ExpString(32768, 'x');
std::array<UInt8, 4> StringSize = {0,0,128,0}; std::array<UInt8, 4> SizeString = {0,0,128,0};
EXPECT_CALL(stream, read(_, _)) EXPECT_CALL(stream, read(_, _))
.WillOnce( .WillOnce(
DoAll( DoAll(
SetValueToVoidPointerArg0(StringSize.data(), StringSize.size()), SetValueToVoidPointerArg0(SizeString.data(), SizeString.size()),
Return(StringSize.size()) Return(SizeString.size())
) )
) )
.WillOnce( .WillOnce(
DoAll( DoAll(
SetValueToVoidPointerArg0(ExpectedString.c_str(), ExpectedString.length()), SetValueToVoidPointerArg0(ExpString.c_str(), ExpString.length()),
Return(ExpectedString.length()) Return(ExpString.length())
) )
) )
.WillOnce( .WillOnce(
@ -371,9 +368,9 @@ TEST_F(ProtocolUtilTests, readf_string_and_vector_int4bytes)
) )
); );
EXPECT_TRUE(ProtocolUtil::readf(&stream, "%s%4I", &ActualString, &Actual4Bytes)); EXPECT_TRUE(ProtocolUtil::readf(&stream, "%s%4I", &ActualString, &Actual));
EXPECT_EQ(ExpectedString, ActualString); EXPECT_EQ(ExpString, ActualString);
EXPECT_EQ(Expected4Bytes, Actual4Bytes); EXPECT_EQ(Expected4Bytes, Actual);
} }
TEST_F(ProtocolUtilTests, readf_vector_int4bytes_and_string) TEST_F(ProtocolUtilTests, readf_vector_int4bytes_and_string)