fix:no more leaks possible
This commit is contained in:
7
config.c
7
config.c
@@ -165,7 +165,8 @@ int parseConfig(char *buffer,struct configEntry **entry,int configSizeCount,int
|
||||
char *keyName=NULL;
|
||||
char *keyValue=NULL;
|
||||
|
||||
|
||||
sectionName = malloc(MAX_LEN_SECTIONNAME);
|
||||
memset(sectionName,0,MAX_LEN_SECTIONNAME);
|
||||
|
||||
//read buffer line by line
|
||||
char *token;
|
||||
@@ -175,7 +176,6 @@ int parseConfig(char *buffer,struct configEntry **entry,int configSizeCount,int
|
||||
switch(state)
|
||||
{
|
||||
case ST_INIT:
|
||||
sectionName = malloc(MAX_LEN_SECTIONNAME);
|
||||
printf("token(line): %s\n",token);
|
||||
|
||||
if((ret=checkSection(token,'[',']',§ionName))==FOUND_SECTION)
|
||||
@@ -196,7 +196,6 @@ int parseConfig(char *buffer,struct configEntry **entry,int configSizeCount,int
|
||||
ptr_entry[i].sectionName = strdup(sectionName);
|
||||
ptr_entry[i].keyValue= strdup(keyValue);
|
||||
ptr_entry[i].keyName = strdup(keyName);
|
||||
free(sectionName);
|
||||
free(keyName);
|
||||
free(keyValue);
|
||||
if(i<configSizeCount-1)
|
||||
@@ -211,6 +210,8 @@ int parseConfig(char *buffer,struct configEntry **entry,int configSizeCount,int
|
||||
state = ST_FOUND_SECTION;
|
||||
}
|
||||
else {
|
||||
free(keyName);
|
||||
free(keyValue);
|
||||
state = ST_SKIP_READ;
|
||||
}
|
||||
break;
|
||||
|
||||
6
test.c
6
test.c
@@ -73,10 +73,14 @@ int main(void)
|
||||
|
||||
entry = malloc(10*sizeof(struct configEntry));
|
||||
parseConfig(content,&entry,10*sizeof(struct configEntry),&returnedCount);
|
||||
free(content);
|
||||
|
||||
for(i=0;i<returnedCount;i++)
|
||||
{
|
||||
printf("i:%d/%d, struct section: %s, keyname: %s, keyvalue: %s\n",i,returnedCount,entry[i].sectionName,entry[i].keyName,entry[i].keyValue);
|
||||
free(entry[i].keyName);
|
||||
free(entry[i].keyValue);
|
||||
free(entry[i].sectionName);
|
||||
}
|
||||
|
||||
|
||||
@@ -85,7 +89,7 @@ int main(void)
|
||||
free(keyValue);
|
||||
free(keyName);
|
||||
free(name);
|
||||
free(entry->keyName);
|
||||
free(entry);
|
||||
free(sectionName);
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user