fix: mem leak, test: config parsing until 300000lines and over 1 million structs

This commit is contained in:
jonathan santis
2025-05-20 15:27:49 +02:00
parent d07e4d9617
commit a834abc8e7
5 changed files with 156 additions and 98 deletions

59
test.c
View File

@@ -1,4 +1,5 @@
#include "config.h"
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -48,7 +49,7 @@ int main(void)
}
long neededSize=0;
if((ret=getFile("config.cfg",NULL,0,&neededSize))==NO_ERROR)
if((ret=getFile("config-segfault.cfg",NULL,0,&neededSize))==NO_ERROR)
{
printf("sucessfull retrieved size:%ld\n",neededSize);
}
@@ -57,10 +58,17 @@ int main(void)
return 1;
}
content = malloc(neededSize);
memset(content,0,neededSize);
if((ret=getFile("config.cfg",&content,neededSize,&neededSize))==NO_ERROR)
printf("allocate buffer for filecontent size:%ld\n",neededSize);
if(content == NULL)
{
printf("Sucessfull read file into buffer:%s|\n---\n",content);
int error = errno;
printf("MALLOC: %d\n",error);
return error;
}
memset(content,0,neededSize);
if((ret=getFile("config-segfault.cfg",&content,neededSize,&neededSize))==NO_ERROR)
{
//printf("Sucessfull read file into buffer:%s|\n---\n",content);
}
else {
printf("Error on getFile:%d\n",ret);
@@ -70,9 +78,46 @@ int main(void)
struct configEntry *entry=NULL;
int returnedCount=0;
int i=0;
int count=10;
entry = malloc(10*sizeof(struct configEntry));
parseConfig(content,&entry,10*sizeof(struct configEntry),&returnedCount);
entry = malloc(count*sizeof(struct configEntry));
if(entry == NULL)
{
int error = errno;
printf("MALLOC: %d\n",error);
return 0;
}
ret = parseConfig(content,&entry,count,&returnedCount);
if(ret!=NO_ERROR)
{
printf("Error on parseConfig:%d\nReallocate from size %d, to %d\n",ret,count,returnedCount);
}
if(returnedCount > count)
{
for(i=0;i<count;i++)
{
free(entry[i].keyName);
free(entry[i].keyValue);
free(entry[i].sectionName);
}
printf("Error on parseConfig:%d\nReallocate from size %d, to %d\n",ret,count,returnedCount);
if((entry=realloc(entry,returnedCount*sizeof(struct configEntry)))==NULL)
{
printf("error could not reallocate from size %d to %d \n",count,returnedCount);
return 1;
}
ret = parseConfig(content,&entry,returnedCount,&returnedCount);
if(ret!=NO_ERROR)
{
printf("Error on parseConfig:%d\n",ret);
return ret;
}
printf("returnedCount:%d\n",returnedCount);
}
free(content);
for(i=0;i<returnedCount;i++)
@@ -84,8 +129,6 @@ int main(void)
}
free(keyValue);
free(keyName);
free(name);