fix: infinite loop when object has an empty string key#58
Conversation
Summary of ChangesHello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request addresses a critical bug in the object differ utility that led to infinite loops and application crashes when processing objects containing empty string keys. The fix refines the key comparison logic to accurately distinguish between an undefined key and an empty string key, thereby preventing erroneous loop conditions and enhancing the stability of the diffing process. Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Changelog
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request fixes an infinite loop that occurs when diffing objects with an empty string as a key. The fix correctly uses a strict undefined check instead of a falsy check to determine if the key arrays are exhausted. A regression test is added to cover this scenario. The implementation of the fix is correct. I've suggested a small improvement to the new test case to make it more concise and efficient by removing a redundant function call.
|
We found this bug at happo.io after seeing certain reports make the page unresponsive. I threw Claude at the data and it tracked it down to this issue in json-diff-kit. Thank you very much for making this library available to the public @RexSkz! We've used it for a few years now and it's been great. |
|
Thank you for your contribution! It's really a rookie mistake in JavaScript. BTW, do you think it's necessary to make code changes according to Gemini's advice? (I'm okay with it. If you are also okay, you can directly resolve the comment and merge this PR.) |
The key-advance logic in `diffObject` used falsy checks (!keyLeft, !keyRight) to detect when one side had run out of keys. Because an empty string is falsy in JavaScript, any JSON object containing "" as a key would cause the left pointer to never advance, producing an infinite while-loop that exhausted all available memory. Fix: replace the falsy checks with strict undefined comparisons (=== undefined) so that empty-string keys are handled correctly. A regression test is added to differ.spec.ts covering objects with an empty-string key on both sides. Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
1779cbf to
e49db76
Compare
|
|
We have all made this mistake, don't worry! I resolved the Gemini feedback without changes. I also signed the commits so that you can merge it. |
|
v1.0.35 has been published :) |



Problem
When diffing two objects where either side contains an empty string (
"") as a key,diffObjectenters an infinite loop and exhausts all available memory, crashing the process/browser tab.The root cause is the key-advance logic at the end of the
whileloop:Because
!''istruein JavaScript, when the current left key is the empty string the code thinks the left side is exhausted and only advances the right pointer.keysLeftis therefore never shifted past"", and oncekeysRightdrains the loop spins forever.Fix
Replace the falsy checks with strict
=== undefinedcomparisons:Test
A regression test is added to
differ.spec.tsthat diffs two objects both containing an empty-string key and asserts: the call completes without throwing, both sides produce the same number of lines, and the changed value is marked as a modification.Reproduction