diff --git a/other/bootstrap_daemon/src/config.c b/other/bootstrap_daemon/src/config.c index cfa96e55a5..891912dce1 100644 --- a/other/bootstrap_daemon/src/config.c +++ b/other/bootstrap_daemon/src/config.c @@ -58,6 +58,10 @@ static void parse_tcp_relay_ports_config(config_t *cfg, uint16_t **tcp_relay_por // Similar procedure to the one of reading config file below *tcp_relay_ports = (uint16_t *)malloc(default_ports_count * sizeof(uint16_t)); + if (*tcp_relay_ports == nullptr) { + log_write(LOG_LEVEL_ERROR, "Allocation failure.\n"); + return; + } for (size_t i = 0; i < default_ports_count; ++i) { @@ -73,10 +77,8 @@ static void parse_tcp_relay_ports_config(config_t *cfg, uint16_t **tcp_relay_por ++*tcp_relay_port_count; } - // The loop above skips invalid ports, so we adjust the allocated memory size - if ((*tcp_relay_port_count) > 0) { - *tcp_relay_ports = (uint16_t *)realloc(*tcp_relay_ports, (*tcp_relay_port_count) * sizeof(uint16_t)); - } else { + // No ports, so we free the array. + if (*tcp_relay_port_count == 0) { free(*tcp_relay_ports); *tcp_relay_ports = nullptr; } @@ -98,6 +100,10 @@ static void parse_tcp_relay_ports_config(config_t *cfg, uint16_t **tcp_relay_por } *tcp_relay_ports = (uint16_t *)malloc(config_port_count * sizeof(uint16_t)); + if (*tcp_relay_ports == nullptr) { + log_write(LOG_LEVEL_ERROR, "Allocation failure.\n"); + return; + } for (int i = 0; i < config_port_count; ++i) { config_setting_t *elem = config_setting_get_elem(ports_array, i); @@ -125,10 +131,8 @@ static void parse_tcp_relay_ports_config(config_t *cfg, uint16_t **tcp_relay_por ++*tcp_relay_port_count; } - // The loop above skips invalid ports, so we adjust the allocated memory size - if ((*tcp_relay_port_count) > 0) { - *tcp_relay_ports = (uint16_t *)realloc(*tcp_relay_ports, (*tcp_relay_port_count) * sizeof(uint16_t)); - } else { + // No ports, so we free the array. + if (*tcp_relay_port_count == 0) { free(*tcp_relay_ports); *tcp_relay_ports = nullptr; } @@ -177,6 +181,10 @@ bool get_general_config(const char *cfg_file_path, char **pid_file_path, char ** const size_t pid_file_path_len = strlen(tmp_pid_file) + 1; *pid_file_path = (char *)malloc(pid_file_path_len); + if (*pid_file_path == nullptr) { + log_write(LOG_LEVEL_ERROR, "Allocation failure.\n"); + return false; + } memcpy(*pid_file_path, tmp_pid_file, pid_file_path_len); // Get keys file location @@ -190,6 +198,10 @@ bool get_general_config(const char *cfg_file_path, char **pid_file_path, char ** const size_t keys_file_path_len = strlen(tmp_keys_file) + 1; *keys_file_path = (char *)malloc(keys_file_path_len); + if (*keys_file_path == nullptr) { + log_write(LOG_LEVEL_ERROR, "Allocation failure.\n"); + return false; + } memcpy(*keys_file_path, tmp_keys_file, keys_file_path_len); // Get IPv6 option @@ -304,6 +316,10 @@ static uint8_t *bootstrap_hex_string_to_bin(const char *hex_string) const size_t len = strlen(hex_string) / 2; uint8_t *ret = (uint8_t *)malloc(len); + if (ret == nullptr) { + log_write(LOG_LEVEL_ERROR, "Allocation failure.\n"); + return nullptr; + } const char *pos = hex_string; diff --git a/other/bootstrap_daemon/src/log_backend_syslog.c b/other/bootstrap_daemon/src/log_backend_syslog.c index 7afb938551..c5fccefd56 100644 --- a/other/bootstrap_daemon/src/log_backend_syslog.c +++ b/other/bootstrap_daemon/src/log_backend_syslog.c @@ -61,6 +61,9 @@ void log_backend_syslog_write(LOG_LEVEL level, const char *format, va_list args) } char *buf = (char *)malloc(size + 1); + if (buf == nullptr) { + return; + } vsnprintf(buf, size + 1, format, args); syslog(log_backend_syslog_level(level), "%s", buf);