|
17 | 17 | #include "azure_c_shared_utility/singlylinkedlist.h" |
18 | 18 | #include "azure_c_shared_utility/shared_util_options.h" |
19 | 19 | #include "azure_c_shared_utility/agenttime.h" |
| 20 | +#include "azure_c_shared_utility/safe_math.h" |
20 | 21 |
|
21 | 22 | #include "iothub_client_core_ll.h" |
22 | 23 | #include "iothub_client_options.h" |
@@ -732,11 +733,14 @@ IOTHUB_CLIENT_RESULT IoTHubClientCore_LL_ParseMethodToCommand(const char* method |
732 | 733 | } |
733 | 734 | else |
734 | 735 | { |
735 | | - size_t component_name_length = componentSplit - method_name; |
736 | | - if ((*component_name = malloc(component_name_length + 1)) == NULL) |
| 736 | + size_t component_name_length = safe_subtract_size_t(componentSplit, method_name); |
| 737 | + size_t malloc_size = safe_add_size_t(component_name_length, 1); |
| 738 | + if (malloc_size == SIZE_MAX || |
| 739 | + (*component_name = malloc(malloc_size)) == NULL) |
737 | 740 | { |
738 | | - LogError("Cannot allocate component name"); |
| 741 | + LogError("Cannot allocate component name, size:%zu", malloc_size); |
739 | 742 | result = IOTHUB_CLIENT_ERROR; |
| 743 | + *component_name = NULL; |
740 | 744 | } |
741 | 745 | else |
742 | 746 | { |
@@ -975,24 +979,27 @@ static IOTHUB_CLIENT_CORE_LL_HANDLE_DATA* initialize_iothub_client(const IOTHUB_ |
975 | 979 | else |
976 | 980 | { |
977 | 981 | size_t suffix_len = strlen(whereIsDot); |
978 | | - IoTHubName = (char*)malloc(whereIsDot - hostname + 1); |
979 | | - if (IoTHubName == NULL) |
| 982 | + size_t calloc_size_hub_name = safe_add_size_t(safe_subtract_size_t(whereIsDot, hostname), 1); |
| 983 | + size_t malloc_size_hub_suffix = safe_add_size_t(suffix_len, 1); |
| 984 | + if (calloc_size_hub_name == SIZE_MAX || |
| 985 | + (IoTHubName = (char*)calloc(1, calloc_size_hub_name)) == NULL) |
980 | 986 | { |
981 | | - LogError("unable to malloc"); |
| 987 | + LogError("unable to calloc, size:%zu", calloc_size_hub_name); |
982 | 988 | IoTHubClient_Auth_Destroy(result->authorization_module); |
983 | 989 | free(result); |
984 | 990 | result = NULL; |
985 | 991 | } |
986 | | - else if ((IoTHubSuffix = (char*)malloc(suffix_len + 1)) == NULL) |
| 992 | + else if (malloc_size_hub_suffix == SIZE_MAX || |
| 993 | + (IoTHubSuffix = (char*)malloc(malloc_size_hub_suffix)) == NULL) |
987 | 994 | { |
988 | 995 | LogError("unable to malloc"); |
989 | 996 | IoTHubClient_Auth_Destroy(result->authorization_module); |
| 997 | + free(IoTHubName); |
990 | 998 | free(result); |
991 | 999 | result = NULL; |
992 | 1000 | } |
993 | 1001 | else |
994 | 1002 | { |
995 | | - memset(IoTHubName, 0, whereIsDot - hostname + 1); |
996 | 1003 | (void)memcpy(IoTHubName, hostname, whereIsDot - hostname); |
997 | 1004 | (void)strcpy(IoTHubSuffix, whereIsDot+1); |
998 | 1005 |
|
@@ -1270,34 +1277,34 @@ IOTHUB_CLIENT_CORE_LL_HANDLE IoTHubClientCore_LL_CreateFromDeviceAuth(const char |
1270 | 1277 | { |
1271 | 1278 | if (*iterator == '.') |
1272 | 1279 | { |
1273 | | - size_t length = iterator - initial; |
1274 | | - iothub_name = (char*)malloc(length + 1); |
1275 | | - if (iothub_name != NULL) |
| 1280 | + size_t length = safe_subtract_size_t(iterator, initial); |
| 1281 | + size_t calloc_size = safe_add_size_t(length, 1); |
| 1282 | + if (calloc_size != SIZE_MAX && |
| 1283 | + (iothub_name = (char*)calloc(1, calloc_size)) != NULL) |
1276 | 1284 | { |
1277 | | - memset(iothub_name, 0, length + 1); |
1278 | 1285 | memcpy(iothub_name, initial, length); |
1279 | 1286 | config->iotHubName = iothub_name; |
1280 | 1287 |
|
1281 | | - length = strlen(initial) - length - 1; |
1282 | | - iothub_suffix = (char*)malloc(length + 1); |
1283 | | - if (iothub_suffix != NULL) |
| 1288 | + length = safe_subtract_size_t(safe_subtract_size_t(strlen(initial), length), 1); |
| 1289 | + calloc_size = safe_add_size_t(length, 1); |
| 1290 | + if (calloc_size != SIZE_MAX && |
| 1291 | + (iothub_suffix = (char*)calloc(1, calloc_size)) != NULL) |
1284 | 1292 | { |
1285 | | - memset(iothub_suffix, 0, length + 1); |
1286 | 1293 | memcpy(iothub_suffix, iterator + 1, length); |
1287 | 1294 | config->iotHubSuffix = iothub_suffix; |
1288 | 1295 | break; |
1289 | 1296 | } |
1290 | 1297 | else |
1291 | 1298 | { |
1292 | | - LogError("Failed to allocate iothub suffix"); |
| 1299 | + LogError("Failed to allocate iothub suffix, size:%zu", calloc_size); |
1293 | 1300 | free(iothub_name); |
1294 | 1301 | iothub_name = NULL; |
1295 | 1302 | result = NULL; |
1296 | 1303 | } |
1297 | 1304 | } |
1298 | 1305 | else |
1299 | 1306 | { |
1300 | | - LogError("Failed to allocate iothub name"); |
| 1307 | + LogError("Failed to allocate iothub name, size:%zu", calloc_size); |
1301 | 1308 | result = NULL; |
1302 | 1309 | } |
1303 | 1310 | } |
|
0 commit comments