Skip to content

Commit

Permalink
Removing duplicate code and added text to IAE when list.length > Inte…
Browse files Browse the repository at this point in the history
…ger.MAX_VALUE
  • Loading branch information
rPraml committed May 21, 2021
1 parent a9b4499 commit e6ba640
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 16 deletions.
21 changes: 5 additions & 16 deletions src/org/mozilla/javascript/NativeArray.java
Original file line number Diff line number Diff line change
Expand Up @@ -2101,11 +2101,7 @@ public Object[] toArray() {

@Override
public Object[] toArray(Object[] a) {
long longLen = length;
if (longLen > Integer.MAX_VALUE) {
throw new IllegalStateException();
}
int len = (int) longLen;
int len = size();
Object[] array =
a.length >= len
? a
Expand All @@ -2128,7 +2124,8 @@ public boolean containsAll(Collection c) {
public int size() {
long longLen = length;
if (longLen > Integer.MAX_VALUE) {
throw new IllegalStateException();
throw new IllegalStateException(
"list.length (" + length + ") exceeds Integer.MAX_VALUE");
}
return (int) longLen;
}
Expand Down Expand Up @@ -2159,11 +2156,7 @@ public Object get(int index) {

@Override
public int indexOf(Object o) {
long longLen = length;
if (longLen > Integer.MAX_VALUE) {
throw new IllegalStateException();
}
int len = (int) longLen;
int len = size();
if (o == null) {
for (int i = 0; i < len; i++) {
if (get(i) == null) {
Expand All @@ -2182,11 +2175,7 @@ public int indexOf(Object o) {

@Override
public int lastIndexOf(Object o) {
long longLen = length;
if (longLen > Integer.MAX_VALUE) {
throw new IllegalStateException();
}
int len = (int) longLen;
int len = size();
if (o == null) {
for (int i = len - 1; i >= 0; i--) {
if (get(i) == null) {
Expand Down
17 changes: 17 additions & 0 deletions testsrc/org/mozilla/javascript/tests/Bug466207Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import java.util.ListIterator;
import junit.framework.TestCase;
import org.mozilla.javascript.Context;
import org.mozilla.javascript.NativeArray;
import org.mozilla.javascript.ScriptableObject;

/**
Expand Down Expand Up @@ -121,4 +122,20 @@ public void testSublist() {
assertTrue(list.subList(0, 0).isEmpty());
assertTrue(list.subList(5, 5).isEmpty());
}

public void testBigList() {
Context context = Context.enter();
ScriptableObject scope = context.initStandardObjects();
NativeArray array =
(NativeArray)
context.evaluateString(scope, "new Array(4294967295)", "testsrc", 1, null);
Context.exit();
assertEquals(4294967295L, array.getLength());
try {
array.size();
fail("Exception expected");
} catch (IllegalStateException e) {
assertEquals("list.length (4294967295) exceeds Integer.MAX_VALUE", e.getMessage());
}
}
}

0 comments on commit e6ba640

Please sign in to comment.