Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Panic in 'ExtractIntoStructPtr' #2872

Open
bobuhiro11 opened this issue Jan 19, 2024 · 6 comments · May be fixed by #2873
Open

Panic in 'ExtractIntoStructPtr' #2872

bobuhiro11 opened this issue Jan 19, 2024 · 6 comments · May be fixed by #2873
Assignees

Comments

@bobuhiro11
Copy link

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
Copy link

Thank you for reporting your first issue! Be sure that we will be looking at it, but keep in mind
this sometimes takes a while.
Please let the maintainers know if your issue has not got enough attention after a few days.
If any doubt, please consult our issue tutorial.

@bobuhiro11 bobuhiro11 linked a pull request Jan 19, 2024 that will close this issue
@pierreprinetti pierreprinetti self-assigned this Jan 19, 2024
@pierreprinetti
Copy link
Contributor

Thank you for this high-quality report featuring a minimal reproducer!

@pierreprinetti
Copy link
Contributor

Actually, your reproducer is misleading.

The first line (where you pass a pointer to a pointer to TestPerson (**TestPerson) correctly errors.

For reference: https://go.dev/play/p/XN6nHpe52yp

Could you find a case where the panic happens, in which you're not passing the literal nil?

@bobuhiro11
Copy link
Author

Could you find a case where the panic happens, in which you're not passing the literal nil?

Sorry, you are right, it seems to panic only when literal nil is passed as an argument.

@pierreprinetti
Copy link
Contributor

@bobuhiro11
By any chance: have you hit a panic in some real-world application of this code?

@bobuhiro11
Copy link
Author

@pierreprinetti
I noticed this during development and decided to validate it on the application side.
I don't have a problem with it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants