Skip to content

Fix NullPointerException when reading "stack" of an error with a null prototype - #2550

Open
average-web-dev wants to merge 1 commit into
mozilla:masterfrom
average-web-dev:fix-error-stack-null-prototype
Open

average-web-dev wants to merge 1 commit into
mozilla:masterfrom
average-web-dev:fix-error-stack-null-prototype

Conversation

@average-web-dev

Copy link
Copy Markdown

Reading stack of an error whose prototype was set to null throws a Java
NullPointerException. A script's try/catch can't catch it:

var e = new Error("m");
Object.setPrototypeOf(e, null);
e.stack; // java.lang.NullPointerException in NativeError.getStackDelegated()

This came up in the 1.9 backport #2544, where @gbrail
asked
whether the bug is 1.9 only or also on master. It is partly both:

  • On 1.9, getStackDelegated() casts the prototype to NativeError, so a plain
    object as prototype (as transpiled class MyError extends Error {} does)
    already throws a ClassCastException. On master, Convert NativeError to descriptor #2295 changed the cast to
    ScriptableObject, which fixed that case.
  • A null prototype still fails on master, with a NullPointerException on
    the next line.

This PR fixes it on master. #2544 is the backport of this PR to 1.9.

Cause and fix

NativeError.getStackDelegated() casts the error's prototype to
ScriptableObject to look up the stack trace settings (stackTraceLimit,
prepareStackTrace), and then calls a method on it.

The fix only reads the settings when the prototype is a ScriptableObject.
Otherwise it uses the defaults, as it already does when the prototype has no
settings attached.

Tests

New test ErrorPropertiesTest.stackWithChangedPrototype, in all modes. It
covers a plain object, null and a Proxy as prototype, the same cases as in
#2544. Without the fix the null case fails. The full test suite passes.

A note on how this was developed

I developed this PR with the help of an AI coding agent (Claude Code), and I
reviewed, tested and take responsibility for all of the changes. If the project
has concerns about or rules for AI-assisted contributions, please let me know.

I'm dyslexic, so I usually let AI help me write or proofread text like this
description. If anything reads unclear, please ask and I'll clarify.

🤖 Generated with Claude Code

… prototype

NativeError.getStackDelegated() casts the error's prototype to
ScriptableObject to look up the stack trace settings. If the prototype was
set to null with Object.setPrototypeOf(), this threw a Java
NullPointerException that scripts cannot catch. Fall back to the default
settings when the prototype is not a ScriptableObject.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

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