diff --git a/regexp.go b/regexp.go index e0bcff6a..337ddd0c 100644 --- a/regexp.go +++ b/regexp.go @@ -187,6 +187,9 @@ type routeRegexp struct { // Match matches the regexp against the URL host or path. func (r *routeRegexp) Match(req *http.Request, match *RouteMatch) bool { + if r == nil { + return false + } if r.regexpType == regexpTypeHost { host := getHost(req) if r.wildcardHostPort { diff --git a/regexp_test.go b/regexp_test.go index f7fce81c..955f80ac 100644 --- a/regexp_test.go +++ b/regexp_test.go @@ -1,6 +1,7 @@ package mux import ( + "net/http" "net/url" "reflect" "strconv" @@ -8,6 +9,18 @@ import ( "testing" ) +func Test_routeRegexp_Match_NilReceiver(t *testing.T) { + var r *routeRegexp + req, _ := http.NewRequest("GET", "http://example.com/test", nil) + match := &RouteMatch{} + + // Calling Match on a nil routeRegexp should not panic and return false + result := r.Match(req, match) + if result { + t.Error("Expected Match to return false for nil receiver, got true") + } +} + func Test_newRouteRegexp_Errors(t *testing.T) { tests := []struct { in, out string