Skip to content

Make PackDomainName return error if the root label doesn't fit#1702

Merged
miekg merged 4 commits intomiekg:masterfrom
fcelda:pack-domain-name-buffer-overrun
Jan 22, 2026
Merged

Make PackDomainName return error if the root label doesn't fit#1702
miekg merged 4 commits intomiekg:masterfrom
fcelda:pack-domain-name-buffer-overrun

Conversation

@fcelda
Copy link
Copy Markdown
Contributor

@fcelda fcelda commented Jan 21, 2026

At the moment, if the buffer provided to PackDomainName doesn't fit the trailing root label for the name, the function will skip writing the root label however it will return an adjusted offset and no error.

I believe this is a bug and not intentional behavior because it would be really confusing.

Here is a small reproducer:

wire := make([]byte, 4)
off, err := dns.PackDomainName("foo.", wire, 0, nil, false)

fmt.Printf("off = %d, err = %s\n", off, err) // off = 5, err = <nil>

_ = wire[:off] // panic: runtime error: slice bounds out of range [:5] with capacity 4

I'm providing a fix that will make the function return an error if the buffer doesn't fit the root label. A unit test is included as well.

@fcelda fcelda requested review from miekg and tmthrgd as code owners January 21, 2026 11:34
msg_test.go Outdated
}
}

func TestPackDomainNameBufferSize(t *testing.T) {
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there is no other test where a single extra case can be added?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think so. There are a couple tests in parse_test.go but none that could be just extended with the new case.

If you prefer that, I could move the unit to parse_test.go and/or limit it to checking just this edge case with no space for the root label.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

then just remove it, there are already too many tests that may or may not be actually testing something sane

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK. Removed.

msg.go Outdated
if off < len(msg) {
msg[off] = 0
off += 1
} else {
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why else and not return here

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Question of coding style. I've updated this.

@fcelda fcelda requested a review from miekg January 21, 2026 14:13
@miekg miekg merged commit c376872 into miekg:master Jan 22, 2026
5 checks passed
@fcelda fcelda deleted the pack-domain-name-buffer-overrun branch January 22, 2026 08:38
@fcelda
Copy link
Copy Markdown
Contributor Author

fcelda commented Jan 22, 2026

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants