settings: Don't overwrite values in-place

This is not thread safe.  If threads are reading from pointers to existing
values they could get a partially updated invalid value.

Refactored assignment to a separate function.
This commit is contained in:
Tobias Brunner
2014-05-15 11:28:08 +02:00
parent 725c479f8b
commit 2fbbea55c5
4 changed files with 52 additions and 36 deletions
+1 -20
View File
@@ -474,26 +474,7 @@ static void set_value(private_settings_t *this, section_t *section,
TRUE);
if (kv)
{
if (!value)
{
if (kv->value)
{
array_insert(this->contents, ARRAY_TAIL, kv->value);
}
kv->value = NULL;
}
else if (kv->value && (strlen(value) <= strlen(kv->value)))
{ /* overwrite in-place, if possible */
strcpy(kv->value, value);
}
else
{ /* otherwise clone the string and cache the replaced one */
if (kv->value)
{
array_insert(this->contents, ARRAY_TAIL, kv->value);
}
kv->value = strdup(value);
}
settings_kv_set(kv, strdupnull(value), this->contents);
}
this->lock->unlock(this->lock);
}