【遇到一个神奇的问题】暂未想到原因,http.Post 传入 nil参数正确,但是传输值为 nil 的 *bytes.BytesReader 就 `invalid memory address or nil pointer dereference`

发布时间 2023-07-24 11:37:23作者: ahfuzhang

出错的代码如下:

func getEab(ctx context.Context, credentialsJSON string, old *externalAccountKeyResp) (*externalAccountKeyResp, error) {
    // init http client
    //
	var postData *bytes.Reader = nil
	if old != nil {
		buf, _ := json.Marshal(old)
		postData = bytes.NewReader(buf)
	}
	var api = fmt.Sprintf("https://publicca.googleapis.com/v1beta1/projects/%s/locations/global/externalAccountKeys", "xxxx")
	resp, err := conf.Client(context.Background()).Post(api, "application/json", postData)

最后一行代码出现:

panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x2 addr=0x18 pc=0x1024b56d0]

goroutine 36 [running]:
bytes.(*Reader).Len(...)
        /opt/homebrew/Cellar/go/1.20.4/libexec/src/bytes/reader.go:27
net/http.NewRequestWithContext({0x102f2d2c8?, 0x140000340b8}, {0x102a565e4?, 0x1035bef20?}, {0x140005b8120?, 0x5e?}, {0x102f24be0, 0x0?})
        /opt/homebrew/Cellar/go/1.20.4/libexec/src/net/http/request.go:909 +0x3e0
net/http.NewRequest(...)
        /opt/homebrew/Cellar/go/1.20.4/libexec/src/net/http/request.go:840
net/http.(*Client).Post(0x102a8fcf2?, {0x140005b8120?, 0x140000340b8?}, {0x102a5fc1e, 0x10}, {0x102f24be0?, 0x0?})
        /opt/homebrew/Cellar/go/1.20.4/libexec/src/net/http/client.go:844 +0x6c
github.com/myklst/terraform-provider-st-gcp/gcp.gcpGetEab({0x102f2d338?, 0x140005a2cf0?}, {0x140005ef500, 0x920}, 0x0)
        /Users/fuchunzhang/code/github.com/ahfuzhang/terraform-provider-st-gcp/gcp/resource_eab.go:223 +0x348
github.com/myklst/terraform-provider-st-gcp/gcp.(*gcpAcmeEabResource).Create(0x0?, {0x102f2d338, 0x140005a2cf0}, {{{{0x102f31510, 0x140005a3c50}, {0x102d37d40, 0x140005a37d0}}, {0x102f32a00, 0x14000622410}}, {{{0x102f31510, ...}, ...}, ...}, ...}, ...)
        /Users/fuchunzhang/code/github.com/ahfuzhang/terraform-provider-st-gcp/gcp/resource_eab.go:83 +0x15c
github.com/hashicorp/terraform-plugin-framework/internal/fwserver.(*Server).CreateResource(0x14000349b80, {0x102f2d338, 0x140005a2cf0}, 0x14000545380, 0x14000545320)
        /Users/fuchunzhang/go/pkg/mod/github.com/hashicorp/terraform-plugin-framework@v1.1.1/internal/fwserver/server_createresource.go:97 +0x428
github.com/hashicorp/terraform-plugin-framework/internal/fwserver.(*Server).ApplyResourceChange(0x140005454e0?, {0x102f2d338, 0x140005a2cf0}, 0x140005aa410, 0x140005454e0)
        /Users/fuchunzhang/go/pkg/mod/github.com/hashicorp/terraform-plugin-framework@v1.1.1/internal/fwserver/server_applyresourcechange.go:54 +0x370
github.com/hashicorp/terraform-plugin-framework/internal/proto6server.(*Server).ApplyResourceChange(0x14000349b80, {0x102f2d338?, 0x140005a2ba0?}, 0x140005aa370)
        /Users/fuchunzhang/go/pkg/mod/github.com/hashicorp/terraform-plugin-framework@v1.1.1/internal/proto6server/server_applyresourcechange.go:52 +0x314
github.com/hashicorp/terraform-plugin-go/tfprotov6/tf6server.(*server).ApplyResourceChange(0x140001b4280, {0x102f2d338?, 0x140005a21b0?}, 0x14000264070)
        /Users/fuchunzhang/go/pkg/mod/github.com/hashicorp/terraform-plugin-go@v0.14.3/tfprotov6/tf6server/server.go:816 +0x3bc
github.com/hashicorp/terraform-plugin-go/tfprotov6/internal/tfplugin6._Provider_ApplyResourceChange_Handler({0x102edc4c0?, 0x140001b4280}, {0x102f2d338, 0x140005a21b0}, 0x14000264000, 0x0)
        /Users/fuchunzhang/go/pkg/mod/github.com/hashicorp/terraform-plugin-go@v0.14.3/tfprotov6/internal/tfplugin6/tfplugin6_grpc.pb.go:385 +0x170
google.golang.org/grpc.(*Server).processUnaryRPC(0x1400014e000, {0x102f317f8, 0x14000582b60}, 0x140005b4000, 0x1400010f080, 0x1035b16c0, 0x0)
        /Users/fuchunzhang/go/pkg/mod/google.golang.org/grpc@v1.55.0/server.go:1337 +0xc90
google.golang.org/grpc.(*Server).handleStream(0x1400014e000, {0x102f317f8, 0x14000582b60}, 0x140005b4000, 0x0)
        /Users/fuchunzhang/go/pkg/mod/google.golang.org/grpc@v1.55.0/server.go:1714 +0x82c
google.golang.org/grpc.(*Server).serveStreams.func1.1()
        /Users/fuchunzhang/go/pkg/mod/google.golang.org/grpc@v1.55.0/server.go:959 +0x84
created by google.golang.org/grpc.(*Server).serveStreams.func1
        /Users/fuchunzhang/go/pkg/mod/google.golang.org/grpc@v1.55.0/server.go:957 +0x16c

换成如下的写法后,问题解决:

	if old != nil {
		buf, _ := json.Marshal(old)
		resp, err = conf.Client(context.Background()).Post(api, "application/json", bytes.NewReader(buf))
	} else {
		resp, err = conf.Client(context.Background()).Post(api, "application/json", nil)
	}

暂未想到原因。