Problem
Three collection functions in BotMaker lack input validation and crash on edge cases rather than
failing gracefully:
- First() crashes on empty lists → IndexOutOfBoundsException
- FirstN() lacks negative index check → May create invalid subLists
- Slice() doesn't validate beginIndex < endIndex → IllegalArgumentException
These are core utility functions used in rule logic and visibility filtering. Uncaught exceptions
break the entire evaluation pipeline.
Current State
First.java (lines 48-50)
public Object apply(Context<Runtime> context, List<Object> list) {
return list.get(0); // ❌ Crashes if list.isEmpty()
}
Error: IndexOutOfBoundsException if called with empty list.
FirstN.java (lines 56-58)
public Object apply(Context<Runtime> context, List<Object> list, Long n) {
return list.subList(0, Math.min(n.intValue(), list.size()));
// ❌ No validation: n could be negative, causing IllegalArgumentException
}
Error: IllegalArgumentException if n < 0 (subList requires non-negative indices).
Slice.java (lines 74-80)
protected Object apply(
Context<Runtime> context, Object input, Long beginIndex, Long endIndex) {
if (input instanceof String) {
return ((String) input).substring(beginIndex.intValue(), endIndex.intValue());
// ❌ No check: what if beginIndex > endIndex?
}
// ...
}
Error: StringIndexOutOfBoundsException or IllegalArgumentException if indices are invalid.
Impact
- Visibility filtering breaks when rules call these functions with malformed inputs
- Silent rule failure — exceptions propagate instead of being handled gracefully
- Unpredictable behavior — different errors for different collections
Proposed Solution
-
First(): Validate non-empty list or return null/default
public Object apply(Context<Runtime> context, List<Object> list) {
if (list.isEmpty()) {
throw new IllegalArgumentException("First() called on empty list");
}
return list.get(0);
}
-
FirstN(): Validate n >= 0
public Object apply(Context<Runtime> context, List<Object> list, Long n) {
long count = Math.max(0, n); // or throw if n < 0
return list.subList(0, (int) Math.min(count, list.size()));
}
-
Slice(): Validate indices before use
protected Object apply(Context<Runtime> context, Object input, Long begin, Long end) {
if (begin < 0 || end < 0 || begin > end) {
throw new IllegalArgumentException(
String.format("Invalid slice indices: begin=%d, end=%d", begin, end)
);
}
// ...
}
References
botmaker/src/java/com/twitter/botmaker/function/collection/First.java (line 49)
botmaker/src/java/com/twitter/botmaker/function/collection/FirstN.java (line 57)
botmaker/src/java/com/twitter/botmaker/function/collection/Slice.java (lines 74-80)
Problem
Three collection functions in BotMaker lack input validation and crash on edge cases rather than
failing gracefully:
These are core utility functions used in rule logic and visibility filtering. Uncaught exceptions
break the entire evaluation pipeline.
Current State
First.java (lines 48-50)
Error:
IndexOutOfBoundsExceptionif called with empty list.FirstN.java (lines 56-58)
Error:
IllegalArgumentExceptionifn < 0(subList requires non-negative indices).Slice.java (lines 74-80)
Error:
StringIndexOutOfBoundsExceptionorIllegalArgumentExceptionif indices are invalid.Impact
Proposed Solution
First(): Validate non-empty list or return null/default
FirstN(): Validate n >= 0
Slice(): Validate indices before use
References
botmaker/src/java/com/twitter/botmaker/function/collection/First.java(line 49)botmaker/src/java/com/twitter/botmaker/function/collection/FirstN.java(line 57)botmaker/src/java/com/twitter/botmaker/function/collection/Slice.java(lines 74-80)