Skip to content

panic on nil *bytes.Buffer as body #188

Description

@alexdulin

When NewRequestWithContext is given a nil *bytes.Buffer as the body, it panics. Best guess is that it is because the type of the body is being checked, but not that it is non-nil.

Reproduction code (example here):

package main

import (
	"bytes"
	"context"
	"net/http"

	"github.com/hashicorp/go-retryablehttp"
)

type config struct {
	Method string
	URL    string
	Body   *bytes.Buffer
}

func main() {
	cfg := config{
		Method: http.MethodGet,
		URL:    "https://example.com",
	}
	_, _ = retryablehttp.NewRequestWithContext(
		context.Background(),
		cfg.Method,
		cfg.URL,
		cfg.Body,
	)
}

Panic output:

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

goroutine 1 [running]:
bytes.(*Buffer).Len(...)
	/usr/local/go-faketime/src/bytes/buffer.go:73
github.com/hashicorp/go-retryablehttp.getBodyReaderAndContentLength({0x63e680?, 0x0?})
	/tmp/gopath1053138375/pkg/mod/github.com/hashicorp/[email protected]/client.go:228 +0x35c
github.com/hashicorp/go-retryablehttp.NewRequestWithContext({0x6afd98, 0xc0000ac000}, {0x648621, 0x3}, {0x64c514, 0x13}, {0x63e680?, 0x0?})
	/tmp/gopath1053138375/pkg/mod/github.com/hashicorp/[email protected]/client.go:295 +0x5a
main.main()
	/tmp/sandbox1468963018/prog.go:22 +0x7b

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions