Skip to content

Commit

Permalink
Fix error passing oversized array to PrimitiveImmutableList
Browse files Browse the repository at this point in the history
  • Loading branch information
Ampflower committed Jul 20, 2024
1 parent 3e59d8a commit d054bf3
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 1 deletion.
2 changes: 1 addition & 1 deletion drv/ImmutableList.drv
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ public class IMMUTABLE_LIST KEY_GENERIC extends LISTS.ImmutableListBase KEY_GENE
@Override
public KEY_TYPE[] toArray(KEY_TYPE[] a) {
if (a == null || a.length < size()) a = new KEY_TYPE[this.a.length];
System.arraycopy(this.a, 0, a, 0, a.length);
System.arraycopy(this.a, 0, a, 0, size());
return a;
}
#else // KEYS_REFERENCE
Expand Down
7 changes: 7 additions & 0 deletions test/it/unimi/dsi/fastutil/ints/IntArrayListTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package it.unimi.dsi.fastutil.ints;

import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals;
Expand Down Expand Up @@ -605,4 +606,10 @@ public void testLegacyMainMethodTests() throws Exception {
public void testZeroLengthToArray() {
assertSame(IntArrays.EMPTY_ARRAY, new IntArrayList().toIntArray());
}

@Test
public void testOversizedToArray() {
final IntArrayList l = IntArrayList.of(0, 1, 2, 3);
assertArrayEquals(new int[]{0, 1, 2, 3, 0}, l.toArray(new int[5]));
}
}
6 changes: 6 additions & 0 deletions test/it/unimi/dsi/fastutil/ints/IntImmutableListTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -439,4 +439,10 @@ public void testSubList_testCompareTo_OtherListImpl() {
public void testZeroLengthToArray() {
assertSame(IntArrays.EMPTY_ARRAY, IntImmutableList.of().toIntArray());
}

@Test
public void testOversizedToArray() {
final IntImmutableList l = IntImmutableList.of(0, 1, 2, 3);
assertArrayEquals(new int[]{0, 1, 2, 3, 0}, l.toArray(new int[5]));
}
}

0 comments on commit d054bf3

Please sign in to comment.