From ee397206894cfb727b8b2ce021f584f737518d17 Mon Sep 17 00:00:00 2001 From: Smriti Gupta Date: Wed, 6 Dec 2017 15:00:40 +0530 Subject: iFM: Removing Memory leaks from the HAL Test Code Test code had some of the allocs not freed up, added code to free up the leaks. Change-Id: I20a61e76f006334731378c01800cf6ad14a69e49 CRs-Fixed: 2159776 --- jni/ConfFileParser.cpp | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) (limited to 'jni/ConfFileParser.cpp') diff --git a/jni/ConfFileParser.cpp b/jni/ConfFileParser.cpp index 1644585..444d3c3 100644 --- a/jni/ConfFileParser.cpp +++ b/jni/ConfFileParser.cpp @@ -790,8 +790,8 @@ static char line_is_key_value_pair ) { const char *equal_start; - char *key; - char *val; + char *key = NULL; + char *val = NULL; unsigned key_len; unsigned val_len; @@ -819,6 +819,10 @@ static char line_is_key_value_pair val = (char *)malloc(sizeof(char) * (val_len + 1)); if(val == NULL) { ALOGE("could not alloc memory for value\n"); + if(key){ + free(key); + key = NULL; + } return FALSE; } memcpy(key, (str - key_len), key_len); @@ -866,20 +870,20 @@ static char add_key_value_pair list = grp->list[key_index]; }else { ALOGE("group list is null\n"); - return FALSE; + goto err; } while((list != NULL) && strcmp(key, list->key)) { list = list->next; } if(list != NULL) { ALOGE("group already contains the key\n"); - return FALSE; + goto err; }else{ list = alloc_key_value_pair(); if(list == NULL) { ALOGE("add key value failed as could not alloc memory for key\ val pair\n"); - return FALSE; + goto err; } key_len = strlen(key); list->key = (char *)malloc(sizeof(char) * @@ -887,7 +891,7 @@ static char add_key_value_pair if(list->key == NULL) { ALOGE("could not alloc memory for key\n"); free(list); - return FALSE; + goto err; } val_len = strlen(val); list->value = (char *)malloc(sizeof(char) * @@ -895,10 +899,12 @@ static char add_key_value_pair if(!list->value) { free(list->key); free(list); - return FALSE; + goto err; } memcpy(list->key, key, key_len); memcpy(list->value, val, val_len); + if (key) free((char*)key); + if (val) free((char*)val); list->key[key_len] = '\0'; list->value[val_len] = '\0'; list->next = grp->list[key_index]; @@ -910,8 +916,10 @@ static char add_key_value_pair grp = grp->grp_next; } ALOGE("group does not exist\n"); - return FALSE; - }else { - return FALSE; + goto err; } +err: + if (key) free((char*)key); + if (val) free((char*)val); + return FALSE; } -- cgit v1.2.3