Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

net/http: invalid memory address or nil pointer dereference when body is a *Buffer nil #44327

Closed
omarsy opened this issue Feb 17, 2021 · 6 comments
Closed

Comments

@omarsy
Copy link

@omarsy omarsy commented Feb 17, 2021

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

$ go version
go version go1.15 windows/amd64

Does this issue reproduce with the latest release?

Yes

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

go env Output
$ go env
set GO111MODULE=
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\SESA571965\AppData\Local\go-build
set GOENV=C:\Users\SESA571965\AppData\Roaming\go\env
set GOEXE=.exe
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOINSECURE=
set GOMODCACHE=C:\Users\SESA571965\go\pkg\mod
set GONOPROXY=
set GONOSUMDB=
set GOOS=windows
set GOPATH=C:\Users\SESA571965\go
set GOPRIVATE=
set GOPROXY=https://proxy.golang.org,direct
set GOROOT=c:\go
set GOSUMDB=sum.golang.org
set GOTMPDIR=
set GOTOOLDIR=c:\go\pkg\tool\windows_amd64
set GCCGO=gccgo
set AR=ar
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
set GOMOD=C:\Users\SESA571965\Documents\EdgeCore.omar\go.mod
set CGO_CFLAGS=-g -O2
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-g -O2
set CGO_FFLAGS=-g -O2
set CGO_LDFLAGS=-g -O2
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -mthreads -fmessage-length=0 -fdebug-prefix-map=C:\Users\SESA57~1\AppData\Local\Temp\go-build806356266=/tmp/go-build -gno-record-gcc-switches

What did you do?

Code to reproduce it

package main

import (
	"bytes"
	"fmt"
	"net/http"
)

func main() {
	var body *bytes.Buffer
	_, _ = http.NewRequest("GET", "https://google.fr/", body)
	fmt.Println("Hello, playground")
}

What did you expect to see?

I want to have the same behaviour when I run this code
package main

import (
	"fmt"
	"net/http"
)

func main() {
	_, _ = http.NewRequest("GET", "https://google.fr/", nil)
	fmt.Println("Hello, playground")
}

What did you see instead?

panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x8 pc=0x546824]

goroutine 1 [running]:
bytes.(*Buffer).Len(...)
	/usr/local/go-faketime/src/bytes/buffer.go:73
net/http.NewRequestWithContext(0x5cc7a0, 0xc0000b8020, 0x59583d, 0x3, 0x598c4f, 0x12, 0x5ca900, 0x0, 0x0, 0x0, ...)
	/usr/local/go-faketime/src/net/http/request.go:875 +0x524
net/http.NewRequest(...)
	/usr/local/go-faketime/src/net/http/request.go:813
main.main()
	/tmp/sandbox461295219/prog.go:11 +0x7c

@mdlayher
Copy link
Member

@mdlayher mdlayher commented Feb 17, 2021

I don't think there's anything to be done here. A nil *bytes.Buffer and untyped nil are not the same and the example you provide that panics seems to indicate a clear error in your user code, not in net/http.

@omarsy
Copy link
Author

@omarsy omarsy commented Feb 17, 2021

Maybe I was wrong about my understanding. But I think a function that provides a pointer as a parameter should handle the case that this pointer should be null.
I know that the two pointers are different but in my understanding I expect the same behaviour.

@omarsy
Copy link
Author

@omarsy omarsy commented Feb 17, 2021

Because of this behaviour I am forced to do something like this.
image

@bcmills
Copy link
Member

@bcmills bcmills commented Feb 17, 2021

This is https://golang.org/doc/faq#nil_error.

The fix is to not pass a nil *bytes.Buffer to http.NewRequest, because a nil *bytes.Buffer is a non-nil io.Reader.

@bcmills bcmills closed this Feb 17, 2021
@bcmills
Copy link
Member

@bcmills bcmills commented Feb 17, 2021

See also #22729.

@omarsy
Copy link
Author

@omarsy omarsy commented Feb 17, 2021

ok thank you @bcmills and @mdlayher . But it is strange for me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants