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;
}

5
file.c
View File

@@ -27,14 +27,14 @@ int getFile(char *fname, char **strContent,int cbSize,long *neededSize)
file_size = ftell(hfile);
if(strContent == NULL) //we must determine the size and return to neededSize
{
*neededSize = file_size;
*neededSize = file_size+1;
fclose(hfile);
return NO_ERROR;
}
if(cbSize < file_size)
{
*neededSize = file_size;
*neededSize = file_size+1; //null terminator
fclose(hfile);
return FILE_ERROR_STRCONTENT_TO_SMALL;
}
@@ -48,6 +48,7 @@ int getFile(char *fname, char **strContent,int cbSize,long *neededSize)
fclose(hfile);
return FILE_ERROR_READ_MISMATCH;
}
strContent[*neededSize]='\0';
fclose(hfile);
return NO_ERROR;
}

12
test.c
View File

@@ -13,13 +13,16 @@ int main(void)
name = malloc(MAX_LEN_SECTIONNAME*sizeof(char));
getStrAtPos(teststr, 11, 15, &name, MAX_LEN_SECTIONNAME);
sectionName = malloc(MAX_LEN_SECTIONNAME*sizeof(char));
if((ret=checkSection(teststr,'[',']', &sectionName)) == FOUND_SECTION)
sectionName = malloc(MAX_LEN_SECTIONNAME);
memset(sectionName,0,MAX_LEN_SECTIONNAME);
ret=checkSection(teststr,'[',']', &sectionName);
if(ret == FOUND_SECTION)
{
printf("checkSection sucessfull\nsectionName=%s\n",sectionName);
}
else
{
free(sectionName);
printf("an error occured:%d\n",ret);
}
@@ -30,6 +33,8 @@ int main(void)
keyName = malloc(MAX_LEN_SECTIONNAME);
keyValue = malloc(MAX_LEN_SECTIONNAME);
memset(keyName,0,MAX_LEN_SECTIONNAME);
memset(keyValue,0,MAX_LEN_SECTIONNAME);
if((ret=getNameValuePair(testpair,
'=',0,
@@ -48,6 +53,7 @@ int main(void)
}
else {
printf("Error on getFile:%d\n",ret);
return 1;
}
content = malloc(neededSize);
if((ret=getFile("config.cfg",&content,neededSize,&neededSize))==NO_ERROR)
@@ -56,6 +62,8 @@ int main(void)
}
else {
printf("Error on getFile:%d\n",ret);
free(content);
return 1;
}
struct configEntry *entry=NULL;
int returnedCount=0;