Skip to content

Commit 98b04a9

Browse files
authored
Merge pull request #2329 from adamretter/hotfix/xq-array-oobe-4.x.x
(4.x.x) Fix an ArrayOutOfBoundException with array:get
2 parents b228d7b + 437664e commit 98b04a9

File tree

3 files changed

+63
-4
lines changed

3 files changed

+63
-4
lines changed

src/org/exist/xquery/functions/array/ArrayFunction.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathExce
249249
return new IntegerValue(array.getSize());
250250
case GET:
251251
final IntegerValue index = (IntegerValue) args[1].itemAt(0);
252-
return array.get(index.getInt() - 1);
252+
return array.get(index);
253253
case APPEND:
254254
return array.append(args[1]);
255255
case HEAD:

src/org/exist/xquery/functions/array/ArrayType.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,15 @@ public Sequence get(int n) {
6868
}
6969

7070
@Override
71-
public Sequence get(AtomicValue key) throws XPathException {
71+
public Sequence get(final AtomicValue key) throws XPathException {
7272
if (!Type.subTypeOf(key.getType(), Type.INTEGER)) {
73-
throw new XPathException(ErrorCodes.XPTY0004, "position argument for array lookup must be a positive integer");
73+
throw new XPathException(ErrorCodes.XPTY0004, "Position argument for array lookup must be a positive integer");
7474
}
7575
final int pos = ((IntegerValue)key).getInt();
7676
if (pos <= 0 || pos > getSize()) {
77-
throw new XPathException(ErrorCodes.XPTY0004, "position argument for array lookup must be > 0 and < array:size");
77+
final String startIdx = vector.length() == 0 ? "0" : "1";
78+
final String endIdx = String.valueOf(vector.length());
79+
throw new XPathException(ErrorCodes.FOAY0001, "Array index " + pos + " out of bounds (" + startIdx + ".." + endIdx + ")");
7880
}
7981
return get(pos - 1);
8082
}

test/src/xquery/arrays/arrays.xql

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,63 @@ function arr:function-item-invalid() {
134134
$a("x")
135135
};
136136

137+
declare
138+
%test:assertError("FOAY0001")
139+
function arr:get-item-out-of-bounds-positive() {
140+
let $a := [13, 10, 14]
141+
return
142+
array:get($a, 4)
143+
};
144+
145+
declare
146+
%test:assertError("FOAY0001")
147+
function arr:get-item-out-of-bounds-positive-large() {
148+
let $a := [13, 10, 14]
149+
return
150+
array:get($a, 22)
151+
};
152+
153+
declare
154+
%test:assertError("FOAY0001")
155+
function arr:get-item-out-of-bounds-zero() {
156+
let $a := [13, 10, 14]
157+
return
158+
array:get($a, 0)
159+
};
160+
161+
declare
162+
%test:assertError("FOAY0001")
163+
function arr:get-item-out-of-bounds-negative() {
164+
let $a := [13, 10, 14]
165+
return
166+
array:get($a, -1)
167+
};
168+
169+
declare
170+
%test:pending("eXist-db does not correctly detect the type error. This is likely a larger issue than just arrays.")
171+
%test:assertError("XPTY0004")
172+
function arr:get-invalid-type() {
173+
let $a := [13, 10, 14]
174+
return
175+
array:get($a, xs:double(0.1))
176+
};
177+
178+
declare
179+
%test:assertEquals(13)
180+
function arr:get-first() {
181+
let $a := [13, 10, 14]
182+
return
183+
array:get($a, 1)
184+
};
185+
186+
declare
187+
%test:assertEquals(14)
188+
function arr:get-last() {
189+
let $a := [13, 10, 14]
190+
return
191+
array:get($a, 3)
192+
};
193+
137194
declare
138195
%test:args(1)
139196
%test:assertEmpty

0 commit comments

Comments
 (0)