Skip to content

fix: skip non-struct fields in extractIntoPtr slice path to prevent unmarshal panic - #3726

Merged
mandre merged 1 commit into
gophercloud:mainfrom
winiciusallan:fix-extractinto
May 5, 2026
Merged

mandre merged 1 commit into
gophercloud:mainfrom
winiciusallan:fix-extractinto

Conversation

@winiciusallan

@winiciusallan winiciusallan commented Apr 23, 2026 •

Copy link
Copy Markdown
Contributor

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 the edit:gophercloud This PR updates common Gophercloud code label Apr 23, 2026
@github-actions github-actions Bot added semver:patch No API change backport-v2 This PR will be backported to v2 labels Apr 23, 2026
@coveralls

Copy link
Copy Markdown

Coverage Status

coverage: 64.021% (-0.001%) from 64.022% — winiciusallan:fix-extractinto into gophercloud:main

@mandre
mandre added this pull request to the merge queue May 5, 2026
Merged via the queue into gophercloud:main with commit 9e77a08 May 5, 2026
64 checks passed
@winiciusallan
winiciusallan deleted the fix-extractinto branch May 5, 2026 08:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backport-v2 This PR will be backported to v2 edit:gophercloud This PR updates common Gophercloud code semver:patch No API change

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants