Skip to content

Fix stack overflow crash when decoding null values in JSON#560

Draft
kieran-osgood-shopify wants to merge 1 commit into
mainfrom
fix/decode-dictionary-null-crash
Draft

Fix stack overflow crash when decoding null values in JSON#560
kieran-osgood-shopify wants to merge 1 commit into
mainfrom
fix/decode-dictionary-null-crash

Conversation

@kieran-osgood-shopify
Copy link
Copy Markdown
Contributor

The UnkeyedDecodingContainer.decode([Any].Type) method enters an infinite recursion when encountering JSON null values. None of the type-specific decode attempts (Bool, Double, String, etc.) succeed for null, so isAtEnd never becomes true and the container index never advances, causing a stack overflow.

Fix by checking decodeNil() first in both the unkeyed and keyed container extensions. Also add a terminal else-break in the unkeyed loop to prevent infinite loops for any other unexpected value types.

Additionally, handle null values in KeyedDecodingContainer.decode( [String: Any].Type) so null dictionary values are preserved as NSNull rather than silently dropped.

What changes are you making?


Before you merge

Important

Releasing a new version of the kit?

Releasing a new major version?

  • I have bumped the version number in the README.

Tip

See the Contributing documentation for instructions on how to publish a new version of the library.

The UnkeyedDecodingContainer.decode([Any].Type) method enters an
infinite recursion when encountering JSON null values. None of the
type-specific decode attempts (Bool, Double, String, etc.) succeed
for null, so isAtEnd never becomes true and the container index
never advances, causing a stack overflow.

Fix by checking decodeNil() first in both the unkeyed and keyed
container extensions. Also add a terminal else-break in the unkeyed
loop to prevent infinite loops for any other unexpected value types.

Additionally, handle null values in KeyedDecodingContainer.decode(
[String: Any].Type) so null dictionary values are preserved as
NSNull rather than silently dropped.
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 this pull request may close these issues.

2 participants