fix: useDeepCompareEffect throws on null-prototype objects#2711
Open
chatman-media wants to merge 1 commit into
Open
fix: useDeepCompareEffect throws on null-prototype objects#2711chatman-media wants to merge 1 commit into
chatman-media wants to merge 1 commit into
Conversation
`isDeepEqual` delegated to `fast-deep-equal/react`, which assumes every object inherits `valueOf`/`toString` from `Object.prototype` and calls them unconditionally. Objects created with `Object.create(null)` have neither, so the comparison threw `TypeError: a.valueOf is not a function` (closes streamich#2700). Inline the comparator and guard the `valueOf`/`toString` shortcuts behind a real-method check so null-prototype objects fall through to the own-keys comparison. Behaviour is otherwise identical (verified by a 200k-case fuzz against the upstream module). The now-unused `fast-deep-equal` direct dependency is dropped.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #2700.
Problem
useDeepCompareEffect(anduseCustomCompareEffect/useBatteryvia the sharedsrc/misc/isDeepEqual.ts) throws when a dependency is an object created withObject.create(null):isDeepEqualsimply re-exportedfast-deep-equal/react, which takes avalueOf/toStringshortcut:A null-prototype object has no
valueOf, soa.valueOfisundefined;undefined !== Object.prototype.valueOfistrue, and the code then callsa.valueOf()and crashes. This is the upstream issue epoberezkin/fast-deep-equal#111, which has been unmaintained for years.When this happens inside the hook, React renders into an error state (the comparison runs during render, so the effect never re-runs and the component breaks).
Fix
Inline the
fast-deep-equal/reactcomparator intosrc/misc/isDeepEqual.tsand guard thevalueOf/toStringshortcuts behind a real-method check (typeof … === "function"), so null-prototype objects fall through to the regular own-keys comparison. The React-specific_ownerhandling is preserved.Behaviour is otherwise identical — verified by a 200,000-case fuzz comparing the inlined implementation against
fast-deep-equal/react(numbers, strings, booleans,null/undefined/NaN,Date,RegExp, nested arrays/objects,Map, functions): 0 divergences on every input that does not use a null prototype.The now-unused direct
fast-deep-equaldependency is dropped frompackage.json(it remains in the tree transitively, so the lockfile is unchanged).Tests
Added a regression test to
tests/useDeepCompareEffect.test.tsthat renders the hook with aObject.create(null)dependency and asserts the comparison never errors and the effect re-runs only on real change.master:Received: [TypeError: a.valueOf is not a function]tsc --noEmitandeslintclean.