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