fix: mem leak, test: config parsing until 300000lines and over 1 million structs
This commit is contained in:
85
config.c
85
config.c
@@ -2,6 +2,7 @@
|
||||
#include<stdio.h>
|
||||
#include<stdlib.h>
|
||||
#include<string.h>
|
||||
#include<errno.h>
|
||||
/*
|
||||
* Here we check if the given string contains a section
|
||||
* for example [SECTIONName] here delimiterLeft is [ and delimiterRight is ]
|
||||
@@ -27,14 +28,12 @@ int checkSection(char *str,char delimiterLeft,char delimiterRight,char **section
|
||||
return ERROR_STR;
|
||||
}
|
||||
int len = strlen(str);
|
||||
printf("strlen: %d",len);
|
||||
int state = ST_INIT;
|
||||
int leftDelimiterPos=0;
|
||||
int rightDelimiterPos=0;
|
||||
char *sectionName2 = NULL;
|
||||
while(state != ST_FINISH)
|
||||
{
|
||||
printf("STATE:%i\n",state);
|
||||
switch(state)
|
||||
{
|
||||
case ST_INIT:
|
||||
@@ -96,6 +95,12 @@ int checkSection(char *str,char delimiterLeft,char delimiterRight,char **section
|
||||
|
||||
int getStrAtPos(char *str,int fromPos,int toPos,char **name,int sizeName)
|
||||
{
|
||||
if(*name == NULL)
|
||||
{
|
||||
int error = errno;
|
||||
printf("Pointer NULL:%d\n",error);
|
||||
return error;
|
||||
}
|
||||
int i=fromPos; //character iterator, which starts from specified pos
|
||||
int j=0; //the character iterator for the target string
|
||||
int diffLen=toPos-fromPos;
|
||||
@@ -109,7 +114,6 @@ 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",ptr_name);
|
||||
return NO_ERROR;
|
||||
}
|
||||
/*
|
||||
@@ -144,6 +148,12 @@ int getStrAtPos(char *str,int fromPos,int toPos,char **name,int sizeName)
|
||||
|
||||
int getNameValuePair(char *str,char leftDelimiterPos,char rightDelimiterPos,char **name,char **value,int sizeName,int sizeValue)
|
||||
{
|
||||
if(*name == NULL || *value == NULL)
|
||||
{
|
||||
int error = errno;
|
||||
printf("Pointer NULL:%d\n",error);
|
||||
return error;
|
||||
}
|
||||
int state=ST_INIT;
|
||||
char *ptr_name=*name;
|
||||
char *ptr_value=*value;
|
||||
@@ -159,11 +169,12 @@ int getNameValuePair(char *str,char leftDelimiterPos,char rightDelimiterPos,char
|
||||
return ERROR_DELIMITER_NOT_FOUND;
|
||||
}
|
||||
posDelimiter = (ptrDelimiter - str);
|
||||
printf("LenUntilDelimiter: %d",posDelimiter);
|
||||
printf("LenUntilDelimiter: %d\n",posDelimiter);
|
||||
if((ret=getStrAtPos(str,0,posDelimiter-1,&ptr_name,sizeName)) == NO_ERROR)
|
||||
{
|
||||
printf("ptr_name:%s",ptr_name);
|
||||
printf("ptr_name:%s\n",ptr_name);
|
||||
}else {
|
||||
printf("Error at getStrAtPos:%d\n",ret);
|
||||
return ret;
|
||||
}
|
||||
if(rightDelimiterPos == 0)
|
||||
@@ -185,15 +196,33 @@ int getNameValuePair(char *str,char leftDelimiterPos,char rightDelimiterPos,char
|
||||
|
||||
|
||||
|
||||
int parseConfig(char *buffer,struct configEntry **entry,int configSizeCount,int *returnedCount)
|
||||
int parseConfig(char *originalBuffer,struct configEntry **entry,int configSizeCount,int *returnedCount)
|
||||
{
|
||||
int state=ST_INIT;
|
||||
int ret=0;
|
||||
int i=0;
|
||||
*returnedCount=0;
|
||||
char *sectionName=NULL;
|
||||
struct configEntry *ptr_entry = *entry;
|
||||
char *keyName=NULL;
|
||||
char *keyValue=NULL;
|
||||
char *buffer=NULL;
|
||||
|
||||
buffer = malloc(strlen(originalBuffer)+1);
|
||||
if(buffer == NULL)
|
||||
{
|
||||
int error = errno;
|
||||
printf("ERROR MALLOC:%d",error);
|
||||
return ERR_PARSECONFIG_UNKNOWN;
|
||||
}
|
||||
memset(buffer,0,strlen(originalBuffer)+1);
|
||||
strcpy(buffer,originalBuffer);
|
||||
|
||||
//this step is necessary, because the strok function modifys the originalBuffer
|
||||
//so we make a copy of it
|
||||
strcpy(buffer,originalBuffer);
|
||||
|
||||
printf("buffer:%s\n--",buffer);
|
||||
|
||||
sectionName = malloc(MAX_LEN_SECTIONNAME);
|
||||
memset(sectionName,0,MAX_LEN_SECTIONNAME);
|
||||
@@ -203,47 +232,47 @@ int parseConfig(char *buffer,struct configEntry **entry,int configSizeCount,int
|
||||
token = strtok(buffer,"\n");
|
||||
while(token != NULL && state != ST_FINISH)
|
||||
{
|
||||
|
||||
switch(state)
|
||||
{
|
||||
case ST_INIT:
|
||||
printf("token(line): %s\n",token);
|
||||
|
||||
if((ret=checkSection(token,'[',']',§ionName))==FOUND_SECTION)
|
||||
{
|
||||
state = ST_FOUND_SECTION;
|
||||
printf("FOUND_SECTION:%s\n",sectionName);
|
||||
}
|
||||
break;
|
||||
case ST_FOUND_SECTION:
|
||||
keyName = malloc(MAX_LEN_SECTIONNAME);
|
||||
keyValue = malloc(MAX_LEN_SECTIONNAME);
|
||||
if(keyName == NULL || keyValue == NULL)
|
||||
{
|
||||
int error = errno;
|
||||
printf("MALLOC:%d\n",error);
|
||||
//return error;
|
||||
}
|
||||
memset(keyName,0,MAX_LEN_SECTIONNAME);
|
||||
memset(keyValue,0,MAX_LEN_SECTIONNAME);
|
||||
if((ret=getNameValuePair(token,'=',0,
|
||||
&keyName,&keyValue,
|
||||
MAX_LEN_SECTIONNAME,MAX_LEN_SECTIONNAME
|
||||
))==NO_ERROR)
|
||||
|
||||
ret=getNameValuePair(token,'=',0,&keyName,&keyValue,MAX_LEN_SECTIONNAME,MAX_LEN_SECTIONNAME);
|
||||
if(ret==NO_ERROR)
|
||||
{
|
||||
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)
|
||||
printf("configSizeCount: %d, i:%d\n",configSizeCount,i);
|
||||
if(i<configSizeCount)
|
||||
{
|
||||
i++;
|
||||
*returnedCount = i;
|
||||
}
|
||||
else {
|
||||
free(sectionName);
|
||||
return ERROR_STR;
|
||||
ptr_entry[i].sectionName = strdup(sectionName);
|
||||
ptr_entry[i].keyValue= strdup(keyValue);
|
||||
ptr_entry[i].keyName = strdup(keyName);
|
||||
}
|
||||
i++;
|
||||
*returnedCount = i;
|
||||
state = ST_FOUND_SECTION;
|
||||
}
|
||||
else {
|
||||
free(keyName);
|
||||
free(keyValue);
|
||||
state = ST_SKIP_READ;
|
||||
}
|
||||
free(keyName);
|
||||
free(keyValue);
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -255,7 +284,11 @@ int parseConfig(char *buffer,struct configEntry **entry,int configSizeCount,int
|
||||
state = ST_INIT;
|
||||
}
|
||||
}
|
||||
printf("token: %s\n",token);
|
||||
printf("buffer:%s\n--",buffer);
|
||||
|
||||
free(sectionName);
|
||||
free(buffer);
|
||||
printf("finish exitting parsing\nSTATE:%d\n",state);
|
||||
return NO_ERROR;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user