fix: leak
This commit is contained in:
19
config.c
19
config.c
@@ -69,10 +69,12 @@ int checkSection(char *str,char delimiterLeft,char delimiterRight,char **section
|
|||||||
break;
|
break;
|
||||||
case ST_FOUND_RIGHT_DELIMITER:
|
case ST_FOUND_RIGHT_DELIMITER:
|
||||||
int ret=0;
|
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,§ionName2,MAX_LEN_SECTIONNAME)) == NO_ERROR)
|
if((ret=getStrAtPos(str,leftDelimiterPos,rightDelimiterPos,§ionName2,MAX_LEN_SECTIONNAME)) == NO_ERROR)
|
||||||
{
|
{
|
||||||
*sectionName = sectionName2;
|
*sectionName = strdup(sectionName2);
|
||||||
|
free(sectionName2);
|
||||||
state = ST_FINISH;
|
state = ST_FINISH;
|
||||||
}else{
|
}else{
|
||||||
return ret;
|
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] = str[i];
|
||||||
}
|
}
|
||||||
ptr_name[j+1]='\0';
|
ptr_name[j+1]='\0';
|
||||||
printf("*name:%s\n",*name);
|
//printf("*name:%s\n",ptr_name);
|
||||||
return NO_ERROR;
|
return NO_ERROR;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -167,8 +169,8 @@ int parseConfig(char *buffer,struct configEntry **entry,int configSizeCount,int
|
|||||||
char *keyName=NULL;
|
char *keyName=NULL;
|
||||||
char *keyValue=NULL;
|
char *keyValue=NULL;
|
||||||
|
|
||||||
keyName = malloc(MAX_LEN_SECTIONNAME);
|
sectionName = malloc(MAX_LEN_SECTIONNAME);
|
||||||
keyValue = malloc(MAX_LEN_SECTIONNAME);
|
|
||||||
//read buffer line by line
|
//read buffer line by line
|
||||||
char *token;
|
char *token;
|
||||||
token = strtok(buffer,"\n");
|
token = strtok(buffer,"\n");
|
||||||
@@ -178,12 +180,15 @@ int parseConfig(char *buffer,struct configEntry **entry,int configSizeCount,int
|
|||||||
{
|
{
|
||||||
case ST_INIT:
|
case ST_INIT:
|
||||||
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)
|
||||||
{
|
{
|
||||||
state = ST_FOUND_SECTION;
|
state = ST_FOUND_SECTION;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case ST_FOUND_SECTION:
|
case ST_FOUND_SECTION:
|
||||||
|
keyName = malloc(MAX_LEN_SECTIONNAME);
|
||||||
|
keyValue = malloc(MAX_LEN_SECTIONNAME);
|
||||||
if((ret=getNameValuePair(token,'=',0,
|
if((ret=getNameValuePair(token,'=',0,
|
||||||
&keyName,&keyValue,
|
&keyName,&keyValue,
|
||||||
MAX_LEN_SECTIONNAME,MAX_LEN_SECTIONNAME
|
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].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(keyName);
|
||||||
|
free(keyValue);
|
||||||
if(i<configSizeCount-1)
|
if(i<configSizeCount-1)
|
||||||
{
|
{
|
||||||
i++;
|
i++;
|
||||||
*returnedCount = i;
|
*returnedCount = i;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
free(sectionName);
|
||||||
return ERROR_STR;
|
return ERROR_STR;
|
||||||
}
|
}
|
||||||
state = ST_FOUND_SECTION;
|
state = ST_FOUND_SECTION;
|
||||||
@@ -219,5 +227,6 @@ int parseConfig(char *buffer,struct configEntry **entry,int configSizeCount,int
|
|||||||
|
|
||||||
free(keyName);
|
free(keyName);
|
||||||
free(keyValue);
|
free(keyValue);
|
||||||
|
free(sectionName);
|
||||||
return NO_ERROR;
|
return NO_ERROR;
|
||||||
}
|
}
|
||||||
|
|||||||
5
file.c
5
file.c
@@ -27,14 +27,14 @@ int getFile(char *fname, char **strContent,int cbSize,long *neededSize)
|
|||||||
file_size = ftell(hfile);
|
file_size = ftell(hfile);
|
||||||
if(strContent == NULL) //we must determine the size and return to neededSize
|
if(strContent == NULL) //we must determine the size and return to neededSize
|
||||||
{
|
{
|
||||||
*neededSize = file_size;
|
*neededSize = file_size+1;
|
||||||
fclose(hfile);
|
fclose(hfile);
|
||||||
return NO_ERROR;
|
return NO_ERROR;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(cbSize < file_size)
|
if(cbSize < file_size)
|
||||||
{
|
{
|
||||||
*neededSize = file_size;
|
*neededSize = file_size+1; //null terminator
|
||||||
fclose(hfile);
|
fclose(hfile);
|
||||||
return FILE_ERROR_STRCONTENT_TO_SMALL;
|
return FILE_ERROR_STRCONTENT_TO_SMALL;
|
||||||
}
|
}
|
||||||
@@ -48,6 +48,7 @@ int getFile(char *fname, char **strContent,int cbSize,long *neededSize)
|
|||||||
fclose(hfile);
|
fclose(hfile);
|
||||||
return FILE_ERROR_READ_MISMATCH;
|
return FILE_ERROR_READ_MISMATCH;
|
||||||
}
|
}
|
||||||
|
strContent[*neededSize]='\0';
|
||||||
fclose(hfile);
|
fclose(hfile);
|
||||||
return NO_ERROR;
|
return NO_ERROR;
|
||||||
}
|
}
|
||||||
|
|||||||
12
test.c
12
test.c
@@ -13,13 +13,16 @@ int main(void)
|
|||||||
name = malloc(MAX_LEN_SECTIONNAME*sizeof(char));
|
name = malloc(MAX_LEN_SECTIONNAME*sizeof(char));
|
||||||
getStrAtPos(teststr, 11, 15, &name, MAX_LEN_SECTIONNAME);
|
getStrAtPos(teststr, 11, 15, &name, MAX_LEN_SECTIONNAME);
|
||||||
|
|
||||||
sectionName = malloc(MAX_LEN_SECTIONNAME*sizeof(char));
|
sectionName = malloc(MAX_LEN_SECTIONNAME);
|
||||||
if((ret=checkSection(teststr,'[',']', §ionName)) == FOUND_SECTION)
|
memset(sectionName,0,MAX_LEN_SECTIONNAME);
|
||||||
|
ret=checkSection(teststr,'[',']', §ionName);
|
||||||
|
if(ret == FOUND_SECTION)
|
||||||
{
|
{
|
||||||
printf("checkSection sucessfull\nsectionName=%s\n",sectionName);
|
printf("checkSection sucessfull\nsectionName=%s\n",sectionName);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
free(sectionName);
|
||||||
printf("an error occured:%d\n",ret);
|
printf("an error occured:%d\n",ret);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -30,6 +33,8 @@ int main(void)
|
|||||||
|
|
||||||
keyName = malloc(MAX_LEN_SECTIONNAME);
|
keyName = malloc(MAX_LEN_SECTIONNAME);
|
||||||
keyValue = 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,
|
if((ret=getNameValuePair(testpair,
|
||||||
'=',0,
|
'=',0,
|
||||||
@@ -48,6 +53,7 @@ int main(void)
|
|||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
printf("Error on getFile:%d\n",ret);
|
printf("Error on getFile:%d\n",ret);
|
||||||
|
return 1;
|
||||||
}
|
}
|
||||||
content = malloc(neededSize);
|
content = malloc(neededSize);
|
||||||
if((ret=getFile("config.cfg",&content,neededSize,&neededSize))==NO_ERROR)
|
if((ret=getFile("config.cfg",&content,neededSize,&neededSize))==NO_ERROR)
|
||||||
@@ -56,6 +62,8 @@ int main(void)
|
|||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
printf("Error on getFile:%d\n",ret);
|
printf("Error on getFile:%d\n",ret);
|
||||||
|
free(content);
|
||||||
|
return 1;
|
||||||
}
|
}
|
||||||
struct configEntry *entry=NULL;
|
struct configEntry *entry=NULL;
|
||||||
int returnedCount=0;
|
int returnedCount=0;
|
||||||
|
|||||||
Reference in New Issue
Block a user