fix: leak

This commit is contained in:
jonathan santis
2025-05-14 14:47:33 +02:00
parent 137f91de73
commit 9225740cd4
3 changed files with 27 additions and 9 deletions

View File

@@ -69,10 +69,12 @@ int checkSection(char *str,char delimiterLeft,char delimiterRight,char **section
break;
case ST_FOUND_RIGHT_DELIMITER:
int ret=0;
sectionName2 = malloc(MAX_LEN_SECTIONNAME*sizeof(char));
sectionName2 = malloc(MAX_LEN_SECTIONNAME);
memset(sectionName2,0,MAX_LEN_SECTIONNAME);
if((ret=getStrAtPos(str,leftDelimiterPos,rightDelimiterPos,&sectionName2,MAX_LEN_SECTIONNAME)) == NO_ERROR)
{
*sectionName = sectionName2;
*sectionName = strdup(sectionName2);
free(sectionName2);
state = ST_FINISH;
}else{
return ret;
@@ -111,7 +113,7 @@ int getStrAtPos(char *str,int fromPos,int toPos,char **name,int sizeName)
ptr_name[j] = str[i];
}
ptr_name[j+1]='\0';
printf("*name:%s\n",*name);
//printf("*name:%s\n",ptr_name);
return NO_ERROR;
}
@@ -167,8 +169,8 @@ int parseConfig(char *buffer,struct configEntry **entry,int configSizeCount,int
char *keyName=NULL;
char *keyValue=NULL;
keyName = malloc(MAX_LEN_SECTIONNAME);
keyValue = malloc(MAX_LEN_SECTIONNAME);
sectionName = malloc(MAX_LEN_SECTIONNAME);
//read buffer line by line
char *token;
token = strtok(buffer,"\n");
@@ -178,12 +180,15 @@ int parseConfig(char *buffer,struct configEntry **entry,int configSizeCount,int
{
case ST_INIT:
printf("token(line): %s\n",token);
if((ret=checkSection(token,'[',']',&sectionName))==FOUND_SECTION)
{
state = ST_FOUND_SECTION;
}
break;
case ST_FOUND_SECTION:
keyName = malloc(MAX_LEN_SECTIONNAME);
keyValue = malloc(MAX_LEN_SECTIONNAME);
if((ret=getNameValuePair(token,'=',0,
&keyName,&keyValue,
MAX_LEN_SECTIONNAME,MAX_LEN_SECTIONNAME
@@ -192,12 +197,15 @@ 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(keyName);
free(keyValue);
if(i<configSizeCount-1)
{
i++;
*returnedCount = i;
}
else {
free(sectionName);
return ERROR_STR;
}
state = ST_FOUND_SECTION;
@@ -219,5 +227,6 @@ int parseConfig(char *buffer,struct configEntry **entry,int configSizeCount,int
free(keyName);
free(keyValue);
free(sectionName);
return NO_ERROR;
}