Index: azure_iot_hub/trunk/azure_iothub/umqtt/src/mqtt_codec.c
===================================================================
--- azure_iot_hub/trunk/azure_iothub/umqtt/src/mqtt_codec.c	(revision 389)
+++ azure_iot_hub/trunk/azure_iothub/umqtt/src/mqtt_codec.c	(revision 399)
@@ -38,5 +38,9 @@
 #define UNSUBSCRIBE_FIXED_HEADER_FLAG       0x2
 
-#define MAX_SEND_SIZE                       0xFFFFFF7F
+#define MAX_SEND_SIZE                       0xFFFFFF7F // 268435455
+
+// This captures the maximum packet size for 3 digits.
+// If it's above this value then we bail out of the loop
+#define MAX_3_DIGIT_PACKET_SIZE             2097152
 
 #define CODEC_STATE_VALUES      \
@@ -409,4 +413,6 @@
         size_t willTopicLen = 0;
         size_t spaceLen = 0;
+        size_t currLen = 0;
+        size_t totalLen = 0;
 
         if (mqttOptions->clientId != NULL)
@@ -436,6 +442,6 @@
         }
 
-        size_t currLen = BUFFER_length(ctrlPacket);
-        size_t totalLen = clientLen + usernameLen + passwordLen + willMessageLen + willTopicLen + spaceLen;
+        currLen = BUFFER_length(ctrlPacket);
+        totalLen = clientLen + usernameLen + passwordLen + willMessageLen + willTopicLen + spaceLen;
 
         // Validate the Username & Password
@@ -507,4 +513,5 @@
                     {
                         (void)STRING_sprintf(connect_payload_trace, " | PWD: XXXX");
+                        //(void)STRING_sprintf(connect_payload_trace, " | PWD: %s", mqttOptions->password);
                     }
                 }
@@ -554,5 +561,5 @@
                 multiplier *= NEXT_128_CHUNK;
 
-                if (multiplier > 128 * 128 * 128)
+                if (multiplier > MAX_3_DIGIT_PACKET_SIZE)
                 {
                     result = MU_FAILURE;
@@ -561,29 +568,37 @@
             } while ((encodeByte & NEXT_128_CHUNK) != 0);
 
-            codecData->codecState = CODEC_STATE_VAR_HEADER;
-
-            // Reset remainLen Index
-            codecData->remainLenIndex = 0;
-            memset(codecData->storeRemainLen, 0, 4 * sizeof(uint8_t));
-
-            if (totalLen > 0)
-            {
-                codecData->bufferOffset = 0;
-                codecData->headerData = BUFFER_new();
-                if (codecData->headerData == NULL)
-                {
-                    /* Codes_SRS_MQTT_CODEC_07_035: [ If any error is encountered then the packet state will be marked as error and mqtt_codec_bytesReceived shall return a non-zero value. ] */
-                    LogError("Failed BUFFER_new");
-                    result = MU_FAILURE;
-                }
-                else
-                {
-                    if (BUFFER_pre_build(codecData->headerData, totalLen) != 0)
+            if (result != 0 || totalLen > MAX_SEND_SIZE)
+            {
+                LogError("Receive buffer too large for MQTT packet");
+                result = MU_FAILURE;
+            }
+            else
+            {
+                codecData->codecState = CODEC_STATE_VAR_HEADER;
+
+                // Reset remainLen Index
+                codecData->remainLenIndex = 0;
+                memset(codecData->storeRemainLen, 0, 4 * sizeof(uint8_t));
+
+                if (totalLen > 0)
+                {
+                    codecData->bufferOffset = 0;
+                    codecData->headerData = BUFFER_new();
+                    if (codecData->headerData == NULL)
                     {
                         /* Codes_SRS_MQTT_CODEC_07_035: [ If any error is encountered then the packet state will be marked as error and mqtt_codec_bytesReceived shall return a non-zero value. ] */
-                        LogError("Failed BUFFER_pre_build");
+                        LogError("Failed BUFFER_new");
                         result = MU_FAILURE;
                     }
-
+                    else
+                    {
+                        if (BUFFER_pre_build(codecData->headerData, totalLen) != 0)
+                        {
+                            /* Codes_SRS_MQTT_CODEC_07_035: [ If any error is encountered then the packet state will be marked as error and mqtt_codec_bytesReceived shall return a non-zero value. ] */
+                            LogError("Failed BUFFER_pre_build");
+                            result = MU_FAILURE;
+                        }
+
+                    }
                 }
             }
@@ -611,4 +626,24 @@
 }
 
+static void clear_codec_data(MQTTCODEC_INSTANCE* codec_data)
+{
+    // Clear the code instance data
+    codec_data->currPacket = UNKNOWN_TYPE;
+    codec_data->codecState = CODEC_STATE_FIXED_HEADER;
+    codec_data->headerFlags = 0;
+    codec_data->bufferOffset = 0;
+    codec_data->headerData = NULL;
+    memset(codec_data->storeRemainLen, 0, 4 * sizeof(uint8_t));
+    codec_data->remainLenIndex = 0;
+}
+
+void mqtt_codec_reset(MQTTCODEC_HANDLE handle)
+{
+    if (handle != NULL)
+    {
+        clear_codec_data(handle);
+    }
+}
+
 MQTTCODEC_HANDLE mqtt_codec_create(ON_PACKET_COMPLETE_CALLBACK packetComplete, void* callbackCtx)
 {
@@ -619,13 +654,7 @@
     {
         /* Codes_SRS_MQTT_CODEC_07_002: [On success mqtt_codec_create shall return a MQTTCODEC_HANDLE value.] */
-        result->currPacket = UNKNOWN_TYPE;
-        result->codecState = CODEC_STATE_FIXED_HEADER;
-        result->headerFlags = 0;
-        result->bufferOffset = 0;
+        clear_codec_data(result);
         result->packetComplete = packetComplete;
         result->callContext = callbackCtx;
-        result->headerData = NULL;
-        memset(result->storeRemainLen, 0, 4 * sizeof(uint8_t));
-        result->remainLenIndex = 0;
     }
     return result;
@@ -1086,5 +1115,5 @@
                         result = MU_FAILURE;
                     }
-                    if (codec_Data->currPacket == PINGRESP_TYPE)
+                    else if (codec_Data->currPacket == PINGRESP_TYPE)
                     {
                         /* Codes_SRS_MQTT_CODEC_07_034: [Upon a constructing a complete MQTT packet mqtt_codec_bytesReceived shall call the ON_PACKET_COMPLETE_CALLBACK function.] */
