From 2d9c788b5837d2a8db025306322a5a8f63a8c9af Mon Sep 17 00:00:00 2001 From: Dmitri Pal Date: Fri, 14 Aug 2009 23:04:37 -0400 Subject: COMMON Fixes to return values, errno, leaks Started looking at the ticket #107 related to traverse functions. Realized that the return values are not consistent. That ovelapped with the work that I wanted to do for ticket #103 - errno cleanup. So I (across collection, INI and ELAPI): * Made the return codes consistent (where found) * Removed errno where it is not needed While was testing used valgrind and found a nasty problem when the value was added to collection with overwriting duplicates the count was decreased improperly. Fixing collection.c to not decrease count made valgrind happy. While I was debugging this I also spotted several build warnings in trace statements when the " exp ? v1 : v2 " was used. Fixed those. In ini_config.c there was a trace stament that used variable after it was freed. Removed trace stament. --- collection/collection_tools.c | 24 ++++++++++-------------- 1 file changed, 10 insertions(+), 14 deletions(-) (limited to 'collection/collection_tools.c') diff --git a/collection/collection_tools.c b/collection/collection_tools.c index c6cf995..aa82134 100644 --- a/collection/collection_tools.c +++ b/collection/collection_tools.c @@ -310,11 +310,10 @@ int col_grow_buffer(struct col_serial_data *buf_data, int len) /* Grow buffer if needed */ while (buf_data->length+len >= buf_data->size) { - errno = 0; tmp = realloc(buf_data->buffer, buf_data->size + BLOCK_SIZE); if (tmp == NULL) { - TRACE_ERROR_NUMBER("Error. Failed to allocate memory. Errno: ", errno); - return errno; + TRACE_ERROR_NUMBER("Error. Failed to allocate memory.", ENOMEM); + return ENOMEM; } buf_data->buffer = tmp; buf_data->size += BLOCK_SIZE; @@ -378,11 +377,10 @@ int col_serialize(const char *property_in, } if (buf_data->buffer == NULL) { TRACE_INFO_STRING("First time use.", "Allocating buffer."); - errno = 0; buf_data->buffer = malloc(BLOCK_SIZE); if (buf_data->buffer == NULL) { - TRACE_ERROR_NUMBER("Error. Failed to allocate memory. Errno: ", errno); - return errno; + TRACE_ERROR_NUMBER("Error. Failed to allocate memory.", ENOMEM); + return ENOMEM; } buf_data->buffer[0] = '\0'; buf_data->length = 0; @@ -691,11 +689,9 @@ char **col_collection_to_list(struct collection_item *handle, int *size, int *er } /* Allocate memory for the sections */ - errno = 0; - list = (char **)calloc(count, sizeof(char *)); + list = (char **)malloc(count * sizeof(char *)); if (list == NULL) { - err = errno; - TRACE_ERROR_NUMBER("Failed to get allocate memory.", err); + TRACE_ERROR_NUMBER("Failed to get allocate memory.", ENOMEM); if (error) *error = ENOMEM; return NULL; } @@ -731,11 +727,9 @@ char **col_collection_to_list(struct collection_item *handle, int *size, int *er /* Allocate memory for the new string */ - errno = 0; list[current] = strdup(col_get_item_property(item, NULL)); if (list[current] == NULL) { - err = errno; - TRACE_ERROR_NUMBER("Failed to dup string.", err); + TRACE_ERROR_NUMBER("Failed to dup string.", ENOMEM); if (error) *error = ENOMEM; col_free_property_list(list); return NULL; @@ -743,12 +737,14 @@ char **col_collection_to_list(struct collection_item *handle, int *size, int *er current++; } + list[current] = NULL; + /* Do not forget to unbind iterator - otherwise there will be a leak */ col_unbind_iterator(iterator); if (size) *size = (int)(count - 1); if (error) *error = EOK; - TRACE_FLOW_STRING("col_collection_to_list returning", list == NULL ? "NULL" : list[0]); + TRACE_FLOW_STRING("col_collection_to_list returning", ((list == NULL) ? "NULL" : list[0])); return list; } -- cgit