Skip to content

runtime error: makeslice: cap out of range when HandleMethodNotAllowed=true and no request handler registered #4002

@phsym

Description

@phsym

Description

Gin server panics with runtime error: makeslice: cap out of range when HandleMethodNotAllowed is set to true and no handler are registered.

How to reproduce

package main

import (
	"net/http/httptest"

	"github.com/gin-gonic/gin"
)

func main() {
	g := gin.New()
	g.HandleMethodNotAllowed = true

	req := httptest.NewRequest("GET", "/", nil)
	resp := httptest.NewRecorder()
	g.ServeHTTP(resp, req) // <-- Will panic
}

Expectations

No crash. I would have instead expected an HTTP error response like a 404

Actual result

panic: runtime error: makeslice: cap out of range

goroutine 1 [running]:
github.com/gin-gonic/gin.(*Engine).handleHTTPRequest(0xc00009bba0, 0xc0001ab900)
	/go/pkg/mod/github.com/gin-gonic/[email protected]/gin.go:652 +0x43f
github.com/gin-gonic/gin.(*Engine).ServeHTTP(0xc00009bba0, {0x10de2878, 0xc000406780}, 0xc0003fa5a0)
	/go/pkg/mod/github.com/gin-gonic/[email protected]/gin.go:589 +0x1b2
main.main()
	main.go:15 +0xc9

Environment

  • go version: 1.22.4
  • gin version (or commit ref): 1.10.0
  • operating system: MacOS

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions