Repository navigation
Validate BotMaker collection helpers on empty/out-of-range input - #218
Open
jainprayush9 wants to merge 1 commit into
Open
jainprayush9 wants to merge 1 commit into
jainprayush9 wants to merge 1 commit into
Conversation
Empty or out-of-range inputs to First/Last/FirstN/Slice/ElementAtIndex threw raw IndexOutOfBoundsException, which aborted scarecrow labeling rules. Surface typed FunctionFailure with clear messages instead. Fixes xai-org#207 Related to xai-org#58 Co-authored-by: Cursor <cursoragent@cursor.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
BotMaker collection helpers in
botmaker/.../function/collection/assumed valid inputs and called into the JDK without bounds checks:First()list.get(0)on empty →IndexOutOfBoundsExceptionLast()list.get(size - 1)on empty →get(-1)→IndexOutOfBoundsExceptionFirstN()n→subList(0, negative)→ crashSlice()/Substring()StringIndexOutOfBoundsException/IndexOutOfBoundsExceptionElementAtIndex()IndexOutOfBoundsExceptionFunctionNode*.toExtractor()wraps those exceptions inFunctionFailure, so the worker does not die — but the enclosing BotMaker rule does not complete. In the scarecrow flow that means the intended label is never applied. Whether the caller fails open or closed is outside this repo; either way, silent rule abortion is the wrong outcome for empty or malformed collections.This was reported for
First/FirstN/Slicein #58 and extended toLast()in #207 (same defect class, one function later in the file).Solution
Validate at the function boundary and raise a typed
FunctionFailurewith a clearIllegalArgumentExceptionmessage (same pattern used elsewhere in BotMaker, e.g.Anybatch-size checks):First/Last— rejectnullor empty lists before index accessFirstN— rejectnulllist; clampnwithMath.max(0, n)beforesubListSlice— validate non-null indices and0 ≤ begin ≤ end ≤ lengthfor bothStringandListinputsElementAtIndex— reject null list / null index and out-of-range indices (same class of bug; included so the collection API is consistent)Happy paths are unchanged. Descriptors / signatures / examples are unchanged.
Files
botmaker/src/java/com/twitter/botmaker/function/collection/First.javabotmaker/src/java/com/twitter/botmaker/function/collection/Last.javabotmaker/src/java/com/twitter/botmaker/function/collection/FirstN.javabotmaker/src/java/com/twitter/botmaker/function/collection/Slice.javabotmaker/src/java/com/twitter/botmaker/function/collection/ElementAtIndex.javaTest plan
First(List())/Last(List())→FunctionFailurewith message mentioning empty listFirstN(list, -1)→ empty prefix (clamped), not a crashSlice/Substringwithbegin < 0,end < begin, orend > length→FunctionFailurewith bounds in the messageElementAtIndex(list, -1)andElementAtIndex(list, size)→FunctionFailureFirst(List(1)),Last(List(1, 2)),FirstN(List("a","b","c"), 2),Slice([1,2,3,4], 1, 2),ElementAtIndex(List("hello", 42), 0)References