alhudz opened a new pull request, #1843:
URL: https://github.com/apache/commons-lang/pull/1843

   `RandomStringUtils.random(int, int, int, boolean, boolean, char[], Random)` 
bounds consecutive rejected draws with `Math.max(64, gap * 10)`, added in #1638 
so that a range with no acceptable value throws instead of hanging. Each draw 
takes `gapBits` random bits, so a valid range whose acceptable values are 
sparse accepts a draw with probability as low as `1 / 2^gapBits`, and the cap 
fires on input the method's own up-front validation has already accepted.
   
   Repro: `chars` is 63 `'-'` plus one `'x'`, then 
`RandomStringUtils.random(100, 0, 64, true, false, chars, new Random(seed))`.
   Expected: `"xxx...x"` (100 chars).
   Actual: `gapBits` is 7, so a draw is accepted with probability 1/128 against 
a cap of 640; 90 of 200 calls threw `IllegalArgumentException: No acceptable 
code points found in range [0, 64) within 640 attempts.` The `secure()` / 
`insecure()` / `secureStrong()` `next(...)` overloads and the static `random*` 
methods go through the same loop.
   Cause: the cap is sized by `gap`, but the rejection rate depends on the 
`2^gapBits` sample space.
   Fix: cap at `64L << gapBits` (and a `long` counter, so a large `chars` array 
cannot overflow it). A range with at least one acceptable value now exhausts it 
with probability below `e^-64` per character. A range with no acceptable value 
still throws: `testOnlyRejectedCodePoints` (`[0xE000, 0xF900)`) throws after 
524,288 draws in about 80 ms, and the widest all-rejected range I could build, 
`[0xE01F0, 0x10FFFF)`, throws in about 0.75 s.
   
   `testSparseAcceptableValuesDoNotTripRejectionCap` fails before the change 
and passes after. The reproducer goes from 90/200 failures to 0/200.
   
   
   
   - [x] Read the [contribution guidelines](CONTRIBUTING.md) for this project.
   - [x] Read the [ASF Generative Tooling 
Guidance](https://www.apache.org/legal/generative-tooling.html) if you use 
Artificial Intelligence (AI).
   - [x] I used AI to create any part of, or all of, this pull request. Which 
AI tool was used to create this pull request, and to what extent did it 
contribute? Claude Code (Anthropic) found the defect and drafted the patch, the 
test and this description; I reviewed all of it before submitting.
   - [x] Run a successful build using the default 
[Maven](https://maven.apache.org/) goal with `mvn`; that's `mvn` on the command 
line by itself.
   - [x] Write unit tests that match behavioral changes, where the tests fail 
if the changes to the runtime are not applied. This may not always be possible, 
but it is a best practice.
   - [x] Write a pull request description that is detailed enough to understand 
what the pull request does, how, and why.
   - [x] Each commit in the pull request should have a meaningful subject line 
and body. Note that a maintainer may squash commits during the merge process.
   


-- 
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