fix: prevent StackOverflow, IndexOutOfRange, and InvalidCast crashes on malformed JavaScript#2340
Merged
Merged
Conversation
…xecute Co-authored-by: lahma <171892+lahma@users.noreply.github.com>
Co-authored-by: lahma <171892+lahma@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Fix crashes in Engine.Execute from malformed JavaScript
fix: prevent StackOverflow, IndexOutOfRange, and InvalidCast crashes on malformed JavaScript
Mar 20, 2026
lahma
marked this pull request as ready for review
March 20, 2026 06:01
lahma
approved these changes
Mar 20, 2026
Collaborator
|
@copilot check ci failures |
…rm compatibility Co-authored-by: lahma <171892+lahma@users.noreply.github.com>
Contributor
Author
The CI failures on Windows and macOS were caused by the Fixed in 86d886d by:
|
lahma
approved these changes
Mar 20, 2026
This was referenced Mar 23, 2026
This was referenced Jun 8, 2026
This was referenced Jun 29, 2026
This was referenced Jul 7, 2026
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.
Fuzzing with AFL++/SharpFuzz found three distinct crash vectors in
Engine.Execute— all denial-of-service via uncaught CLR exceptions. Two terminate the process entirely.Crash 1 — StackOverflowException in
HoistingScope.ScriptWalker.VisitUnbounded recursion over deeply-nested ASTs during hoisting analysis — before execution begins, so all engine constraints (timeout, recursion limit, memory) are bypassed. StackOverflowException is uncatchable and terminates the process.
Fix: Split
Visitinto a guarded entry point andVisitCore. Added_depthcounter withMaxDepth = 2_000; exceeding it throwsScriptPreparationException— the existing publicJintExceptionsubclass for pre-execution failures.Crash 2 — IndexOutOfRangeException in
JsonParser.ScanHexEscapeOff-by-one:
_index < _length + 1permits_index == _length, which then accesses_source[_index]out-of-bounds when a JSON string ends immediately after\u(e.g."\u).Fix:
_index < _length + 1→_index < _length. The existingThrowErrorpath then correctly surfaces aJavaScriptException(SyntaxError).Crash 3 — InvalidCastException in
JintObjectExpression.Initializep.Value(typed asNode) was unconditionally cast toExpression. Acornima emits anAssignmentPatternnode for shorthand-default syntax ({a = expr}) in ambiguous destructuring contexts, which is not a subtype ofExpression.Fix: Added a runtime type check; non-
Expressionvalues now throw aJavaScriptException(SyntaxError) with a descriptive message instead of letting the cast propagate.Original prompt
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.