The virNWFilterHashTable struct contains a virHashTable and
then a 'char **names' field which keeps a copy of all the
hash keys. Presumably this was intended to record the ordering
of the hash keys. No code ever uses this and the ordering is
mangled whenever a variable is removed from the hash, because
the last element in the list is copied into the middle of the
list when shrinking the arra.
Signed-off-by: Daniel P. Berrange <berrange(a)redhat.com>
---
src/conf/nwfilter_ipaddrmap.c | 2 +-
src/conf/nwfilter_params.c | 48 +++++-----------------------------
src/conf/nwfilter_params.h | 6 +----
src/nwfilter/nwfilter_gentech_driver.c | 2 +-
4 files changed, 9 insertions(+), 49 deletions(-)
diff --git a/src/conf/nwfilter_ipaddrmap.c b/src/conf/nwfilter_ipaddrmap.c
index 4bb6775..446f3de 100644
--- a/src/conf/nwfilter_ipaddrmap.c
+++ b/src/conf/nwfilter_ipaddrmap.c
@@ -60,7 +60,7 @@ virNWFilterIPAddrMapAddIPAddr(const char *ifname, char *addr)
val = virNWFilterVarValueCreateSimple(addr);
if (!val)
goto cleanup;
- ret = virNWFilterHashTablePut(ipAddressMap, ifname, val, 1);
+ ret = virNWFilterHashTablePut(ipAddressMap, ifname, val);
goto cleanup;
} else {
if (virNWFilterVarValueAddValue(val, addr) < 0)
diff --git a/src/conf/nwfilter_params.c b/src/conf/nwfilter_params.c
index 3e85bc1..7655033 100644
--- a/src/conf/nwfilter_params.c
+++ b/src/conf/nwfilter_params.c
@@ -631,33 +631,14 @@ hashDataFree(void *payload, const void *name ATTRIBUTE_UNUSED)
int
virNWFilterHashTablePut(virNWFilterHashTablePtr table,
const char *name,
- virNWFilterVarValuePtr val,
- int copyName)
+ virNWFilterVarValuePtr val)
{
if (!virHashLookup(table->hashTable, name)) {
- char *newName;
- if (copyName) {
- if (VIR_STRDUP(newName, name) < 0)
- return -1;
-
- if (VIR_APPEND_ELEMENT_COPY(table->names,
- table->nNames, newName) < 0) {
- VIR_FREE(newName);
- return -1;
- }
- }
-
- if (virHashAddEntry(table->hashTable, name, val) < 0) {
- if (copyName) {
- VIR_FREE(newName);
- table->nNames--;
- }
+ if (virHashAddEntry(table->hashTable, name, val) < 0)
return -1;
- }
} else {
- if (virHashUpdateEntry(table->hashTable, name, val) < 0) {
+ if (virHashUpdateEntry(table->hashTable, name, val) < 0)
return -1;
- }
}
return 0;
}
@@ -675,14 +656,10 @@ virNWFilterHashTablePut(virNWFilterHashTablePtr table,
void
virNWFilterHashTableFree(virNWFilterHashTablePtr table)
{
- size_t i;
if (!table)
return;
virHashFree(table->hashTable);
- for (i = 0; i < table->nNames; i++)
- VIR_FREE(table->names[i]);
- VIR_FREE(table->names);
VIR_FREE(table);
}
@@ -707,20 +684,7 @@ void *
virNWFilterHashTableRemoveEntry(virNWFilterHashTablePtr ht,
const char *entry)
{
- size_t i;
- void *value = virHashSteal(ht->hashTable, entry);
-
- if (value) {
- for (i = 0; i < ht->nNames; i++) {
- if (STREQ(ht->names[i], entry)) {
- VIR_FREE(ht->names[i]);
- ht->names[i] = ht->names[--ht->nNames];
- ht->names[ht->nNames] = NULL;
- break;
- }
- }
- }
- return value;
+ return virHashSteal(ht->hashTable, entry);
}
@@ -745,7 +709,7 @@ addToTable(void *payload, const void *name, void *data)
return;
}
- if (virNWFilterHashTablePut(atts->target, (const char *)name, val, 1) < 0){
+ if (virNWFilterHashTablePut(atts->target, (const char *)name, val) < 0){
virReportError(VIR_ERR_INTERNAL_ERROR,
_("Could not put variable '%s' into hashmap"),
(const char *)name);
@@ -850,7 +814,7 @@ virNWFilterParseParamAttributes(xmlNodePtr cur)
value = virNWFilterParseVarValue(val);
if (!value)
goto skip_entry;
- if (virNWFilterHashTablePut(table, nam, value, 1) < 0)
+ if (virNWFilterHashTablePut(table, nam, value) < 0)
goto err_exit;
}
value = NULL;
diff --git a/src/conf/nwfilter_params.h b/src/conf/nwfilter_params.h
index 5e9777b..f9efc42 100644
--- a/src/conf/nwfilter_params.h
+++ b/src/conf/nwfilter_params.h
@@ -66,9 +66,6 @@ typedef struct _virNWFilterHashTable virNWFilterHashTable;
typedef virNWFilterHashTable *virNWFilterHashTablePtr;
struct _virNWFilterHashTable {
virHashTablePtr hashTable;
-
- size_t nNames;
- char **names;
};
@@ -81,8 +78,7 @@ virNWFilterHashTablePtr virNWFilterHashTableCreate(int n);
void virNWFilterHashTableFree(virNWFilterHashTablePtr table);
int virNWFilterHashTablePut(virNWFilterHashTablePtr table,
const char *name,
- virNWFilterVarValuePtr val,
- int freeName);
+ virNWFilterVarValuePtr val);
void *virNWFilterHashTableRemoveEntry(virNWFilterHashTablePtr table,
const char *name);
int virNWFilterHashTablePutAll(virNWFilterHashTablePtr src,
diff --git a/src/nwfilter/nwfilter_gentech_driver.c
b/src/nwfilter/nwfilter_gentech_driver.c
index 82ff628..d482f43 100644
--- a/src/nwfilter/nwfilter_gentech_driver.c
+++ b/src/nwfilter/nwfilter_gentech_driver.c
@@ -512,7 +512,7 @@ virNWFilterDetermineMissingVarsRec(virNWFilterDefPtr filter,
varAccess = virBufferContentAndReset(&buf);
virNWFilterHashTablePut(missing_vars, varAccess,
- val, 1);
+ val);
VIR_FREE(varAccess);
}
}
--
1.9.0