@@ -58,6 +58,10 @@ static void parse_tcp_relay_ports_config(config_t *cfg, uint16_t **tcp_relay_por
5858
5959 // Similar procedure to the one of reading config file below
6060 * tcp_relay_ports = (uint16_t * )malloc (default_ports_count * sizeof (uint16_t ));
61+ if (* tcp_relay_ports == nullptr ) {
62+ log_write (LOG_LEVEL_ERROR , "Allocation failure.\n" );
63+ return ;
64+ }
6165
6266 for (size_t i = 0 ; i < default_ports_count ; ++ i ) {
6367
@@ -73,10 +77,8 @@ static void parse_tcp_relay_ports_config(config_t *cfg, uint16_t **tcp_relay_por
7377 ++ * tcp_relay_port_count ;
7478 }
7579
76- // The loop above skips invalid ports, so we adjust the allocated memory size
77- if ((* tcp_relay_port_count ) > 0 ) {
78- * tcp_relay_ports = (uint16_t * )realloc (* tcp_relay_ports , (* tcp_relay_port_count ) * sizeof (uint16_t ));
79- } else {
80+ // No ports, so we free the array.
81+ if (* tcp_relay_port_count == 0 ) {
8082 free (* tcp_relay_ports );
8183 * tcp_relay_ports = nullptr ;
8284 }
@@ -98,6 +100,10 @@ static void parse_tcp_relay_ports_config(config_t *cfg, uint16_t **tcp_relay_por
98100 }
99101
100102 * tcp_relay_ports = (uint16_t * )malloc (config_port_count * sizeof (uint16_t ));
103+ if (* tcp_relay_ports == nullptr ) {
104+ log_write (LOG_LEVEL_ERROR , "Allocation failure.\n" );
105+ return ;
106+ }
101107
102108 for (int i = 0 ; i < config_port_count ; ++ i ) {
103109 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
125131 ++ * tcp_relay_port_count ;
126132 }
127133
128- // The loop above skips invalid ports, so we adjust the allocated memory size
129- if ((* tcp_relay_port_count ) > 0 ) {
130- * tcp_relay_ports = (uint16_t * )realloc (* tcp_relay_ports , (* tcp_relay_port_count ) * sizeof (uint16_t ));
131- } else {
134+ // No ports, so we free the array.
135+ if (* tcp_relay_port_count == 0 ) {
132136 free (* tcp_relay_ports );
133137 * tcp_relay_ports = nullptr ;
134138 }
@@ -177,6 +181,10 @@ bool get_general_config(const char *cfg_file_path, char **pid_file_path, char **
177181
178182 const size_t pid_file_path_len = strlen (tmp_pid_file ) + 1 ;
179183 * pid_file_path = (char * )malloc (pid_file_path_len );
184+ if (* pid_file_path == nullptr ) {
185+ log_write (LOG_LEVEL_ERROR , "Allocation failure.\n" );
186+ return false;
187+ }
180188 memcpy (* pid_file_path , tmp_pid_file , pid_file_path_len );
181189
182190 // Get keys file location
@@ -190,6 +198,10 @@ bool get_general_config(const char *cfg_file_path, char **pid_file_path, char **
190198
191199 const size_t keys_file_path_len = strlen (tmp_keys_file ) + 1 ;
192200 * keys_file_path = (char * )malloc (keys_file_path_len );
201+ if (* keys_file_path == nullptr ) {
202+ log_write (LOG_LEVEL_ERROR , "Allocation failure.\n" );
203+ return false;
204+ }
193205 memcpy (* keys_file_path , tmp_keys_file , keys_file_path_len );
194206
195207 // Get IPv6 option
@@ -304,6 +316,10 @@ static uint8_t *bootstrap_hex_string_to_bin(const char *hex_string)
304316
305317 const size_t len = strlen (hex_string ) / 2 ;
306318 uint8_t * ret = (uint8_t * )malloc (len );
319+ if (ret == nullptr ) {
320+ log_write (LOG_LEVEL_ERROR , "Allocation failure.\n" );
321+ return nullptr ;
322+ }
307323
308324 const char * pos = hex_string ;
309325
0 commit comments