Fixed overflow bug in [Big]Arrays.ensureOffsetLength()

This commit is contained in:
Sebastiano Vigna 2022-03-03 15:37:50 +00:00
parent 4e5ab259cc
commit a637cf8b18
8 changed files with 23 additions and 13 deletions

View file

@ -1,3 +1,8 @@
8.5.9
- Arrays.ensureOffsetLength() could not work properly due to an
integer overflow. Thanks to Alex Herbert for fixing this bug.
8.5.8
- Fixed erroneous switch to Java 9.

View file

@ -96,7 +96,7 @@ public class Arrays {
// When Java 9 becomes the minimum, use Objects#checkFromIndexSize, as that can be an intrinsic
if (offset < 0) throw new ArrayIndexOutOfBoundsException("Offset (" + offset + ") is negative");
if (length < 0) throw new IllegalArgumentException("Length (" + length + ") is negative");
if (offset + length > arrayLength) throw new ArrayIndexOutOfBoundsException("Last index (" + (offset + length) + ") is greater than array length (" + arrayLength + ")");
if (length > arrayLength - offset) throw new ArrayIndexOutOfBoundsException("Last index (" + (offset + length) + ") is greater than array length (" + arrayLength + ")");
}
/**

View file

@ -189,7 +189,6 @@ public class InspectableFileCachedInputStream extends MeasurableInputStream impl
overflowFile.delete();
}
@SuppressWarnings("deprecation")
@Override
protected void finalize() throws Throwable {
try {

View file

@ -142,4 +142,8 @@ public class ArraysTest {
testParallelQuickSort(t);
}
@Test(expected = ArrayIndexOutOfBoundsException.class)
public void testEnsureOffSetLength() {
Arrays.ensureOffsetLength(42, Integer.MAX_VALUE, 10);
}
}

View file

@ -67,4 +67,9 @@ public class BigArraysTest {
// Never completes!
longList.ensureCapacity(2);
}
@Test(expected = ArrayIndexOutOfBoundsException.class)
public void testEnsureOffSetLength() {
BigArrays.ensureOffsetLength(42, Long.MAX_VALUE, 10);
}
}

View file

@ -46,8 +46,8 @@ public class AbstractInt2IntMapTest extends Int2IntMapGenericTest<AbstractInt2In
private final IntList keys = new IntArrayList();
private final IntList values = new IntArrayList();
@Override
public int get(int key) {
int index = keys.indexOf(key);
public int get(final int key) {
final int index = keys.indexOf(key);
if (index == -1) {
return defaultReturnValue();
}
@ -58,8 +58,8 @@ public class AbstractInt2IntMapTest extends Int2IntMapGenericTest<AbstractInt2In
return keys.size();
}
@Override
public int put(int key, int value) {
int index = keys.indexOf(key);
public int put(final int key, final int value) {
final int index = keys.indexOf(key);
if (index == -1) {
keys.add(key);
values.add(value);
@ -68,8 +68,8 @@ public class AbstractInt2IntMapTest extends Int2IntMapGenericTest<AbstractInt2In
return values.set(index, value);
}
@Override
public int remove(int key) {
int index = keys.indexOf(key);
public int remove(final int key) {
final int index = keys.indexOf(key);
if (index == -1) {
return defaultReturnValue();
}
@ -123,11 +123,10 @@ public class AbstractInt2IntMapTest extends Int2IntMapGenericTest<AbstractInt2In
}
@Override
public int get(int key) {
public int get(final int key) {
return -1;
}
@SuppressWarnings("unchecked")
@Override
public ObjectSet<Entry> int2IntEntrySet() {
return ObjectSets.EMPTY_SET;
@ -148,7 +147,7 @@ public class AbstractInt2IntMapTest extends Int2IntMapGenericTest<AbstractInt2In
}
@Override
public int get(int key) {
public int get(final int key) {
return key == 0 ? 0 : -1;
}

View file

@ -31,7 +31,6 @@ import org.junit.Test;
import it.unimi.dsi.fastutil.io.BinIO;
@SuppressWarnings("deprecation")
public class Reference2ReferenceArrayMapTest {
@Test

View file

@ -30,7 +30,6 @@ import org.junit.Test;
import it.unimi.dsi.fastutil.io.BinIO;
@SuppressWarnings("deprecation")
public class ReferenceArraySetTest {
@Test