Skip to content

Commit

Permalink
resolved: when adding names to packet fails, remove them from label c…
Browse files Browse the repository at this point in the history
…ompression hash table again

let's make sure we undo any pollution of the label compression hash
table.

Fixes: systemd#33671
(cherry picked from commit 360105f)
  • Loading branch information
poettering authored and bluca committed Nov 13, 2024
1 parent 88c9d7d commit e5d67c8
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions src/resolve/resolved-dns-packet.c
Original file line number Diff line number Diff line change
Expand Up @@ -557,7 +557,8 @@ int dns_packet_append_name(
bool canonical_candidate,
size_t *start) {

size_t saved_size;
_cleanup_free_ char **added_entries = NULL; /* doesn't own the strings! this is just regular pointer array, not a NULL-terminated strv! */
size_t n_added_entries = 0, saved_size;
int r;

assert(p);
Expand Down Expand Up @@ -598,6 +599,11 @@ int dns_packet_append_name(
if (allow_compression) {
_cleanup_free_ char *s = NULL;

if (!GREEDY_REALLOC(added_entries, n_added_entries + 1)) {
r = -ENOMEM;
goto fail;
}

s = strdup(z);
if (!s) {
r = -ENOMEM;
Expand All @@ -608,7 +614,8 @@ int dns_packet_append_name(
if (r < 0)
goto fail;

TAKE_PTR(s);
/* Keep track of the entries we just added (note that the string is owned by the hashtable, not this array!) */
added_entries[n_added_entries++] = TAKE_PTR(s);
}
}

Expand All @@ -623,6 +630,12 @@ int dns_packet_append_name(
return 0;

fail:
/* Remove all label compression names we added again */
FOREACH_ARRAY(s, added_entries, n_added_entries) {
hashmap_remove(p->names, *s);
free(*s);
}

dns_packet_truncate(p, saved_size);
return r;
}
Expand Down

0 comments on commit e5d67c8

Please sign in to comment.