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

Fix Array.prototype.toString() stackoverflow #1976

Merged
merged 6 commits into from
Oct 6, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions Jint.Tests/Runtime/ArrayTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@ public void ArrayPrototypeToStringWithObject()
Assert.Equal("[object Object]", result);
}

[Fact]
public void ArrayPrototypeToStringWithCircularReference()
{
var result = _engine.Evaluate("Array.prototype.toString.call((c = [1, 2, 3, 4], c[1] = c, c))").AsString();

Assert.Equal("1,,3,4", result);
}

[Fact]
public void EmptyStringKey()
{
Expand Down
8 changes: 4 additions & 4 deletions Jint/Native/Array/ArrayPrototype.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1273,14 +1273,14 @@ private JsValue Join(JsValue thisObject, JsValue[] arguments)
return JsString.Empty;
}

static string StringFromJsValue(JsValue value)
static string StringFromJsValue(JsValue value, JsValue thisObject)
{
return value.IsNullOrUndefined()
return value.IsNullOrUndefined() || thisObject == value
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for clarity, I think this should be ReferenceEquals(thisObject, value), the operator overloads can be a bit quirky

? ""
: TypeConverter.ToString(value);
}

var s = StringFromJsValue(o.Get(0));
var s = StringFromJsValue(o.Get(0), thisObject);
if (len == 1)
{
return s;
Expand All @@ -1294,7 +1294,7 @@ static string StringFromJsValue(JsValue value)
{
sb.Append(sep);
}
sb.Append(StringFromJsValue(o.Get(k)));
sb.Append(StringFromJsValue(o.Get(k), thisObject));
}

return sb.ToString();
Expand Down
Loading