Skip to content

MemorySegmentIndexInput absolute read methods might modify position #15730

@viliam-durina

Description

@viliam-durina

Description

The absolute read methods in RandomAccessInput should read from the given position, and not use or modify the input's position. However MemorySegmentIndexInput's absolute read methods modify the input's position if the read happens at the segment boundary, because if the value can't be read from a single segment, the implementation falls back to seek + relative read.

The following test reproduces it:

public class TestMemorySegmentIndexInput extends LuceneTestCase {

  public void testPositionNotModifiedAtBoundary() throws IOException {
    try (Arena arena = Arena.ofConfined()) {
      MemorySegment seg1 = arena.allocate(16);
      MemorySegment seg2 = arena.allocate(16);
      try (MemorySegmentIndexInput input = MemorySegmentIndexInput.newInstance("", arena,
          new MemorySegment[] {seg1, seg2}, 32, 4, false, null)) {
        input.readInt(4);
        assertEquals(0, input.getFilePointer());
        input.readInt(14);
        assertEquals(0, input.getFilePointer()); // this fails, position is now 18
      }
    }
  }
}

Perhaps it's not an actual issue, I don't know if Lucene combines relative and absolute reads anywhere and relies on the position not being changed by the absolute read, and even if it does, multiple segments are used if the file is larger than 16GB, so it's very unlikely. But it should be fixed anyway I think, because the root cause will be very hard to figure out should it happen.

Version and environment details

main branch.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions