Skip to content

Commit e5d67c8

Browse files
poetteringbluca
authored andcommitted
resolved: when adding names to packet fails, remove them from label compression 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)
1 parent 88c9d7d commit e5d67c8

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

src/resolve/resolved-dns-packet.c

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -557,7 +557,8 @@ int dns_packet_append_name(
557557
bool canonical_candidate,
558558
size_t *start) {
559559

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

563564
assert(p);
@@ -598,6 +599,11 @@ int dns_packet_append_name(
598599
if (allow_compression) {
599600
_cleanup_free_ char *s = NULL;
600601

602+
if (!GREEDY_REALLOC(added_entries, n_added_entries + 1)) {
603+
r = -ENOMEM;
604+
goto fail;
605+
}
606+
601607
s = strdup(z);
602608
if (!s) {
603609
r = -ENOMEM;
@@ -608,7 +614,8 @@ int dns_packet_append_name(
608614
if (r < 0)
609615
goto fail;
610616

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

@@ -623,6 +630,12 @@ int dns_packet_append_name(
623630
return 0;
624631

625632
fail:
633+
/* Remove all label compression names we added again */
634+
FOREACH_ARRAY(s, added_entries, n_added_entries) {
635+
hashmap_remove(p->names, *s);
636+
free(*s);
637+
}
638+
626639
dns_packet_truncate(p, saved_size);
627640
return r;
628641
}

0 commit comments

Comments
 (0)