Add bounds validation to ArrayUtil.growExact() and CharTermAttributeImpl.clone()#16345
Add bounds validation to ArrayUtil.growExact() and CharTermAttributeImpl.clone()#16345vik-hashira wants to merge 3 commits into
Conversation
Adds bounds checking to all growExact() methods to prevent: - Negative newLength values - newLength less than array.length (maintains backward compatibility with IndexOutOfBoundsException) - newLength exceeding MAX_ARRAY_LENGTH This fixes potential security issues where malicious input could cause excessive memory allocation or undefined behavior.
fa7979c to
d02e3f5
Compare
Adds bounds checking in clone() method to validate termLength: - Checks termLength is not negative - Checks termLength does not exceed MAX_ARRAY_LENGTH This prevents potential security issues with crafted/malicious TermBuffer values that could cause excessive memory allocation or undefined behavior during cloning.
d02e3f5 to
3c0d0e6
Compare
|
not vulnerabilities |
- Remove OOM-causing test that allocated MAX_ARRAY_LENGTH - Update CHANGES.txt to reference new bug issue apache#16346 - Rephrase CHANGES.txt entry as bug fix (not security) Co-Authored-By: Claude <noreply@anthropic.com>
|
Thank you for the feedback! I've updated this PR to frame it as a bug fix rather than a security vulnerability:
The validation changes remain the same - they add proper bounds checking with clear error messages for invalid input values. This improves robustness and provides better error messages for edge cases. |
| throw new IllegalStateException( | ||
| "termLength (" + this.termLength + ") exceeds maximum allowed array length"); | ||
| } | ||
| t.termBuffer = new char[this.termLength]; |
There was a problem hiding this comment.
this isn't needed. java's bounds checking already does this.
|
i dont agree with the changes, we don't need such additional bounds checks, java already has them. this isn't C. |
|
Thank you for the feedback @rmuir. You're right - Java already provides bounds checking for array operations, and these additional checks are redundant. I appreciate you taking the time to review this. Closing this PR as the changes are not needed. |
Fixes #16346
Summary
This PR adds bounds validation to
ArrayUtil.growExact()methods andCharTermAttributeImpl.clone()to reject invalid input values with clear error messages.Changes
ArrayUtil.growExact()
checkGrowExactLength()for centralized validationnewLength >= 0(throwsIllegalArgumentException)newLength >= array.length(throwsIndexOutOfBoundsExceptionfor backward compatibility)newLength <= MAX_ARRAY_LENGTH(throwsIllegalArgumentException)CharTermAttributeImpl.clone()
termLengthbefore array allocationtermLength >= 0(throwsIllegalStateException)termLength <= ArrayUtil.MAX_ARRAY_LENGTH(throwsIllegalStateException)Backward Compatibility
newLengthvalues will now receive clear error messages instead of undefined behaviorIndexOutOfBoundsExceptionmaintained fornewLength < array.lengthto preserve existing test expectationsTesting
testGrowExactBoundsValidation()to verify validation works correctlytestGrowExacttests continue to pass