Test malloc/realloc return values

This commit is contained in:
Matteo Cypriani 2012-09-09 17:28:12 +02:00
parent e9e6d25894
commit d1c64212d8
5 changed files with 100 additions and 1 deletions

View File

@ -103,6 +103,11 @@ owl_result* owl_fill_result(char *csv)
owl_result *result = NULL ; owl_result *result = NULL ;
result = malloc(sizeof(owl_result)) ; result = malloc(sizeof(owl_result)) ;
if (! result)
{
perror("Cannot allocate memory") ;
return NULL ;
}
memset(result, 0, sizeof(*result)) ; memset(result, 0, sizeof(*result)) ;
/* Mobile MAC address */ /* Mobile MAC address */
@ -193,6 +198,11 @@ owl_algorithm_result* owl_fill_algorithm_result(char **csv)
char *csv_field = NULL ; char *csv_field = NULL ;
algo = malloc(sizeof(owl_algorithm_result)) ; algo = malloc(sizeof(owl_algorithm_result)) ;
if (! algo)
{
perror("Cannot allocate memory") ;
return NULL ;
}
memset(algo, 0, sizeof(*algo)) ; memset(algo, 0, sizeof(*algo)) ;
// Algorithm name // Algorithm name

View File

@ -295,6 +295,12 @@ int parse_config_file(int argc, char **argv)
{ {
case 'f' : case 'f' :
config_file = malloc((strlen(optarg) + 1) * sizeof(char)) ; config_file = malloc((strlen(optarg) + 1) * sizeof(char)) ;
if (! config_file)
{
perror("Cannot allocate memory") ;
owl_run = owl_false ;
return errno ;
}
strcpy(config_file, optarg) ; strcpy(config_file, optarg) ;
break ; break ;
case 'h' : case 'h' :
@ -313,6 +319,12 @@ int parse_config_file(int argc, char **argv)
{ {
config_file = config_file =
malloc((strlen(DEFAULT_CONFIG_FILE) + 1) * sizeof(char)) ; malloc((strlen(DEFAULT_CONFIG_FILE) + 1) * sizeof(char)) ;
if (! config_file)
{
perror("Cannot allocate memory") ;
owl_run = owl_false ;
return errno ;
}
strcpy(config_file, DEFAULT_CONFIG_FILE) ; strcpy(config_file, DEFAULT_CONFIG_FILE) ;
} }
@ -821,6 +833,13 @@ void got_request(owl_captured_request request)
/* Create a new request */ /* Create a new request */
tmp_info = malloc(sizeof(request_info_list)) ; tmp_info = malloc(sizeof(request_info_list)) ;
if (! tmp_info)
{
perror("Cannot allocate memory") ;
owl_run = owl_false ;
return ;
}
tmp_info->packet_id = request.packet_id ; tmp_info->packet_id = request.packet_id ;
memcpy(tmp_info->ap_mac_addr_bytes, request.ap_mac_addr_bytes, memcpy(tmp_info->ap_mac_addr_bytes, request.ap_mac_addr_bytes,
ETHER_ADDR_LEN) ; ETHER_ADDR_LEN) ;
@ -838,6 +857,12 @@ void got_request(owl_captured_request request)
fprintf(stderr, "Creating request list with AP \"%s\".\n", fprintf(stderr, "Creating request list with AP \"%s\".\n",
owl_mac_bytes_to_string(request.ap_mac_addr_bytes)) ; owl_mac_bytes_to_string(request.ap_mac_addr_bytes)) ;
tmp_request = malloc(sizeof(request_list)) ; // create it. tmp_request = malloc(sizeof(request_list)) ; // create it.
if (! tmp_request)
{
perror("Cannot allocate memory") ;
owl_run = owl_false ;
goto end ;
}
tmp_request->type = request.type ; tmp_request->type = request.type ;
tmp_request->nb_packets = request.nb_packets ; tmp_request->nb_packets = request.nb_packets ;
memcpy(tmp_request->mobile_mac_addr_bytes, memcpy(tmp_request->mobile_mac_addr_bytes,
@ -901,6 +926,12 @@ void got_request(owl_captured_request request)
fprintf(stderr, "Create new request from AP \"%s\".\n", fprintf(stderr, "Create new request from AP \"%s\".\n",
owl_mac_bytes_to_string(request.ap_mac_addr_bytes)) ; owl_mac_bytes_to_string(request.ap_mac_addr_bytes)) ;
tmp_request = malloc(sizeof(request_list)) ; // create it tmp_request = malloc(sizeof(request_list)) ; // create it
if (! tmp_request)
{
perror("Cannot allocate memory") ;
owl_run = owl_false ;
goto end ;
}
tmp_request->type = request.type ; tmp_request->type = request.type ;
tmp_request->nb_packets = request.nb_packets ; tmp_request->nb_packets = request.nb_packets ;
memcpy(tmp_request->mobile_mac_addr_bytes, memcpy(tmp_request->mobile_mac_addr_bytes,
@ -944,6 +975,7 @@ void got_request(owl_captured_request request)
} }
} }
end:
sem_post(&lock_requests) ; sem_post(&lock_requests) ;
} }
@ -1044,7 +1076,8 @@ void update_ap(uint8_t mac_addr_bytes[ETHER_ADDR_LEN],
if ((found = find_ap(mac_addr_bytes)) == NULL) if ((found = find_ap(mac_addr_bytes)) == NULL)
{ {
ap_list *new_ap = add_ap_front(mac_addr_bytes) ; ap_list *new_ap = add_ap_front(mac_addr_bytes) ;
update_ap_ip_addr(new_ap, ip_addr) ; if (new_ap)
update_ap_ip_addr(new_ap, ip_addr) ;
} }
else else
@ -1078,6 +1111,7 @@ ap_list* find_ap(uint8_t mac_addr_bytes[ETHER_ADDR_LEN])
/* /*
* Adds a new AP in front of the AP list. * Adds a new AP in front of the AP list.
* Returns the new list head, or NULL in case of error.
*/ */
ap_list* add_ap_front(uint8_t mac_addr_bytes[ETHER_ADDR_LEN]) ap_list* add_ap_front(uint8_t mac_addr_bytes[ETHER_ADDR_LEN])
{ {
@ -1090,6 +1124,12 @@ ap_list* add_ap_front(uint8_t mac_addr_bytes[ETHER_ADDR_LEN])
} }
ap_list *ap = malloc(sizeof(ap_list)) ; ap_list *ap = malloc(sizeof(ap_list)) ;
if (! ap)
{
perror("Cannot allocate memory") ;
owl_run = owl_false ;
return NULL ;
}
memcpy(ap->mac_addr_bytes, mac_addr_bytes, ETHER_ADDR_LEN) ; memcpy(ap->mac_addr_bytes, mac_addr_bytes, ETHER_ADDR_LEN) ;
update_ap_seen(ap) ; update_ap_seen(ap) ;
push_ap(ap) ; push_ap(ap) ;

View File

@ -529,6 +529,11 @@ void make_packet()
sizeof(uint16_t) * 2 ; sizeof(uint16_t) * 2 ;
add_padding() ; add_padding() ;
packet = malloc(packet_size) ; packet = malloc(packet_size) ;
if (! packet)
{
perror("Cannot allocate memory") ;
abort() ;
}
offset = initialise_common_fields(OWL_REQUEST_CALIBRATION) ; offset = initialise_common_fields(OWL_REQUEST_CALIBRATION) ;
offset += initialise_calibration_fields(offset) ; offset += initialise_calibration_fields(offset) ;
@ -542,6 +547,11 @@ void make_packet()
sizeof(uint8_t) + sizeof(owl_timestamp) + sizeof(uint16_t) * 2 ; sizeof(uint8_t) + sizeof(owl_timestamp) + sizeof(uint16_t) * 2 ;
add_padding() ; add_padding() ;
packet = malloc(packet_size) ; packet = malloc(packet_size) ;
if (! packet)
{
perror("Cannot allocate memory") ;
abort() ;
}
offset = initialise_common_fields(OWL_REQUEST_NORMAL) ; offset = initialise_common_fields(OWL_REQUEST_NORMAL) ;
} }

View File

@ -415,6 +415,12 @@ int parse_config_file(int argc, char **argv)
case 'f' : case 'f' :
#ifdef USE_CONFIG_FILE #ifdef USE_CONFIG_FILE
config_file = malloc((strlen(optarg) + 1) * sizeof(char)) ; config_file = malloc((strlen(optarg) + 1) * sizeof(char)) ;
if (! config_file)
{
perror("Cannot allocate memory") ;
owl_run = owl_false ;
return errno ;
}
strcpy(config_file, optarg) ; strcpy(config_file, optarg) ;
#else // USE_CONFIG_FILE #else // USE_CONFIG_FILE
fprintf(stderr, "Warning! Program was not compiled with" fprintf(stderr, "Warning! Program was not compiled with"
@ -440,6 +446,12 @@ int parse_config_file(int argc, char **argv)
{ {
config_file = config_file =
malloc((strlen(DEFAULT_CONFIG_FILE) + 1) * sizeof(char)) ; malloc((strlen(DEFAULT_CONFIG_FILE) + 1) * sizeof(char)) ;
if (! config_file)
{
perror("Cannot allocate memory") ;
owl_run = owl_false ;
return errno ;
}
strcpy(config_file, DEFAULT_CONFIG_FILE) ; strcpy(config_file, DEFAULT_CONFIG_FILE) ;
} }
@ -1464,6 +1476,7 @@ void send_autocalibration_request()
* Creates the calibration packet to send. * Creates the calibration packet to send.
* The packet must be freed by the calling function. * The packet must be freed by the calling function.
* Returns the size of the packet. * Returns the size of the packet.
* In case of error, 0 is returned and *packet is not updated.
*/ */
uint_fast16_t make_packet(uint8_t **packet) uint_fast16_t make_packet(uint8_t **packet)
{ {
@ -1493,6 +1506,11 @@ uint_fast16_t make_packet(uint8_t **packet)
sizeof(uint8_t) * 2 + sizeof(owl_timestamp) + sizeof(float) * 3 + sizeof(uint8_t) * 2 + sizeof(owl_timestamp) + sizeof(float) * 3 +
sizeof(uint16_t) * 2 ; sizeof(uint16_t) * 2 ;
pkt = malloc(size) ; pkt = malloc(size) ;
if (! pkt)
{
perror("Cannot allocate memory") ;
return 0 ;
}
// Request type: // Request type:
memset(&pkt[offset++], OWL_REQUEST_AUTOCALIBRATION, 1) ; memset(&pkt[offset++], OWL_REQUEST_AUTOCALIBRATION, 1) ;

View File

@ -225,6 +225,12 @@ void store_result(owl_result *new_result)
if (! results) if (! results)
{ {
results = malloc(sizeof(results_list)) ; results = malloc(sizeof(results_list)) ;
if (! results)
{
perror("Cannot allocate memory") ;
owl_run = owl_false ;
goto end ;
}
++nb_results ; ++nb_results ;
results->result = new_result ; results->result = new_result ;
results->next = NULL ; results->next = NULL ;
@ -247,6 +253,12 @@ void store_result(owl_result *new_result)
if (res == NULL) // Not found, adding an element if (res == NULL) // Not found, adding an element
{ {
res = malloc(sizeof(results_list)) ; res = malloc(sizeof(results_list)) ;
if (! res)
{
perror("Cannot allocate memory") ;
owl_run = owl_false ;
goto end ;
}
++nb_results ; ++nb_results ;
res->next = results ; res->next = results ;
results = res ; results = res ;
@ -257,6 +269,7 @@ void store_result(owl_result *new_result)
res->result = new_result ; res->result = new_result ;
} }
end:
sem_post(&lock_results) ; sem_post(&lock_results) ;
} }
@ -313,6 +326,12 @@ void* tcp_server(void *NULL_value)
// message will also fit) // message will also fit)
answer_buflen = ANSWER_HDR_STRLEN + OWL_CSV_RESULT_STRLEN ; answer_buflen = ANSWER_HDR_STRLEN + OWL_CSV_RESULT_STRLEN ;
answer = malloc(answer_buflen) ; answer = malloc(answer_buflen) ;
if (! answer_buflen)
{
perror("Cannot allocate memory") ;
owl_run = owl_false ;
return NULL ;
}
strncpy(answer, ANSWER_HDR, ANSWER_HDR_STRLEN) ; strncpy(answer, ANSWER_HDR, ANSWER_HDR_STRLEN) ;
while (owl_run) while (owl_run)
@ -533,6 +552,8 @@ void realloc_answer(size_t new_size)
return ; return ;
answer = realloc(answer, answer_buflen) ; answer = realloc(answer, answer_buflen) ;
if (! answer)
abort() ;
} }