Fixed memory leak in ini parser and test functions.

This commit is contained in:
akallabeth 2020-05-15 14:49:52 +02:00
parent 319afb082b
commit 1baf67d881
2 changed files with 48 additions and 34 deletions

View File

@ -206,6 +206,16 @@ static char* IniFile_Load_GetNextLine(wIniFile* ini)
return ini->line;
}
static void IniFile_Key_Free(wIniFileKey* key)
{
if (!key)
return;
free(key->name);
free(key->value);
free(key);
}
static wIniFileKey* IniFile_Key_New(const char* name, const char* value)
{
wIniFileKey* key;
@ -222,9 +232,7 @@ static wIniFileKey* IniFile_Key_New(const char* name, const char* value)
if (!key->name || !key->value)
{
free(key->name);
free(key->value);
free(key);
IniFile_Key_Free(key);
return NULL;
}
}
@ -232,16 +240,6 @@ static wIniFileKey* IniFile_Key_New(const char* name, const char* value)
return key;
}
static void IniFile_Key_Free(wIniFileKey* key)
{
if (!key)
return;
free(key->name);
free(key->value);
free(key);
}
static wIniFileSection* IniFile_Section_New(const char* name)
{
wIniFileSection* section;
@ -821,5 +819,6 @@ void IniFile_Free(wIniFile* ini)
IniFile_Section_Free(ini->sections[index]);
free(ini->sections);
free(ini->buffer);
free(ini);
}

View File

@ -38,40 +38,48 @@ const char TEST_INI_03[] = "[FreeRDS]\n"
int TestIni(int argc, char* argv[])
{
int rc = -1;
int i, j;
int nKeys;
int nSections;
UINT32 iValue;
wIniFile* ini;
wIniFile* ini = NULL;
const char* sValue;
char** keyNames;
char** sectionNames;
char** keyNames = NULL;
char** sectionNames = NULL;
/* First Sample */
ini = IniFile_New();
IniFile_ReadBuffer(ini, TEST_INI_01);
if (!ini)
goto fail;
if (IniFile_ReadBuffer(ini, TEST_INI_01) < 0)
goto fail;
free(sectionNames);
sectionNames = IniFile_GetSectionNames(ini, &nSections);
if (!sectionNames && (nSections > 0))
goto fail;
for (i = 0; i < nSections; i++)
{
free(keyNames);
keyNames = IniFile_GetSectionKeyNames(ini, sectionNames[i], &nKeys);
printf("[%s]\n", sectionNames[i]);
if (!keyNames && (nKeys > 0))
goto fail;
for (j = 0; j < nKeys; j++)
{
sValue = IniFile_GetKeyValueString(ini, sectionNames[i], keyNames[j]);
printf("%s = %s\n", keyNames[j], sValue);
}
free(keyNames);
}
free(sectionNames);
iValue = IniFile_GetKeyValueInt(ini, "first_section", "one");
if (iValue != 1)
{
printf("IniFile_GetKeyValueInt failure\n");
return -1;
goto fail;
}
iValue = IniFile_GetKeyValueInt(ini, "first_section", "five");
@ -79,7 +87,7 @@ int TestIni(int argc, char* argv[])
if (iValue != 5)
{
printf("IniFile_GetKeyValueInt failure\n");
return -1;
goto fail;
}
sValue = IniFile_GetKeyValueString(ini, "first_section", "animal");
@ -87,7 +95,7 @@ int TestIni(int argc, char* argv[])
if (strcmp(sValue, "BIRD") != 0)
{
printf("IniFile_GetKeyValueString failure\n");
return -1;
goto fail;
}
sValue = IniFile_GetKeyValueString(ini, "second_section", "path");
@ -95,7 +103,7 @@ int TestIni(int argc, char* argv[])
if (strcmp(sValue, "/usr/local/bin") != 0)
{
printf("IniFile_GetKeyValueString failure\n");
return -1;
goto fail;
}
sValue = IniFile_GetKeyValueString(ini, "second_section", "URL");
@ -103,40 +111,47 @@ int TestIni(int argc, char* argv[])
if (strcmp(sValue, "http://www.example.com/~username") != 0)
{
printf("IniFile_GetKeyValueString failure\n");
return -1;
goto fail;
}
IniFile_Free(ini);
/* Second Sample */
ini = IniFile_New();
IniFile_ReadBuffer(ini, TEST_INI_02);
if (!ini)
goto fail;
if (IniFile_ReadBuffer(ini, TEST_INI_02) < 0)
goto fail;
free(sectionNames);
sectionNames = IniFile_GetSectionNames(ini, &nSections);
if (!sectionNames && (nSections > 0))
goto fail;
for (i = 0; i < nSections; i++)
{
free(keyNames);
keyNames = IniFile_GetSectionKeyNames(ini, sectionNames[i], &nKeys);
printf("[%s]\n", sectionNames[i]);
if (!keyNames && (nKeys > 0))
goto fail;
for (j = 0; j < nKeys; j++)
{
sValue = IniFile_GetKeyValueString(ini, sectionNames[i], keyNames[j]);
printf("%s = %s\n", keyNames[j], sValue);
}
free(keyNames);
}
free(sectionNames);
IniFile_Free(ini);
/* Third sample - invalid input */
ini = IniFile_New();
if (IniFile_ReadBuffer(ini, TEST_INI_03) != -1)
{
IniFile_Free(ini);
return -1;
}
goto fail;
rc = 0;
fail:
free(keyNames);
free(sectionNames);
IniFile_Free(ini);
return 0;
}