haproxy/src/dict.c
Frédéric Lécaille b5ecf0393c BUG/MINOR: dict: race condition fix when inserting dictionary entries.
When checking the result of an ebis_insert() call in an ebtree with unique keys,
if already present, in place of freeing() the old one and return the new one,
rather the correct way is to free the new one, and return the old one. For
this, the __dict_insert() function was folded into dict_insert() as this
significantly simplifies the test of duplicates.

Thanks to Olivier for having reported this bug which came with this one:
"MINOR: dict: Add dictionary new data structure".
2019-06-11 09:54:12 +02:00

108 lines
1.9 KiB
C

#include <string.h>
#include <proto/dict.h>
#include <eb32tree.h>
#include <ebistree.h>
struct dict *new_dict(const char *name)
{
struct dict *dict;
dict = malloc(sizeof *dict);
if (!dict)
return NULL;
dict->name = name;
dict->values = EB_ROOT_UNIQUE;
HA_RWLOCK_INIT(&dict->rwlock);
return dict;
}
/*
* Allocate a new dictionary entry with <s> as string value which is strdup()'ed.
* Returns the new allocated entry if succeeded, NULL if not.
*/
static struct dict_entry *new_dict_entry(char *s)
{
struct dict_entry *de;
de = calloc(1, sizeof *de);
if (!de)
return NULL;
de->value.key = strdup(s);
if (!de->value.key)
goto err;
de->len = strlen(s);
de->refcount = 1;
return de;
err:
free(de->value.key);
de->value.key = NULL;
de->len = 0;
free(de);
return NULL;
}
/*
* Release the memory allocated for <de> dictionary entry.
*/
static void free_dict_entry(struct dict_entry *de)
{
de->refcount = 0;
free(de->value.key);
de->value.key = NULL;
free(de);
}
/*
* Simple function to lookup dictionary entries with <s> as value.
*/
static struct dict_entry *__dict_lookup(struct dict *d, const char *s)
{
struct dict_entry *de;
struct ebpt_node *node;
de = NULL;
node = ebis_lookup(&d->values, s);
if (node)
de = container_of(node, struct dict_entry, value);
return de;
}
/*
* Insert an entry in <d> dictionary with <s> as value. *
*/
struct dict_entry *dict_insert(struct dict *d, char *s)
{
struct dict_entry *de;
struct ebpt_node *n;
HA_RWLOCK_RDLOCK(DICT_LOCK, &d->rwlock);
de = __dict_lookup(d, s);
HA_RWLOCK_RDUNLOCK(DICT_LOCK, &d->rwlock);
if (de)
return de;
de = new_dict_entry(s);
if (!de)
return NULL;
HA_RWLOCK_WRLOCK(DICT_LOCK, &d->rwlock);
n = ebis_insert(&d->values, &de->value);
HA_RWLOCK_WRUNLOCK(DICT_LOCK, &d->rwlock);
if (n != &de->value) {
free_dict_entry(de);
de = container_of(n, struct dict_entry, value);
}
return de;
}