Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[v2] Fix panic in ExtractIntoStructPtr #3287

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

Merged
merged 1 commit into from
Feb 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions 18 results.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,10 +184,19 @@ func (r Result) ExtractIntoStructPtr(to any, label string) error {
return r.Err
}

if to == nil {
return fmt.Errorf("Expected pointer, got %T", to)
}

t := reflect.TypeOf(to)
if k := t.Kind(); k != reflect.Ptr {
return fmt.Errorf("Expected pointer, got %v", k)
}

if reflect.ValueOf(to).IsNil() {
return fmt.Errorf("Expected pointer, got %T", to)
}

switch t.Elem().Kind() {
case reflect.Struct:
return r.extractIntoPtr(to, label)
Expand All @@ -210,10 +219,19 @@ func (r Result) ExtractIntoSlicePtr(to any, label string) error {
return r.Err
}

if to == nil {
return fmt.Errorf("Expected pointer, got %T", to)
}

t := reflect.TypeOf(to)
if k := t.Kind(); k != reflect.Ptr {
return fmt.Errorf("Expected pointer, got %v", k)
}

if reflect.ValueOf(to).IsNil() {
return fmt.Errorf("Expected pointer, got %T", to)
}

switch t.Elem().Kind() {
case reflect.Slice:
return r.extractIntoPtr(to, label)
Expand Down
34 changes: 34 additions & 0 deletions 34 testing/results_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,40 @@ func TestUnmarshalAnonymousStructs(t *testing.T) {
th.AssertEquals(t, "Canada unmarshalled", actual.Location)
}

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

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

th.AssertErr(t, err1)
th.AssertErr(t, err2)
th.AssertErr(t, err3)
th.AssertNoErr(t, err4)
th.AssertErr(t, err5)
}

func TestUnmarshalNilSlice(t *testing.T) {
var x *[]TestPerson
var y []TestPerson

err1 := gophercloud.Result{}.ExtractIntoSlicePtr(&x, "")
err2 := gophercloud.Result{}.ExtractIntoSlicePtr(nil, "")
err3 := gophercloud.Result{}.ExtractIntoSlicePtr(y, "")
err4 := gophercloud.Result{}.ExtractIntoSlicePtr(&y, "")
err5 := gophercloud.Result{}.ExtractIntoSlicePtr(x, "")

th.AssertErr(t, err1)
th.AssertErr(t, err2)
th.AssertErr(t, err3)
th.AssertNoErr(t, err4)
th.AssertErr(t, err5)
}

// TestUnmarshalSliceofAnonymousStructs tests if UnmarshalJSON is called on each
// of the anonymous structs contained in an overarching struct slice.
func TestUnmarshalSliceOfAnonymousStructs(t *testing.T) {
Expand Down
Loading
Morty Proxy This is a proxified and sanitized view of the page, visit original site.