-
Notifications
You must be signed in to change notification settings - Fork 1.7k
[LANG-1772] Restrict size of cache to prevent overflow errors #1379
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[LANG-1772] Restrict size of cache to prevent overflow errors #1379
Conversation
The problem is caused by an integer overflow of
A simpler solution would be to:
|
The test as is blows up GitHub builds so let's use something like |
…ngs method in all other profiles.
I considered that, changing |
Hi @ppkarwasz You've proposed an alternative solution. Would you shows in a PR? |
I'll submit a PR by the end of the week. |
…nside the CachedRandomBits constructor - also checking if the padding produces overflow. No longer using an arbitrary value but being more precise.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jcwinters
Thank you for your update.
I think the test should be more of a white box test and test just below and above the overflow. WDYT? @ppkarwasz ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks OK to me.
I don’t think we need to preemptively generate more than 256 MiB of random data. The goal of generating data in bulk is to take advantage of the fact that random number generators are typically more efficient when producing large chunks of data rather than individual bytes. However, I suspect the optimal amount is significantly less than 256 MiB—we should run some benchmarks to determine the best value.
@jcwinters |
|
* The maximum size of the cache. | ||
* | ||
* <p> | ||
* This is dictated by the {@code if (bitIndex >> 3 >= cache.length)} in the {@link #nextBits(int)} method. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If the 3
in this expression MUST match the 3
in the expression building cacheSize
in the random(...)
method, then it should be refactored from a magic number in to a constant IMO.
This makes me wonder about the other magic numbers 5
and 10
which beg for documentation if only to help with maintenance.
WDYT?
Hi all, Where are we on this one? @ppkarwasz do you still plan on providing an alternative solution? |
This pull request appears to be nearly ready. It includes the following changes:
I could switch the |
…ng the result to MAX_INT/5, also restricting max cache length to MAX_INT/3, there are now no opportunities for overflow. The test checks at the boundary condition
I believe I've now incorporated most of the suggestions (I didn't use constants for the divide by 3 for instance) - sorry for the length of time this has taken |
Hi @jcwinters, No need to apologize, we're all busy 😃 |
Hi @jcwinters |
…ve documentation around the nextBits method and the size allocation for the cache
I think I'm there now |
You're right, should be outside the min Co-authored-by: Piotr P. Karwasz <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for diligently addressing all of my feedback — I have no further suggestions.
As far as I’m concerned, the only remaining task is to add a changelog entry to src/changes/changes.xml
.
@garydgregory, have your concerns been addressed as well? If so, I believe we're ready to merge this PR.
https://issues.apache.org/jira/browse/LANG-1772
Added a length restriction to
RandomStringutils
, limiting the cache to 60M entries. Because of rejections the bitIndex in the underling cache can overflow when right shifting. Also added a test to verify the fix.This test takes quite a while to run, so if necessary I can create a profile for slow tests to exclude the test from the normal build.