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 a missing array limit check #1371

Merged
merged 1 commit into from
Aug 16, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
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);
}
}