• With issues:
  • Use the search tool before opening a new issue.
  • Please provide source code and commit sha if you found a bug.
  • Review existing issues and provide feedback or react to them.

Description

How to reproduce

Poc is here:

package main

import (
    "fmt"
    "net/http"
    "net/http/httptest"

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

func main() {
    c, _ := gin.CreateTestContext(httptest.NewRecorder())
    c.Request, _ = http.NewRequest("POST", "/", nil)
    c.Request.Header.Add("Accept", "text/html")
    fmt.Println(c.NegotiateFormat("text/htm"))
}

Expectations

It prints "" and exit without error

Actual result

It crashed with:

panic: runtime error: index out of range [8] with length 8

goroutine 1 [running]:
github.com/gin-gonic/gin.(*Context).NegotiateFormat(0xc000261000, {0xc00032ff60, 0x1, 0x6})
        /home/zjx/workspace/gowork/src/go-fdg-exmaples/gin/context.go:1110 +0x186
main.main()
        /home/zjx/workspace/gowork/src/go-fdg-exmaples/gin/pocTestContextNegotiation/main.go:14 +0x1db
exit status 2

Environment

  • go version: go version go1.17.8 linux/amd64
  • gin version (or commit ref): gin with commit c4580944ae8edba96809660ffbfdadc6caaefc2d
  • operating system: ubuntu 18.04 64bit