Skip to content

net/http: NewRequest/NewRequestWithContext fails with (*bytes.Buffer)(nil) #68252

Closed as not planned
@isnastish

Description

@isnastish

Go version

go version go1.22.3 windows/amd64

Output of go env in your module/workspace:

set GO111MODULE=
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\AYevtushenko\AppData\Local\go-build
set GOENV=C:\Users\AYevtushenko\AppData\Roaming\go\env
set GOEXE=.exe
set GOEXPERIMENT=
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOINSECURE=
set GOMODCACHE=C:\Users\AYevtushenko\go\pkg\mod
set GONOPROXY=
set GONOSUMDB=
set GOOS=windows
set GOPATH=C:\Users\AYevtushenko\go
set GOPRIVATE=
set GOPROXY=https://proxy.golang.org,direct
set GOROOT=C:\Users\AYevtushenko\go\pkg\mod\golang.org\toolchain@v0.0.1-go1.22.3.windows-amd64
set GOSUMDB=sum.golang.org
set GOTMPDIR=
set GOTOOLCHAIN=auto
set GOTOOLDIR=C:\Users\AYevtushenko\go\pkg\mod\golang.org\toolchain@v0.0.1-go1.22.3.windows-amd64\pkg\tool\windows_amd64
set GOVCS=
set GOVERSION=go1.22.3
set GCCGO=gccgo
set GOAMD64=v1
set AR=ar
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
set GOMOD=C:\work\go\kvs\go.mod
set GOWORK=
set CGO_CFLAGS=-O2 -g
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-O2 -g
set CGO_FFLAGS=-O2 -g
set CGO_LDFLAGS=-O2 -g
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -mthreads -fno-caret-diagnostics -Qunused-arguments -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=C:\WINDOWS\TEMP\go-build2583212312=/tmp/go-build -gno-record-gcc-switches

What did you do?

I made a small wrapper function for performing http requests, which I needed exclusively for testing. The function created an http.Client instance inside its body and invoked http.NewRequest() procedure. Here is a sample code:

func performHttpRequest(method, endpoint string body *bytes.Buffer) error {
    	httpClient := http.Client{}
	req, err = http.NewRequest(method, endpoint, body)
	resp, err := httpClient.Do(req)
        if err != nil { return err }
	resp.Body.Close()
}

// And then somewhere in the code

func foo() {
   // first usage example (the one which is crashing)
   if err := performHttpRequest(http.MethodGet, "some_endpoint", nil); err != nil {
         // process error
   }
   
   // second usage example which works correctly by passing a non-nil body
   body := bytes.NewBufferString("body")
   if err := performHttpRequest(http.MethodGet, "some_endpoint", body); err != nil {
        // process error 
   }
}

What did you see happen?

When calling performHttpRequest() procedure with a nil value provided for the body, the http.NewRequestWithContext() function terminates abnormally because it passes a nil-check for the body argument. Whereas calling http.NewReques(method, endpoint, nil) with a nil value directly for the body works as expected.
Here is an extract from http.NewRequestWithContext() with my comments where exactly the failure happens.

func NewRequestWithContext(ctx context.Context, method, url string, body io.Read) (*Request, error) {
    // ...
    // We pass a nil check for the body despite the fact, 
    // that nil was passed as an argument to `performHttpRequest()` procedure 
    // and then forwarded to `http.NewRequestWithContext()` properly
    if body != nil {
          // This is the point of failure,
          // when we're trying to determine type of the body, the value of which is nil
          // So, in this case the nil check above SHOULDN'T pass in order to get the correct behaviour
          switch v := body.(type) {
              // ...
          }
    }
}

What did you expect to see?

I expect the function http.NewRequestWithContext() do the proper nil-check for the body and not to fail, if the nil is passed not directly to http.NewRequest/NewRequestWithContext, but through the intermediary procedure, like performHttpRequest() as in the example provided above.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions