Skip to content

[v2] fix: skip non-struct fields in extractIntoPtr slice path to prevent unmarshal panic - #3730

Merged
mandre merged 1 commit into
v2from
bp-v2-a95f7c3
May 5, 2026
Merged

mandre merged 1 commit into
v2from
bp-v2-a95f7c3

Conversation

@gophercloud-backport-bot

Copy link
Copy Markdown

Backport: #3726

In extractIntoPtr function (link), the slice path iterates over all fields of the target struct and attempts to unmarshal the full JSON body into each one. When the target struct contains a non-struct field, such as a *bool (as described below), the code attempts to unmarshal a JSON object into it, causing a type mismatch error.

type PortExt struct {
	ports.Port
	portsecurity.PortSecurityExt
	portsbinding.PortsBindingExt

	PropagateUplinkStatusPtr *bool `json:"propagate_uplink_status,omitempty"`
}
for i := 0; i < newType.NumField(); i++ {
    // newType.Field(3) = PropagateUplinkStatusPtr (*bool)
	s := newType.Field(i).Addr()

	// Unmarshal is used rather than NewDecoder to also work
	// around the above-mentioned bug
	err = json.Unmarshal(b, s) // json.Unmarshal(b, **bool)
	if err != nil {
		return err
	}
}

This PR proposes a fix to only decode embedded structs in the loop, adding a check that skips non-struct types. All other field types are handled correctly by this ExtractInto call and by the final json.Unmarshal call after the loop.

@github-actions github-actions Bot added v2 This PR targets v2 edit:gophercloud This PR updates common Gophercloud code semver:patch No API change labels May 5, 2026
@coveralls

Copy link
Copy Markdown

Coverage Status

coverage: 63.805% (-0.001%) from 63.806% — bp-v2-a95f7c3 into v2

@mandre
mandre merged commit 5b26c58 into v2 May 5, 2026
16 checks passed
@mandre
mandre deleted the bp-v2-a95f7c3 branch May 5, 2026 09:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

edit:gophercloud This PR updates common Gophercloud code semver:patch No API change v2 This PR targets v2

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants