Copilot commented on code in PR #1703:
URL: https://github.com/apache/commons-lang/pull/1703#discussion_r3409490970


##########
src/main/java/org/apache/commons/lang3/RandomStringUtils.java:
##########
@@ -296,7 +296,9 @@ public static String random(int count, int start, int end, 
final boolean letters
             if (letters && digits && start <= ASCII_0 && end >= ASCII_z + 1) {
                 return random(count, 0, 0, false, false, ALPHANUMERICAL_CHARS, 
random);
             }
-            if (digits && end <= ASCII_0 || letters && end <= ASCII_A) {
+            // Only reject when none of the requested categories is reachable; 
otherwise a letters && digits
+            // request would throw on a range that holds one category but not 
the other (e.g. [ASCII_0, ASCII_A)).
+            if ((!digits || end <= ASCII_0) && (!letters || end <= ASCII_A) && 
(digits || letters)) {
                 throw new IllegalArgumentException(
                         String.format("Parameter end (%,d) must be greater 
than (%,d) for generating digits or greater than (%,d) for generating 
letters.", end,

Review Comment:
   The new ASCII fast-path guard is still based only on `end`, but whether 
letters/digits are reachable also depends on `start`. With `letters && digits` 
it’s possible for the later clamping (`start = max('0', start)`, `end = 
min('z'+1, end)`) to collapse the range to `start == end` (e.g. start >= 'z'+1 
while end <= 0x7f). That makes `gap == 0` and `gapBits == 0`, which then causes 
`CachedRandomBits.nextBits(0)` to throw an unrelated IllegalArgumentException 
("number of bits must be between 1 and 32") instead of a clear range-validation 
error.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to