[LANG-1801] Fix RandomStringUtils.random() does not strictly validate start/end when chars != null, causing potential IndexOutOfBoundsException#1521
Conversation
…chars != null, causing potential IndexOutOfBoundsException
|
Hi maintainers, I have prepared a fix to the problem of the method RandomStringUtils.random(), that:
Would you be open to accepting a pull request with this fix? Thank you for your time and for maintaining this excellent library! Best regards, |
…utOfBoundsException
|
Hello @theodoral22 |
|
Hi @garydgregory |
|
Hello @IcoreE |
| assertIllegalArgumentException(() -> RandomStringUtils.random(8, 32, 48, false, true)); | ||
| assertIllegalArgumentException(() -> RandomStringUtils.random(8, 32, 65, true, false)); | ||
| assertIllegalArgumentException(() -> RandomStringUtils.random(1, Integer.MIN_VALUE, -10, false, false, null)); | ||
| assertIllegalArgumentException(() -> RandomStringUtils.random(2, 5, 6, false, false, new char[] { 'a', 'b', 'c', 'd' }, new Random())); |
There was a problem hiding this comment.
The test should cover checks for end and start both separately and together.
Also should check that the correct messages are generated.
When a custom character array (chars != null) is supplied to RandomStringUtils.random(), the method does not strictly check that the start and end parameters fall within the valid bounds of the chars array.
As a result, if start or end exceeds chars.length, the method may generate a random index outside the array range, leading to an unexpected ArrayIndexOutOfBoundsException.
This fails the method contract and causes unpredictable runtime errors.
Actual:
Throws ArrayIndexOutOfBoundsException
Expected:
Throw IllegalArgumentException indicating invalid start/end range when chars != null
my issue @garydgregory