Azure IoT common library

Dependents:   STM32F746_iothub_client_sample_mqtt f767zi_mqtt iothub_client_sample_amqp iothub_client_sample_http ... more

Revision:
18:6d8a413a4d9a
Parent:
15:956c6d205aa7
Child:
19:2e0811512ceb
--- a/httpapi_compact.c	Sun Jan 08 11:12:54 2017 -0800
+++ b/httpapi_compact.c	Fri Jan 13 18:41:15 2017 -0800
@@ -60,8 +60,8 @@
 {
     int result;
     char* next;
-    (*dst) = strtol(src, &next, 0);
-    if ((src == next) || ((((*dst) == LONG_MAX) || ((*dst) == LONG_MIN)) && (errno != 0)))
+    long num = strtol(src, &next, 0);
+    if (src == next || num < INT_MIN || num > INT_MAX)
     {
         result = EOF;
     }
@@ -69,6 +69,9 @@
     {
         result = 1;
     }
+    if (num < INT_MIN) num = INT_MIN;
+    if (num > INT_MAX) num = INT_MAX;
+    *dst = (int)num;
     return result;
 }
 
@@ -366,24 +369,23 @@
 {
     int result;
 
-    if ((s1 == NULL) || (s2 == NULL))
-    {
-        result = -1;
-    }
+    if (s1 == NULL) result = -1;
+    else if (s2 == NULL) result = 1;
     else
     {
         result = 0;
-        while (((n--) >= 0) && ((*s1) != '\0') && ((*s2) != '\0') && (result == 0))
+
+        while(n-- && result == 0)
         {
-            /* compute the difference between the chars */
-            result = TOLOWER(*s1) - TOLOWER(*s2);
-            s1++;
-            s2++;
-        }
+            if (*s1 == 0) result = -1;
+            else if (*s2 == 0) result = 1;
+            else
+            {
 
-        if ((*s2) != '\0')
-        {
-            result = -1;
+                result = TOLOWER(*s1) - TOLOWER(*s2);
+                ++s1;
+                ++s2;
+            }
         }
     }
 
@@ -506,7 +508,7 @@
 {
     int resultLineSize;
 
-    if ((http_instance == NULL) || (buf == NULL) || (maxBufSize < 0))
+    if ((http_instance == NULL) || (buf == NULL) || (maxBufSize == 0))
     {
         LogError("%s", ((http_instance == NULL) ? "Invalid HTTP instance" : "Invalid HTTP buffer"));
         resultLineSize = -1;
@@ -932,12 +934,12 @@
     const char* substr;
     char* whereIsColon;
     int lengthInMsg;
-    const char* ContentLength = "content-length:";
-    const int ContentLengthSize = 16;
-    const char* TransferEncoding = "transfer-encoding:";
-    const int TransferEncodingSize = 19;
-    const char* Chunked = "chunked";
-    const int ChunkedSize = 8;
+    const char ContentLength[] = "content-length:";
+    const size_t ContentLengthSize = sizeof(ContentLength) - 1;
+    const char TransferEncoding[] = "transfer-encoding:";
+    const size_t TransferEncodingSize = sizeof(TransferEncoding) - 1;
+    const char Chunked[] = "chunked";
+    const size_t ChunkedSize = sizeof(Chunked) - 1;
 
     http_instance->is_io_error = 0;
 
@@ -957,7 +959,7 @@
         {
             if (InternStrnicmp(buf, ContentLength, ContentLengthSize) == 0)
             {
-                substr = buf + ContentLengthSize - 1;
+                substr = buf + ContentLengthSize;
                 if (ParseStringToDecimal(substr, &lengthInMsg) != 1)
                 {
                     /*Codes_SRS_HTTPAPI_COMPACT_21_032: [ If the HTTPAPI_ExecuteRequest cannot read the message with the request result, it shall return HTTPAPI_READ_DATA_FAILED. ]*/
@@ -970,7 +972,7 @@
             }
             else if (InternStrnicmp(buf, TransferEncoding, TransferEncodingSize) == 0)
             {
-                substr = buf + TransferEncodingSize - 1;
+                substr = buf + TransferEncodingSize;
 
                 while (isspace(*substr)) substr++;