A small footprint MQTT library

Dependents:   STM32F746_iothub_client_sample_mqtt FXOS8700CQ_To_Azure_IoT f767zi_mqtt FXOS8700CQ_To_Azure_IoT ... more

Revision:
22:786a74c2d89b
Parent:
20:a8254fa69b6c
Child:
24:75f5b91d1c6e
--- a/mqtt_codec.c	Fri Nov 03 13:19:28 2017 -0700
+++ b/mqtt_codec.c	Fri Nov 17 13:58:21 2017 -0800
@@ -560,17 +560,33 @@
                 }
             } while ((encodeByte & NEXT_128_CHUNK) != 0);
 
-            if (totalLen > 0)
-            {
-                codecData->headerData = BUFFER_new();
-                (void)BUFFER_pre_build(codecData->headerData, totalLen);
-                codecData->bufferOffset = 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 = __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 = __FAILURE__;
+                    }
+
+                }
+            }
         }
     }
     return result;