Avoid a memory allocation when overwriting values
This commit is contained in:
parent
489e810f8b
commit
c5e49563e0
26
src/config.c
26
src/config.c
|
@ -107,7 +107,6 @@ int config_set(Config * config, char const * section, char const * variable,
|
||||||
{
|
{
|
||||||
Mutator * mutator;
|
Mutator * mutator;
|
||||||
char * p;
|
char * p;
|
||||||
char * oldvalue;
|
|
||||||
char * newvalue = NULL;
|
char * newvalue = NULL;
|
||||||
|
|
||||||
#ifdef DEBUG
|
#ifdef DEBUG
|
||||||
|
@ -131,11 +130,21 @@ int config_set(Config * config, char const * section, char const * variable,
|
||||||
mutator_delete(mutator);
|
mutator_delete(mutator);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
oldvalue = NULL;
|
|
||||||
}
|
}
|
||||||
else
|
/* check if a value was already set for this variable */
|
||||||
/* to free the current value if already set */
|
else if((p = mutator_get(mutator, variable)) != NULL)
|
||||||
oldvalue = mutator_get(mutator, variable);
|
{
|
||||||
|
if(value != NULL && (newvalue = string_new(value)) == NULL)
|
||||||
|
return -1;
|
||||||
|
if(mutator_set(mutator, variable, newvalue) != 0)
|
||||||
|
{
|
||||||
|
string_delete(newvalue);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
/* free the former value */
|
||||||
|
string_delete(p);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
if((p = string_new(variable)) == NULL)
|
if((p = string_new(variable)) == NULL)
|
||||||
return -1;
|
return -1;
|
||||||
if(value != NULL && (newvalue = string_new(value)) == NULL)
|
if(value != NULL && (newvalue = string_new(value)) == NULL)
|
||||||
|
@ -146,14 +155,9 @@ int config_set(Config * config, char const * section, char const * variable,
|
||||||
/* set the new value */
|
/* set the new value */
|
||||||
if(mutator_set(mutator, p, newvalue) != 0)
|
if(mutator_set(mutator, p, newvalue) != 0)
|
||||||
{
|
{
|
||||||
string_delete(p);
|
|
||||||
string_delete(newvalue);
|
string_delete(newvalue);
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
if(oldvalue != NULL)
|
|
||||||
{
|
|
||||||
string_delete(p);
|
string_delete(p);
|
||||||
string_delete(oldvalue);
|
return -1;
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user