From c14a692493e75b77111215e08b93b3f3e1ca2cb1 Mon Sep 17 00:00:00 2001
From: Marko Budiselic <mbudiselicbuda@gmail.com>
Date: Mon, 5 Sep 2016 15:54:56 +0100
Subject: [PATCH] init struct size problem fix

---
 .../communication/bolt/v1/packing/codes.hpp   |  6 ++++++
 src/communication/bolt/v1/states/init.cpp     | 20 +++++++++----------
 2 files changed, 15 insertions(+), 11 deletions(-)

diff --git a/include/communication/bolt/v1/packing/codes.hpp b/include/communication/bolt/v1/packing/codes.hpp
index 274cbb757..a8c6e7c3e 100644
--- a/include/communication/bolt/v1/packing/codes.hpp
+++ b/include/communication/bolt/v1/packing/codes.hpp
@@ -13,6 +13,7 @@ enum Code : uint8_t
     TinyString   = 0x80,
     TinyList     = 0x90,
     TinyMap      = 0xA0,
+
     TinyStruct   = 0xB0,
 
     Null         = 0xC0,
@@ -53,6 +54,11 @@ enum Code : uint8_t
     EndOfStream  = 0xDF,
 };
 
+enum Rule : uint8_t
+{
+    MaxInitStructSize = 0x02
+};
+
 }
 
 }
diff --git a/src/communication/bolt/v1/states/init.cpp b/src/communication/bolt/v1/states/init.cpp
index d792014c3..c0f2de255 100644
--- a/src/communication/bolt/v1/states/init.cpp
+++ b/src/communication/bolt/v1/states/init.cpp
@@ -1,7 +1,7 @@
 #include "communication/bolt/v1/states/init.hpp"
 
-#include "communication/bolt/v1/session.hpp"
 #include "communication/bolt/v1/messaging/codes.hpp"
+#include "communication/bolt/v1/session.hpp"
 
 #include "utils/likely.hpp"
 
@@ -10,24 +10,23 @@ namespace bolt
 
 Init::Init() : MessageParser<Init>(logging::log->logger("Init")) {}
 
-State* Init::parse(Session& session, Message& message)
+State *Init::parse(Session &session, Message &message)
 {
     auto struct_type = session.decoder.read_byte();
 
-    if(UNLIKELY(struct_type != 0xB2))
-    {
+    if (UNLIKELY(struct_type & 0x0F <= pack::Rule::MaxInitStructSize)) {
         logger.debug("{}", struct_type);
 
-        logger.debug("Expected struct marker 0xB2 instead of 0x{:02X}",
-                     (unsigned)struct_type);
+        logger.debug(
+            "Expected struct marker of max size 0x{:02} instead of 0x{:02X}",
+            pack::Rule::MaxInitStructSize, pack::Code(unsigned) struct_type);
 
         return nullptr;
     }
 
     auto message_type = session.decoder.read_byte();
 
-    if(UNLIKELY(message_type != MessageCode::Init))
-    {
+    if (UNLIKELY(message_type != MessageCode::Init)) {
         logger.debug("Expected Init (0x01) instead of (0x{:02X})",
                      (unsigned)message_type);
 
@@ -36,12 +35,12 @@ State* Init::parse(Session& session, Message& message)
 
     message.client_name = session.decoder.read_string();
 
-    // TODO read authentication tokens
+    // TODO read authentication tokens if B2
 
     return this;
 }
 
-State* Init::execute(Session& session, Message& message)
+State *Init::execute(Session &session, Message &message)
 {
     logger.debug("Client connected '{}'", message.client_name);
 
@@ -51,5 +50,4 @@ State* Init::execute(Session& session, Message& message)
 
     return session.bolt.states.executor.get();
 }
-
 }