fix(plugin): prevent panic when ListenHosts is empty#7565
Merged
Conversation
Fix a runtime panic caused by direct indexing into an empty ListenHosts slice. The trace plugin accessed cfg.ListenHosts[0] to build a service endpoint. The loop plugin used conf.ListenHosts[0] to construct a self-query address. When bind resolves no addresses or excludes all configured addresses, ListenHosts can be empty. Both plugins now check length before indexing. Signed-off-by: Ville Vesilehto <ville@vesilehto.fi>
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #7565 +/- ##
==========================================
+ Coverage 55.70% 62.21% +6.51%
==========================================
Files 224 274 +50
Lines 10016 18313 +8297
==========================================
+ Hits 5579 11393 +5814
- Misses 3978 6252 +2274
- Partials 459 668 +209 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
yongtang
approved these changes
Sep 19, 2025
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
1. Why is this pull request needed and what does it do?
Fix a runtime panic caused by direct indexing into an empty
ListenHostsslice. The trace plugin accessedcfg.ListenHosts[0]to build a service endpoint. The loop plugin usedconf.ListenHosts[0]to construct a self-query address. When bind resolves no addresses or excludes all configured addresses,ListenHostscan be empty. Both plugins now check length before indexing.2. Which issues (if any) are related?
OSS-Fuzz finding #42515496
You can run
TestBind_FilterAllagainstmasterbranch and the test will panic on:panic: runtime error: index out of range [0] with length 0 [recovered, repanicked]Details
--- FAIL: TestBind_FilterAll (0.00s) panic: runtime error: index out of range [0] with length 0 [recovered, repanicked]goroutine 99 [running]:
testing.tRunner.func1.2({0x10586dc60, 0x1400061a0a8})
/opt/homebrew/Cellar/go/1.25.1/libexec/src/testing/testing.go:1872 +0x190
testing.tRunner.func1()
/opt/homebrew/Cellar/go/1.25.1/libexec/src/testing/testing.go:1875 +0x31c
panic({0x10586dc60?, 0x1400061a0a8?})
/opt/homebrew/Cellar/go/1.25.1/libexec/src/runtime/panic.go:783 +0x120
github.com/coredns/coredns/plugin/trace.traceParse(0x1400023d440)
/git/coredns/plugin/trace/setup.go:40 +0x8f0
github.com/coredns/coredns/plugin/trace.setup(0x1400023d440)
/git/coredns/plugin/trace/setup.go:17 +0x24
github.com/coredns/caddy.executeDirectives(0x140001e4600, {0x104e2a6e8, 0x8}, {0x107425f80, 0x36, 0x1?}, {0x14000606440, 0x1, 0x10744c840?}, 0x0)
/go/pkg/mod/github.com/coredns/caddy@v1.1.2-0.20241029205200-8de985351a98/caddy.go:663 +0x480
github.com/coredns/caddy.ValidateAndExecuteDirectives({0x105a9a3e0, 0x14000612210}, 0x1400009fd28?, 0x0)
/go/pkg/mod/github.com/coredns/caddy@v1.1.2-0.20241029205200-8de985351a98/caddy.go:614 +0x2cc
github.com/coredns/caddy.startWithListenerFds({0x105a9a3e0, 0x14000612210}, 0x140001e4600, 0x0)
/go/pkg/mod/github.com/coredns/caddy@v1.1.2-0.20241029205200-8de985351a98/caddy.go:517 +0x224
github.com/coredns/caddy.Start({0x105a9a3e0, 0x14000612210})
/go/pkg/mod/github.com/coredns/caddy@v1.1.2-0.20241029205200-8de985351a98/caddy.go:474 +0xbc
github.com/coredns/coredns/test.CoreDNSServer({0x104efe2e5?, 0x10307ad20?})
/git/coredns/test/server.go:21 +0x144
github.com/coredns/coredns/test.TestBind_FilterAll(0x140002ee8c0)
/git/coredns/test/corefile_test.go:36 +0x30
testing.tRunner(0x140002ee8c0, 0x105a6ac38)
/opt/homebrew/Cellar/go/1.25.1/libexec/src/testing/testing.go:1934 +0xc8
created by testing.(*T).Run in goroutine 1
/opt/homebrew/Cellar/go/1.25.1/libexec/src/testing/testing.go:1997 +0x364
exit status 2
FAIL github.com/coredns/coredns/test 0.664s
3. Which documentation changes (if any) need to be made?
None.
4. Does this introduce a backward incompatible change or deprecation?
No, code hardening.