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

Avoid overflow in index input slices invariant checks #14126

Merged
merged 3 commits into from
Jan 20, 2025
Merged
Changes from 1 commit
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
Next Next commit
Avoid overflow in index input slices invariant checks
ChrisHegarty committed Jan 9, 2025
commit d50908e7e0df12c891af64944b11b9c0fca7740f
Original file line number Diff line number Diff line change
@@ -401,7 +401,7 @@ private static final class SlicedIndexInput extends BufferedIndexInput {
? base.toString()
: (base.toString() + " [slice=" + sliceDescription + "]"),
BufferedIndexInput.BUFFER_SIZE);
if (offset < 0 || length < 0 || offset + length > base.length()) {
if ((length | offset) < 0 || length > base.length() - offset) {
throw new IllegalArgumentException(
"slice() " + sliceDescription + " out of bounds: " + base);
}
Original file line number Diff line number Diff line change
@@ -424,7 +424,7 @@ public void skipBytes(long numBytes) throws IOException {
}

public ByteBuffersDataInput slice(long offset, long length) {
if (offset < 0 || length < 0 || offset + length > this.length) {
if ((length | offset) < 0 || length > this.length - offset) {
throw new IllegalArgumentException(
String.format(
Locale.ROOT,
Original file line number Diff line number Diff line change
@@ -139,7 +139,7 @@ public NIOFSIndexInput clone() {

@Override
public IndexInput slice(String sliceDescription, long offset, long length) throws IOException {
if (offset < 0 || length < 0 || offset + length > this.length()) {
if ((length | offset) < 0 || length > this.length() - offset) {
throw new IllegalArgumentException(
"slice() "
+ sliceDescription
Original file line number Diff line number Diff line change
@@ -597,7 +597,7 @@ public final MemorySegmentIndexInput clone() {
*/
@Override
public final MemorySegmentIndexInput slice(String sliceDescription, long offset, long length) {
if (offset < 0 || length < 0 || offset + length > this.length) {
if ((length | offset) < 0 || length > this.length - offset) {
throw new IllegalArgumentException(
"slice() "
+ sliceDescription
Original file line number Diff line number Diff line change
@@ -136,7 +136,7 @@ public RAFIndexInput clone() {

@Override
public IndexInput slice(String sliceDescription, long offset, long length) throws IOException {
if (offset < 0 || length < 0 || offset + length > this.length()) {
if ((length | offset) < 0 || length > this.length() - offset) {
throw new IllegalArgumentException(
"slice() " + sliceDescription + " out of bounds: " + this);
}
Original file line number Diff line number Diff line change
@@ -771,6 +771,12 @@ public void testSliceOutOfBounds() throws Exception {
slice.slice("slice3sub", 1, len / 2);
});

expectThrows(
IllegalArgumentException.class,
() -> {
i.slice("slice4", Long.MAX_VALUE - 1, 10);
});

i.close();
}
}
Original file line number Diff line number Diff line change
@@ -194,7 +194,7 @@ public IndexInput slice(String sliceDescription, long offset, long length) throw
public IndexInput slice(
String sliceDescription, long offset, long length, ReadAdvice readAdvice)
throws IOException {
if (offset < 0 || offset + length > sliceLength) {
if ((length | offset) < 0 || length > sliceLength - offset) {
throw new IllegalArgumentException();
}
IndexInput clone = in.clone();