1919#include "azure_c_shared_utility/singlylinkedlist.h"
2020#include "azure_c_shared_utility/shared_util_options.h"
2121#include "azure_c_shared_utility/optimize_size.h"
22+ #include "azure_c_shared_utility/safe_math.h"
2223
2324#define MAX_HOSTNAME 64
2425#define TIME_MAX_BUFFER 16
@@ -202,9 +203,11 @@ static int process_header_line(const unsigned char* buffer, size_t len, size_t*
202203 free (headerKey );
203204 headerKey = NULL ;
204205 }
205- headerKey = (char * )malloc (keyLen + 1 );
206- if (headerKey == NULL )
206+ size_t malloc_size = safe_add_size_t (keyLen , 1 );
207+ if (malloc_size == SIZE_MAX ||
208+ (headerKey = (char * )malloc (malloc_size )) == NULL )
207209 {
210+ LogError ("Cannot malloc headerKey, size:%zu" , malloc_size );
208211 result = MU_FAILURE ;
209212 continueProcessing = false;
210213 }
@@ -231,12 +234,17 @@ static int process_header_line(const unsigned char* buffer, size_t len, size_t*
231234 // Remove leading spaces
232235 while (* targetPos == 32 ) { targetPos ++ ; }
233236
234- size_t valueLen = (& buffer [index ])- targetPos ;
235- char * headerValue = (char * )malloc (valueLen + 1 );
236- if (headerValue == NULL )
237+ size_t valueLen = safe_subtract_size_t ((& buffer [index ]), targetPos );
238+ size_t malloc_size = safe_add_size_t (valueLen , 1 );
239+
240+ char * headerValue ;
241+ if (malloc_size == SIZE_MAX ||
242+ (headerValue = (char * )malloc (malloc_size )) == NULL )
237243 {
244+ LogError ("Cannot malloc headerValue, size:%zu" , malloc_size );
238245 result = MU_FAILURE ;
239246 continueProcessing = false;
247+ headerValue = NULL ;
240248 }
241249 else
242250 {
@@ -870,12 +878,15 @@ static int construct_http_headers(HTTP_HEADERS_HANDLE http_header, size_t conten
870878 }
871879 else
872880 {
873- size_t dataLen = strlen (header )+ 2 ;
874- char * sendData = malloc (dataLen + 1 );
875- if (sendData == NULL )
881+ size_t dataLen = safe_add_size_t (strlen (header ), 2 );
882+ size_t malloc_size = safe_add_size_t (dataLen , 1 );
883+
884+ char * sendData ;
885+ if (malloc_size == SIZE_MAX ||
886+ (sendData = malloc (malloc_size )) == NULL )
876887 {
877888 result = MU_FAILURE ;
878- LogError ("Failed in allocating header data" );
889+ LogError ("Failed in allocating header data, size:%zu" , malloc_size );
879890 }
880891 else
881892 {
@@ -884,7 +895,7 @@ static int construct_http_headers(HTTP_HEADERS_HANDLE http_header, size_t conten
884895 hostname_found = true;
885896 }
886897
887- if (snprintf (sendData , dataLen + 1 , "%s\r\n" , header ) <= 0 )
898+ if (snprintf (sendData , malloc_size , "%s\r\n" , header ) <= 0 )
888899 {
889900 result = MU_FAILURE ;
890901 LogError ("Failed in constructing header data" );
@@ -905,16 +916,21 @@ static int construct_http_headers(HTTP_HEADERS_HANDLE http_header, size_t conten
905916 if (!hostname_found )
906917 {
907918 // calculate the size of the host header
908- size_t host_len = strlen (HTTP_HOST ) + strlen (hostname ) + MAX_CONTENT_LENGTH + 2 ;
909- char * host_header = malloc (host_len + 1 );
910- if (host_header == NULL )
919+ size_t host_len = safe_add_size_t (strlen (HTTP_HOST ), strlen (hostname ));
920+ host_len = safe_add_size_t (host_len , MAX_CONTENT_LENGTH );
921+ host_len = safe_add_size_t (host_len , 2 );
922+
923+ size_t malloc_size = safe_add_size_t (host_len , 1 );
924+ char * host_header ;
925+ if (malloc_size == SIZE_MAX ||
926+ (host_header = malloc (malloc_size )) == NULL )
911927 {
912- LogError ("Failed allocating host header" );
928+ LogError ("Failed allocating host header, size:%zu" , malloc_size );
913929 result = MU_FAILURE ;
914930 }
915931 else
916932 {
917- if (snprintf (host_header , host_len + 1 , "%s: %s:%d\r\n" , HTTP_HOST , hostname , port_num ) <= 0 )
933+ if (snprintf (host_header , malloc_size , "%s: %s:%d\r\n" , HTTP_HOST , hostname , port_num ) <= 0 )
918934 {
919935 LogError ("Failed constructing host header" );
920936 result = MU_FAILURE ;
@@ -931,11 +947,14 @@ static int construct_http_headers(HTTP_HEADERS_HANDLE http_header, size_t conten
931947 if (result == 0 )
932948 {
933949 /* Codes_SRS_UHTTP_07_015: [uhttp_client_execute_request shall add the Content-Length to the request if the contentLength is > 0] */
934- size_t fmtLen = strlen (HTTP_CONTENT_LEN ) + HTTP_CRLF_LEN + 8 ;
935- char * content = malloc (fmtLen + 1 );
936- if (content == NULL )
950+ size_t fmtLen = safe_add_size_t (strlen (HTTP_CONTENT_LEN ), HTTP_CRLF_LEN );
951+ fmtLen = safe_add_size_t (fmtLen , 25 );
952+
953+ size_t malloc_size = safe_add_size_t (fmtLen , 1 );
954+ char * content ;
955+ if ((content = malloc (malloc_size )) == NULL )
937956 {
938- LogError ("Failed allocating chunk header" );
957+ LogError ("Failed allocating chunk header, size:%zu" , malloc_size );
939958 result = MU_FAILURE ;
940959 }
941960 else
@@ -986,16 +1005,20 @@ static STRING_HANDLE construct_http_data(HTTP_CLIENT_REQUEST_TYPE request_type,
9861005 }
9871006 else
9881007 {
989- size_t buffLen = strlen (HTTP_REQUEST_LINE_FMT ) + strlen (method ) + strlen (relative_path );
990- char * request = malloc (buffLen + 1 );
991- if (request == NULL )
1008+ size_t buffLen = safe_add_size_t (strlen (HTTP_REQUEST_LINE_FMT ), strlen (method ));
1009+ buffLen = safe_add_size_t (buffLen , strlen (relative_path ));
1010+
1011+ size_t malloc_size = safe_add_size_t (buffLen , 1 );
1012+ char * request ;
1013+ if (malloc_size == SIZE_MAX ||
1014+ (request = malloc (malloc_size )) == NULL )
9921015 {
9931016 result = NULL ;
994- LogError ("Failure allocating Request data" );
1017+ LogError ("Failure allocating Request data, size:%zu" , malloc_size );
9951018 }
9961019 else
9971020 {
998- if (snprintf (request , buffLen + 1 , HTTP_REQUEST_LINE_FMT , method , relative_path ) <= 0 )
1021+ if (snprintf (request , malloc_size , HTTP_REQUEST_LINE_FMT , method , relative_path ) <= 0 )
9991022 {
10001023 result = NULL ;
10011024 LogError ("Failure writing request buffer" );
0 commit comments