Skip to content

Commit

Permalink
EnsureCapacity can throw if called against sparse mode array (#746)
Browse files Browse the repository at this point in the history
  • Loading branch information
lahma committed Jun 8, 2020
1 parent 56ca37b commit 293161f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
12 changes: 12 additions & 0 deletions Jint.Tests/Runtime/ArrayTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,17 @@ public void EmptyStringKey()

Assert.Equal(8, result);
}

[Fact]
public void LargeArraySize()
{
const string code = @"
let arr = [];
for (let i = 0; i < 10000; i++) arr.push(i);
for (let i=0;i<10000;i++) arr.splice(0, 1);
";
var engine = new Engine();
engine.Execute(code);
}
}
}
12 changes: 7 additions & 5 deletions Jint/Native/Array/ArrayInstance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -648,13 +648,15 @@ private void ConvertToSparse()

internal void EnsureCapacity(uint capacity)
{
if (capacity <= MaxDenseArrayLength && capacity > (uint) _dense.Length)
if (capacity > MaxDenseArrayLength || _dense is null || capacity <= (uint) _dense.Length)
{
// need to grow
var newArray = new PropertyDescriptor[capacity];
System.Array.Copy(_dense, newArray, _dense.Length);
_dense = newArray;
return;
}

// need to grow
var newArray = new PropertyDescriptor[capacity];
System.Array.Copy(_dense, newArray, _dense.Length);
_dense = newArray;
}

public IEnumerator<JsValue> GetEnumerator()
Expand Down

0 comments on commit 293161f

Please sign in to comment.