]> Cypherpunks repositories - gostls13.git/commitdiff
net/http: panic when a nil handler is passed to (*ServeMux)HandleFunc
authorKunpei Sakai <namusyaka@gmail.com>
Thu, 8 Mar 2018 16:31:50 +0000 (01:31 +0900)
committerBrad Fitzpatrick <bradfitz@golang.org>
Thu, 8 Mar 2018 18:11:41 +0000 (18:11 +0000)
Fixes #24297

Change-Id: I759e88655632fda97dced240b3f13392b2785d0a
Reviewed-on: https://go-review.googlesource.com/99575
Reviewed-by: Andrew Bonventre <andybons@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Run-TryBot: Andrew Bonventre <andybons@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>

src/net/http/serve_test.go
src/net/http/server.go

index 2fa3bc664fabfc44e301eec344af1fa4ebb52a55..be465dd35ef029b6f822c7579e0e9c4a60d24891 100644 (file)
@@ -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
index a7ba753bf50812dc3f7d8417de9ef295751ebffc..c04eb42fef5e35d8c29e7b91df7d53b50702f557 100644 (file)
@@ -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))
 }