mirror of
https://github.com/coredns/coredns.git
synced 2026-09-04 02:57:05 -04:00
plugin/cache: Prevents a nil pointer dereference panic in the cache prefetch (#8512)
* plugin/cache: Prevents a nil pointer dereference panic in the cache prefetch This PR prevents a nil pointer dereference panic in the cache prefetch, by adding nil guards Signed-off-by: Yong Tang <yong.tang.github@outlook.com> * Address comment Signed-off-by: Yong Tang <yong.tang.github@outlook.com> --------- Signed-off-by: Yong Tang <yong.tang.github@outlook.com>
This commit is contained in:
4
plugin/cache/cache.go
vendored
4
plugin/cache/cache.go
vendored
@@ -3,6 +3,7 @@ package cache
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
"fmt"
|
||||
"hash/fnv"
|
||||
"net"
|
||||
"strings"
|
||||
@@ -413,6 +414,9 @@ func (w *ResponseWriter) Hijack() {
|
||||
|
||||
// WriteMsg implements the dns.ResponseWriter interface.
|
||||
func (w *ResponseWriter) WriteMsg(res *dns.Msg) error {
|
||||
if res == nil {
|
||||
return fmt.Errorf("cache: response message is nil")
|
||||
}
|
||||
res = res.Copy()
|
||||
w.lastItem = nil
|
||||
mt := cacheResponseType(res, w.now().UTC())
|
||||
|
||||
18
plugin/cache/cache_test.go
vendored
18
plugin/cache/cache_test.go
vendored
@@ -1990,3 +1990,21 @@ func TestServeFromStaleCacheFetchVerifyTimeoutMetadataIsolation(t *testing.T) {
|
||||
t.Fatalf("background verifier mutated foreground metadata: %q", f())
|
||||
}
|
||||
}
|
||||
|
||||
func TestCacheWriteMsgNilResponse(t *testing.T) {
|
||||
c := New()
|
||||
|
||||
rec := dnstest.NewRecorder(&test.ResponseWriter{})
|
||||
cw := &ResponseWriter{ResponseWriter: rec, Cache: c}
|
||||
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
t.Fatalf("ResponseWriter.WriteMsg panicked on nil response: %v", r)
|
||||
}
|
||||
}()
|
||||
|
||||
err := cw.WriteMsg(nil)
|
||||
if err == nil {
|
||||
t.Error("Expected error when passing nil response to WriteMsg, got nil")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user