dns: cleanup handling of shadowed priorities rebuild_domain_lists()

domain_is_shadowed() only works, because we pre-sort all items. When
we call domain_is_shadowed(), then "priority" must be not smaller than
any priority already in the dictionary.

Let's add an nm_assert() for that.

While at it, I also found it ugly to rely on

  GPOINTER_TO_INT(g_hash_table_lookup(ht, domain))

returning zero to know whether the domain is tracked. While more
cumbersome, we should check whether the value is in the hash (and not).
Not whether the value does not translate to zero.

Add domain_ht_get_priority() for that.

(cherry picked from commit 5902f1c91f)
This commit is contained in:
Thomas Haller 2020-11-19 16:23:15 +01:00
parent 59d48fcc35
commit 2502b88511
No known key found for this signature in database
GPG key ID: 29C2366E4DFC5728

View file

@ -1286,6 +1286,19 @@ get_ip_rdns_domains(NMIPConfig *ip_config)
return _nm_utils_strv_cleanup(strv, FALSE, FALSE, TRUE);
}
static gboolean
domain_ht_get_priority(GHashTable *ht, const char *domain, int *out_priority)
{
gpointer ptr;
if (!ht || !g_hash_table_lookup_extended(ht, domain, NULL, &ptr)) {
*out_priority = 0;
return FALSE;
}
*out_priority = GPOINTER_TO_INT(ptr);
return TRUE;
}
/* Check if the domain is shadowed by a parent domain with more negative priority */
static gboolean
domain_is_shadowed(GHashTable * ht,
@ -1302,21 +1315,25 @@ domain_is_shadowed(GHashTable * ht,
nm_assert(!g_hash_table_contains(ht, domain));
parent_priority = GPOINTER_TO_INT(g_hash_table_lookup(ht, ""));
if (parent_priority < 0 && parent_priority < priority) {
*out_parent = "";
*out_parent_priority = parent_priority;
return TRUE;
if (domain_ht_get_priority(ht, "", &parent_priority)) {
nm_assert(parent_priority <= priority);
if (parent_priority < 0 && parent_priority < priority) {
*out_parent = "";
*out_parent_priority = parent_priority;
return TRUE;
}
}
parent = strchr(domain, '.');
while (parent && parent[1]) {
parent++;
parent_priority = GPOINTER_TO_INT(g_hash_table_lookup(ht, parent));
if (parent_priority < 0 && parent_priority < priority) {
*out_parent = parent;
*out_parent_priority = parent_priority;
return TRUE;
if (domain_ht_get_priority(ht, parent, &parent_priority)) {
nm_assert(parent_priority <= priority);
if (parent_priority < 0 && parent_priority < priority) {
*out_parent = parent;
*out_parent_priority = parent_priority;
return TRUE;
}
}
parent = strchr(parent, '.');
}
@ -1427,8 +1444,8 @@ rebuild_domain_lists(NMDnsManager *self)
domain_clean = nm_utils_parse_dns_domain(domains[i], NULL);
/* Remove domains with lower priority */
old_priority = GPOINTER_TO_INT(nm_g_hash_table_lookup(ht, domain_clean));
if (old_priority != 0) {
if (domain_ht_get_priority(ht, domain_clean, &old_priority)) {
nm_assert(old_priority <= priority);
if (old_priority < priority) {
_LOGT(
"plugin: drop domain '%s' (i=%d, p=%d) because it already exists with p=%d",