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
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 @@
)
{
const char *equal_start;
- char *key;
- char *val;
+ char *key = NULL;
+ char *val = NULL;
unsigned key_len;
unsigned val_len;
@@ -819,6 +819,10 @@
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 @@
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 @@
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 @@
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 @@
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;
}
diff --git a/libfm_jni/ConfFileParser.cpp b/libfm_jni/ConfFileParser.cpp
index f3d32a4..1817ad7 100644
--- a/libfm_jni/ConfFileParser.cpp
+++ b/libfm_jni/ConfFileParser.cpp
@@ -791,8 +791,8 @@
)
{
char *equal_start;
- char *key;
- char *val;
+ char *key = NULL;
+ char *val = NULL;
unsigned key_len;
unsigned val_len;
@@ -820,6 +820,10 @@
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);
@@ -867,20 +871,20 @@
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) *
@@ -888,7 +892,7 @@
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) *
@@ -896,10 +900,12 @@
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];
@@ -911,8 +917,10 @@
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;
}