Skip to content

Fix JSON.parse source records for duplicate keys - #1851

Open
jeromeleong wants to merge 1 commit into
quickjs-ng:masterfrom
jeromeleong:master
Open

jeromeleong wants to merge 1 commit into
quickjs-ng:masterfrom
jeromeleong:master

Conversation

@jeromeleong

Copy link
Copy Markdown

JSON.parse keeps a parse record for each object member, including duplicate keys, so the reviver can access its source text. With up to eight input members, json_parse_record_find() used a forward linear search and returned the first matching record. The hash-table path used for larger inputs returns the last matching record instead.

For {"x":1.0,"x":1}, this exposed source "1.0" alongside the value 1, but adding unrelated members past the threshold changed the source to "1". Since both literals parse to the same value, the SameValue check accepted the stale record. For unequal duplicate values, that check rejected the record and incorrectly omitted source.

Search the linear entries backwards so both paths select the last occurrence, as required by CreateJSONParseRecord. This keeps context.source associated with the surviving property value regardless of the input member count.

JSON.parse keeps a parse record for each object member, including duplicate keys, so the reviver can access its source text. With up to eight input members, json_parse_record_find() used a forward linear search and returned the first matching record. The hash-table path used for larger inputs returns the last matching record instead.

For {"x":1.0,"x":1}, this exposed source "1.0" alongside the value 1, but adding unrelated members past the threshold changed the source to "1". Since both literals parse to the same value, the SameValue check accepted the stale record. For unequal duplicate values, that check rejected the record and incorrectly omitted source.

Search the linear entries backwards so both paths select the last occurrence, as required by CreateJSONParseRecord. This keeps context.source associated with the surviving property value regardless of the input member count.

This branch has not been deployed

No deployments
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.

1 participant