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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Loading