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:
Yong Tang
2026-09-03 01:11:33 -07:00
committed by GitHub
parent c942ca7c36
commit c2e309e2e4
2 changed files with 22 additions and 0 deletions

View File

@@ -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")
}
}