Skip to content

Panic in 'ExtractIntoStructPtr' #2872

@bobuhiro11

Description

@bobuhiro11

Accidentally passing nil to 'ExtractIntoStructPtr' function would cause panic.
You can reproduce this by adding the following code to testing/results_test.go .

func TestUnmarshalNilPointer(t *testing.T) {
        var x *TestPerson

        err1 := gophercloud.Result{}.ExtractIntoStructPtr(&x, "")
        err2 := gophercloud.Result{}.ExtractIntoStructPtr(nil, "")

        th.AssertErr(t, err1)
        th.AssertErr(t, err2)
}

Outputs are as follows:

ubuntu2204:~/gophercloud$ go test ./testing/ -run TestUnmarshalNilPointer -v
=== RUN   TestUnmarshalNilPointer
--- FAIL: TestUnmarshalNilPointer (0.00s)
panic: runtime error: invalid memory address or nil pointer dereference [recovered]
        panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x98 pc=0x6d12d2]

goroutine 6 [running]:
testing.tRunner.func1.2({0x71d100, 0x9daca0})
        /usr/lib/go-1.20/src/testing/testing.go:1526 +0x24e
testing.tRunner.func1()
        /usr/lib/go-1.20/src/testing/testing.go:1529 +0x39f
panic({0x71d100, 0x9daca0})
        /usr/lib/go-1.20/src/runtime/panic.go:884 +0x213
github.com/gophercloud/gophercloud.Result.ExtractIntoStructPtr({{0x0, 0x0}, 0x0, 0x0, {0x0, 0x0}}, {0x0, 0x0}, {0x0, 0x0})
        /home/bobuhiro11/gophercloud/results.go:188 +0xb2
github.com/gophercloud/gophercloud/testing.TestUnmarshalNilPointer(0x0?)
        /home/bobuhiro11/gophercloud/testing/results_test.go:120 +0x6e
testing.tRunner(0xc00013e4e0, 0x7a8b98)
        /usr/lib/go-1.20/src/testing/testing.go:1576 +0x10b
created by testing.(*T).Run
        /usr/lib/go-1.20/src/testing/testing.go:1629 +0x3ea
FAIL    github.com/gophercloud/gophercloud/testing      0.006s
FAIL

Metadata

Metadata

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