hashicorp / hashicorp/go-retryablehttp

panic on nil *bytes.Buffer as body

Open
#188 2 comments 0 reactions 0 assignees View on GitHub
Dominant language
Go
Stars
2.3k
Forks
298
PR merge metrics
No merged PRs in 30d

Description

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](https://go.dev/play/p/6AYSNl6ErJB)):

```go
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/go-retryablehttp@v0.7.2/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/go-retryablehttp@v0.7.2/client.go:295 +0x5a
main.main()
/tmp/sandbox1468963018/prog.go:22 +0x7b
```

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.