diff --git a/src/lib/deskflow/ProtocolUtil.cpp b/src/lib/deskflow/ProtocolUtil.cpp index bab69bbbc..5fbb9c729 100644 --- a/src/lib/deskflow/ProtocolUtil.cpp +++ b/src/lib/deskflow/ProtocolUtil.cpp @@ -177,13 +177,6 @@ void ProtocolUtil::vreadf(deskflow::IStream *stream, const char *fmt, va_list ar case 'I': { void *destination = va_arg(args, void *); - UInt32 n = read4BytesInt(stream); - - if (n > PROTOCOL_MAX_LIST_LENGTH) { - LOG((CLOG_ERR "read: vector length exceeds maximum allowed size: %u", n)); - throw XBadClient("Too long message received"); - } - switch (len) { case 1: // 1 byte integer @@ -493,7 +486,7 @@ UInt32 ProtocolUtil::read4BytesInt(deskflow::IStream *stream) void ProtocolUtil::readVector1ByteInt(deskflow::IStream *stream, std::vector &destination) { - UInt32 size = read4BytesInt(stream); + UInt32 size = readVectorSize(stream); for (UInt32 i = 0; i < size; ++i) { destination.push_back(read1ByteInt(stream)); } @@ -501,7 +494,7 @@ void ProtocolUtil::readVector1ByteInt(deskflow::IStream *stream, std::vector &destination) { - UInt32 size = read4BytesInt(stream); + UInt32 size = readVectorSize(stream); for (UInt32 i = 0; i < size; ++i) { destination.push_back(read2BytesInt(stream)); } @@ -509,12 +502,24 @@ void ProtocolUtil::readVector2BytesInt(deskflow::IStream *stream, std::vector &destination) { - UInt32 size = read4BytesInt(stream); + UInt32 size = readVectorSize(stream); for (UInt32 i = 0; i < size; ++i) { destination.push_back(read4BytesInt(stream)); } } +UInt32 ProtocolUtil::readVectorSize(deskflow::IStream *stream) +{ + UInt32 size = read4BytesInt(stream); + + if (size > PROTOCOL_MAX_LIST_LENGTH) { + LOG((CLOG_ERR "readVectorSize: vector length exceeds maximum allowed size: %u", size)); + throw XBadClient("Too long message received"); + } + + return size; +} + void ProtocolUtil::readBytes(deskflow::IStream *stream, UInt32 len, String *destination) { // read the string length diff --git a/src/lib/deskflow/ProtocolUtil.h b/src/lib/deskflow/ProtocolUtil.h index 1a2ee4923..60b436d99 100644 --- a/src/lib/deskflow/ProtocolUtil.h +++ b/src/lib/deskflow/ProtocolUtil.h @@ -95,6 +95,7 @@ private: static void readVector1ByteInt(deskflow::IStream *, std::vector &); static void readVector2BytesInt(deskflow::IStream *, std::vector &); static void readVector4BytesInt(deskflow::IStream *, std::vector &); + static UInt32 readVectorSize(deskflow::IStream *stream); /** * @brief Handles an array of bytes