Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Setting private properties causes NRE #1921

Closed
VelvetToroyashi opened this issue Jul 22, 2024 · 6 comments
Closed

Setting private properties causes NRE #1921

VelvetToroyashi opened this issue Jul 22, 2024 · 6 comments

Comments

@VelvetToroyashi
Copy link
Contributor

Version used

Jint 3.1.5

Describe the bug

Setting a private property on a class in any capacity throws a NullReferenceException from native code.

To Reproduce

class ClassWithPrivateField {
    #nonexistent = 'evidently';
}

new ClassWithPrivateField().#nonexistent = 'explosion!';

Expected behavior

Jint throws a managed (JavaScript) exception as it does with attempting to use other invalid properties.

Additional context

This specifically occurs when attempting to set a private property outside of a class (e.g., in a module function). Interestingly, doing it in a class function throws a different (and equally as vague) exception (though, "not found" does seem more indicative of correct behavior here-):

Promise was rejected with value ReferenceError: TODO Not found!!
Stack trace: 
   at Bot (eventCallback, commandCallback) bootstrap:34:23
   at entrypoint () bootstrap:15:21
   at bootstrap:23:8



@lahma
Copy link
Collaborator

lahma commented Jul 22, 2024

So iare there more code sample of specific cases? Ideally in c# utilizing the Engine.

@VelvetToroyashi
Copy link
Contributor Author

VelvetToroyashi commented Jul 22, 2024

@lahma Here's a minimal repro in .NET:
(Just realized I misread the message, oopsies,)

In normal code I don't foresee this really happening intentionally, but for arbitrary inputs to the engine this could happen intentionally or otherwise

using Jint;

new Engine().Evaluate
(
    """
    class A { }
    
    new A().#nonexistent = 1;
    """
);

And the produced exception:

Unhandled exception. System.NullReferenceException: Object reference not set to an instance of an object.
   at Jint.Runtime.Interpreter.Expressions.JintPrivateIdentifierExpression.EvaluateInternal(EvaluationContext context)
   at Jint.Runtime.Interpreter.Expressions.JintExpression.GetValue(EvaluationContext context)
   at Jint.Runtime.Interpreter.Expressions.JintMemberExpression.EvaluateInternal(EvaluationContext context)
   at Jint.Runtime.Interpreter.Expressions.JintAssignmentExpression.SimpleAssignmentExpression.SetValue(EvaluationContext context)
   at Jint.Runtime.Interpreter.Expressions.JintAssignmentExpression.SimpleAssignmentExpression.EvaluateInternal(EvaluationContext context)
   at Jint.Runtime.Interpreter.Expressions.JintExpression.GetValue(EvaluationContext context)
   at Jint.Runtime.Interpreter.Statements.JintExpressionStatement.ExecuteInternal(EvaluationContext context)
   at Jint.Engine.ScriptEvaluation(ScriptRecord scriptRecord, ParserOptions parserOptions)
   at Jint.Engine.<>c__DisplayClass96_0.<Execute>b__0()
   at Jint.Engine.ExecuteWithConstraints[T](Boolean strict, Func`1 callback)
   at Jint.Engine.Execute(Prepared`1& preparedScript)
   at Jint.Engine.Evaluate(Prepared`1& preparedScript)

@lahma
Copy link
Collaborator

lahma commented Jul 22, 2024

Thanks, what about the different error message in different call context?

@VelvetToroyashi
Copy link
Contributor Author

VelvetToroyashi commented Jul 22, 2024

class A { 
    constructor() { #nonexistent = 2; }
}
new A();

Produces the following error

Unhandled exception. Jint.Runtime.JavaScriptException: TODO Not found!!
 ---> Error: TODO Not found!!
   at A () <anonymous>:2:20
   at <anonymous>:5:5
   --- End of inner exception stack trace ---
   at Jint.Engine.ScriptEvaluation(ScriptRecord scriptRecord, ParserOptions parserOptions)
   at Jint.Engine.<>c__DisplayClass96_0.<Execute>b__0()
   at Jint.Engine.ExecuteWithConstraints[T](Boolean strict, Func`1 callback)
   at Jint.Engine.Execute(Prepared`1& preparedScript)
   at Jint.Engine.Execute(String code, String source)
   at Program.<Main>$(String[] args) in /media/velvet/NVMe/Projects/DiscordJSPluginsBot/DiscordJSPluginsBot/Program.cs:line 14

@lahma
Copy link
Collaborator

lahma commented Jul 22, 2024

Thanks for the repros. Now far easier to investigate 👍🏻

@lahma
Copy link
Collaborator

lahma commented Jul 22, 2024

This was a bit monkey-patched as in v4 the parser actually detects these errors better instead of allowing the code to get into Jint's runtime logic. But at least the error message should now be better.

@lahma lahma closed this as completed Jul 22, 2024
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

No branches or pull requests

2 participants