Azure IoT common library

Dependents:   STM32F746_iothub_client_sample_mqtt f767zi_mqtt iothub_client_sample_amqp iothub_client_sample_http ... more

Revision:
48:81866008bba4
Parent:
37:f0dc2835e9e0
diff -r e59b74c745bc -r 81866008bba4 crt_abstractions.c
--- a/crt_abstractions.c	Thu Jul 12 18:10:56 2018 -0700
+++ b/crt_abstractions.c	Tue Sep 11 11:15:08 2018 -0700
@@ -6,6 +6,7 @@
 #include <stdlib.h>
 #include <stddef.h>
 #include <stdarg.h>
+#include <string.h>
 #include <limits.h>
 #include <float.h>
 #include <math.h>
@@ -41,7 +42,7 @@
 #define NAN        ((float)(INFINITY * 0.0F))
 #endif
 
-#ifdef _MSC_VER
+#if defined (_MSC_VER) || defined (MINGW_HAS_SECURE_API)
 #else
 
 /*Codes_SRS_CRT_ABSTRACTIONS_99_008: [strcat_s shall append the src to dst and terminates the resulting string with a null character.]*/
@@ -70,6 +71,7 @@
         else
         {
             size_t dstStrLen = 0;
+            size_t src_len = strlen(src);
 #ifdef __STDC_LIB_EXT1__
             dstStrLen = strnlen_s(dst, dstSizeInBytes);
 #else
@@ -84,12 +86,26 @@
             {
                 result = EINVAL;
             }
+            // If we are instructed to write too much data to the buffer
+            // return ERANGE
+            else if ((src_len + dstStrLen) >= dstSizeInBytes)
+            {
+                dst[0] = '\0';
+                result = ERANGE;
+            }
             else
             {
+                // memcpy should at most copy the result of strlen(src) or there may be
+                // some issues with copying unwanted memory
+                size_t bytes_to_cpy = dstSizeInBytes - dstStrLen;
+                if (bytes_to_cpy > src_len)
+                {
+                    bytes_to_cpy = src_len;
+                }
+
                 /*Codes_SRS_CRT_ABSTRACTIONS_99_009: [The initial character of src shall overwrite the terminating null character of dst.]*/
-                (void)strncpy(&dst[dstStrLen], src, dstSizeInBytes - dstStrLen);
+                if (memcpy(&dst[dstStrLen], src, bytes_to_cpy) == NULL)
                 /*Codes_SRS_CRT_ABSTRACTIONS_99_006: [If the dstSizeInBytes is 0 or smaller than the required size for dst & src, the error code returned shall be ERANGE & dst[0] set to 0.]*/
-                if (dst[dstSizeInBytes-1] != '\0')
                 {
                     dst[0] = '\0';
                     result = ERANGE;
@@ -97,12 +113,12 @@
                 else
                 {
                     /*Codes_SRS_CRT_ABSTRACTIONS_99_003: [strcat_s shall return Zero upon success.]*/
+                    dst[dstStrLen+bytes_to_cpy] = '\0';
                     result = 0;
                 }
             }
         }
     }
-
     return result;
 }
 
@@ -127,7 +143,7 @@
     {
         result = EINVAL;
     }
-    else 
+    else
     {
         size_t srcLength = strlen(src);
         if (maxCount != _TRUNCATE)
@@ -146,7 +162,7 @@
             }
             else
             {
-                (void)strncpy(dst, src, srcLength);
+                (void)memcpy(dst, src, srcLength);
                 dst[srcLength] = '\0';
                 /*Codes_SRS_CRT_ABSTRACTIONS_99_018: [strncpy_s shall return Zero upon success]*/
                 result = 0;
@@ -160,7 +176,7 @@
                 srcLength = dstSizeInBytes - 1;
                 truncationFlag = 1;
             }
-            (void)strncpy(dst, src, srcLength);
+            (void)memcpy(dst, src, srcLength);
             dst[srcLength] = '\0';
             result = 0;
         }
@@ -233,7 +249,7 @@
     {
         /*Codes_SRS_CRT_ABSTRACTIONS_99_033: [sprintf_s shall check the format string for valid formatting characters.  If the check fails, the function returns -1.]*/
 
-#if defined _MSC_VER 
+#if defined _MSC_VER
 #error crt_abstractions is not provided for Microsoft Compilers
 #else
         /*not Microsoft compiler... */
@@ -274,7 +290,7 @@
     }
     return result;
 }
-#endif /* _MSC_VER */
+#endif /* _MSC_VER || MINGW_HAS_SECURE_API */
 
 /*Codes_SRS_CRT_ABSTRACTIONS_21_006: [The strtoull_s must use the letters from a(or A) through z(or Z) to represent the numbers between 10 to 35.]*/
 /* returns the integer value that correspond to the character 'c'. If the character is invalid, it returns -1. */
@@ -293,7 +309,7 @@
     bool validStr = true;
     char* runner = (char*)nptr;
     bool isNegative = false;
-	int digitVal;
+    int digitVal;
 
     /*Codes_SRS_CRT_ABSTRACTIONS_21_005: [The strtoull_s must convert number using base 2 to 36.]*/
     /*Codes_SRS_CRT_ABSTRACTIONS_21_012: [If the subject sequence is empty or does not have the expected form, the strtoull_s must not perform any conversion; the value of nptr is stored in the object pointed to by endptr, provided that endptr is not a NULL pointer.]*/
@@ -338,7 +354,7 @@
                 runner++;
             }
         }
-        
+
         if(base == 0)
         {
             /*Codes_SRS_CRT_ABSTRACTIONS_21_007: [If the base is 0 and no special chars precedes the number, strtoull_s must convert to a decimal (base 10).]*/
@@ -465,7 +481,7 @@
     unsigned long long ullFraction = 0;
     int integerSize = 0;
     int fractionSize = 0;
-	char* startptr;
+    char* startptr;
 
     (*endptr) = (char*)nptr;
 
@@ -524,7 +540,7 @@
                 result = FST_OVERFLOW;
             }
         }
-        
+
         if (((**endptr) == 'e') || ((**endptr) == 'E'))
         {
             startptr = (*endptr) + 1;
@@ -681,7 +697,7 @@
 int mallocAndStrcpy_s(char** destination, const char* source)
 {
     int result;
-	int copied_result;
+    int copied_result;
     /*Codes_SRS_CRT_ABSTRACTIONS_99_036: [destination parameter or source parameter is NULL, the error code returned shall be EINVAL and destination shall not be modified.]*/
     if ((destination == NULL) || (source == NULL))
     {
@@ -692,7 +708,7 @@
     {
         size_t l = strlen(source);
         char* temp = (char*)malloc(l + 1);
-        
+
         /*Codes_SRS_CRT_ABSTRACTIONS_99_037: [Upon failure to allocate memory for the destination, the function will return ENOMEM.]*/
         if (temp == NULL)
         {