Skip to content

net/http: Request.WithContext now panics if URL is nil #20601

@rhysh

Description

@rhysh

What version of Go are you using (go version)?

$ go1.7 version
go version go1.7.6 darwin/amd64
$ go1.8 version
go version go1.8.3 darwin/amd64
$ go-tip version
go version devel +4e7067cde4 Wed Jun 7 02:13:51 2017 +0000 darwin/amd64

What operating system and processor architecture are you using (go env)?

$ go-tip env
GOARCH="amd64"
GOBIN=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/rhys/go"
GORACE=""
GOROOT="/Users/rhys/go/version/go-tip"
GOTOOLDIR="/Users/rhys/go/version/go-tip/pkg/tool/darwin_amd64"
GCCGO="gccgo"
CC="clang"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/49/zmds5zsn75z1283vtzxyfr5hj7yjq4/T/go-build294697682=/tmp/go-build -gno-record-gcc-switches -fno-common"
CXX="clang++"
CGO_ENABLED="1"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"

What did you do?

https://play.golang.org/p/T4d5HhNoPL

$ cat ./withcontext.go
package main

import (
	"context"
	"net/http"
)

func main() {
	r := &http.Request{}
	r.WithContext(context.Background())
}

What did you expect to see?

$ go1.7 run -gcflags="-trimpath=$PWD" ./withcontext.go ; echo $?
0
$ go1.8 run -gcflags="-trimpath=$PWD" ./withcontext.go ; echo $?
0

net/http.Request.WithContext does not document that the URL field must be non-nil, and both Go 1.7 and 1.8 allow WithContext to be called with a nil URL field.

What did you see instead?

$ go-tip run -gcflags="-trimpath=$PWD" ./withcontext.go ; echo $?
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x11be3c5]

goroutine 1 [running]:
net/http.(*Request).WithContext(0xc420053e78, 0x13725c0, 0xc4200140a0, 0x3e7)
	/usr/local/go/src/net/http/request.go:337 +0xc5
main.main()
	withcontext.go:10 +0x7c
exit status 2
1

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions