Fixing QuickJS bug with nan value handling, causing an infinite loop on Nintendo Switch #366
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.
The following scripting code was resulting in an infinite loop on Nintendo Switch in Minecraft with a custom behaviour pack:
The first while loop condition assumes that once it goes beyond the end of the array bounds, it returns undefined (which it does), but then it also assumes that comparing UNDEFINED to a real value, returns false (which it should, but doesn't).
This internal code in quick js relies on the C++ 'feature' that comparing any value against nan, returns false:
However, this is not a real constraint in the C++ stl/language.
https://stackoverflow.com/questions/38798791/nan-comparison-rule-in-c-c
It is a constraint for IEEE floating points, but C++ is not constrained to that requirement itself. So the result here is that on Switch sometimes it returns true for this evaluation, and in the scripting code above means we infinite loop.
Fix here is to deliberately and directly check for is nan and handle this case as false.