Skip to content

Commit 59e1a83

Browse files
committed
Added index bounds check
1 parent 7456405 commit 59e1a83

File tree

2 files changed

+9
-8
lines changed

2 files changed

+9
-8
lines changed

query/src/main/java/tech/ydb/query/tools/QueryReader.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -176,8 +176,12 @@ public int getRowCount() {
176176

177177
@Override
178178
public void setRowIndex(int index) {
179+
if (index < 0 || index >= rowsCount) {
180+
throw new IndexOutOfBoundsException(String.format("Index %s out of bounds for length %s",
181+
index, rowsCount));
182+
}
179183
partIndex = 0;
180-
int currentIdx = Math.max(index, 0);
184+
int currentIdx = index;
181185
while (partIndex < parts.length) {
182186
int readerRows = parts[partIndex].getRowCount();
183187
if (currentIdx < readerRows) {
@@ -189,9 +193,6 @@ public void setRowIndex(int index) {
189193
partIndex++;
190194
}
191195

192-
if (partIndex >= parts.length) {
193-
partIndex = parts.length - 1;
194-
}
195196
for (int partStep = partIndex + 1; partStep < parts.length; partStep++) {
196197
parts[partStep].setRowIndex(0);
197198
}

query/src/test/java/tech/ydb/query/tools/QueryReaderTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -142,11 +142,11 @@ public void compositeResultSetTest() {
142142
rsr.setRowIndex(5);
143143
Assert.assertEquals(1, readAll(rsr, 5));
144144

145-
rsr.setRowIndex(6); // after end
146-
Assert.assertEquals(0, readAll(rsr, 0));
145+
IndexOutOfBoundsException ex1 = Assert.assertThrows(IndexOutOfBoundsException.class, () -> rsr.setRowIndex(6));
146+
Assert.assertEquals("Index 6 out of bounds for length 6", ex1.getMessage());
147147

148-
rsr.setRowIndex(-1); // before start
149-
Assert.assertEquals(6, readAll(rsr, 0));
148+
IndexOutOfBoundsException ex2 = Assert.assertThrows(IndexOutOfBoundsException.class, () -> rsr.setRowIndex(-1));
149+
Assert.assertEquals("Index -1 out of bounds for length 6", ex2.getMessage());
150150
}
151151

152152
private int readAll(ResultSetReader rsr, int startKey) {

0 commit comments

Comments
 (0)