Skip to content

Commit

Permalink
fix a missing array limit check
Browse files Browse the repository at this point in the history
  • Loading branch information
rbri authored and gbrail committed Aug 16, 2023
1 parent fad89c7 commit a1e011b
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/org/mozilla/javascript/NativeArray.java
Original file line number Diff line number Diff line change
Expand Up @@ -1607,6 +1607,10 @@ private static long concatSpreadArg(
long srclen = getLengthProperty(cx, arg);
long newlen = srclen + offset;

if (newlen > NativeNumber.MAX_SAFE_INTEGER) {
throw ScriptRuntime.typeErrorById("msg.arraylength.too.big", newlen);
}

// First, optimize for a pair of native, dense arrays
if ((newlen <= Integer.MAX_VALUE) && (result instanceof NativeArray)) {
final NativeArray denseResult = (NativeArray) result;
Expand Down
65 changes: 65 additions & 0 deletions testsrc/org/mozilla/javascript/tests/es6/NativeArray2Test.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package org.mozilla.javascript.tests.es6;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.mozilla.javascript.Context;
import org.mozilla.javascript.ScriptableObject;

/** Test for NativeArray. */
public class NativeArray2Test {

private Context cx;
private ScriptableObject scope;

@Before
public void setUp() {
cx = Context.enter();
cx.setLanguageVersion(Context.VERSION_ES6);
scope = cx.initStandardObjects();
}

@After
public void tearDown() {
Context.exit();
}

@Test
public void concatLimitSpreadable() {
String js =
"var spreadable = {};\n"
+ "spreadable.length = Number.MAX_SAFE_INTEGER;\n"
+ "spreadable[Symbol.isConcatSpreadable] = true;\n"
+ "try {\n"
+ " [1].concat(spreadable);\n"
+ "} catch(e) {"
+ " '' + e;\n"
+ "};";

String result = (String) cx.evaluateString(scope, js, "test", 1, null);
assertTrue(result.endsWith("exceeds supported capacity limit."));
}

@Test
public void concatLimitSpreadable2() {
String js =
"var spreadable = {\n"
+ " length: Number.MAX_SAFE_INTEGER,\n"
+ " get 0() {\n"
+ " throw new Error('get failed');\n"
+ " },\n"
+ "};\n"
+ "spreadable[Symbol.isConcatSpreadable] = true;\n"
+ "try {\n"
+ " [].concat(spreadable);\n"
+ "} catch(e) {"
+ " '' + e;\n"
+ "};";

String result = (String) cx.evaluateString(scope, js, "test", 1, null);
assertEquals(result, "Error: get failed", result);
}
}

0 comments on commit a1e011b

Please sign in to comment.