From: Kunpei Sakai Date: Thu, 8 Mar 2018 16:31:50 +0000 (+0900) Subject: net/http: panic when a nil handler is passed to (*ServeMux)HandleFunc X-Git-Tag: go1.11beta1~1289 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=7d654af5867ed8f89a7a7c88406c4213b2701324;p=gostls13.git net/http: panic when a nil handler is passed to (*ServeMux)HandleFunc Fixes #24297 Change-Id: I759e88655632fda97dced240b3f13392b2785d0a Reviewed-on: https://go-review.googlesource.com/99575 Reviewed-by: Andrew Bonventre Reviewed-by: Brad Fitzpatrick Run-TryBot: Andrew Bonventre TryBot-Result: Gobot Gobot --- diff --git a/src/net/http/serve_test.go b/src/net/http/serve_test.go index 2fa3bc664f..be465dd35e 100644 --- a/src/net/http/serve_test.go +++ b/src/net/http/serve_test.go @@ -379,6 +379,18 @@ func TestServeMuxHandler(t *testing.T) { } } +// Issue 24297 +func TestServeMuxHandleFuncWithNilHandler(t *testing.T) { + setParallel(t) + defer func() { + if err := recover(); err == nil { + t.Error("expected call to mux.HandleFunc to panic") + } + }() + mux := NewServeMux() + mux.HandleFunc("/", nil) +} + var serveMuxTests2 = []struct { method string host string diff --git a/src/net/http/server.go b/src/net/http/server.go index a7ba753bf5..c04eb42fef 100644 --- a/src/net/http/server.go +++ b/src/net/http/server.go @@ -2358,6 +2358,9 @@ func (mux *ServeMux) Handle(pattern string, handler Handler) { // HandleFunc registers the handler function for the given pattern. func (mux *ServeMux) HandleFunc(pattern string, handler func(ResponseWriter, *Request)) { + if handler == nil { + panic("http: nil handler") + } mux.Handle(pattern, HandlerFunc(handler)) }