-
Notifications
You must be signed in to change notification settings - Fork 641
Fix buffer resizing overflow and introduce safe power-of-two capacity growth #2985
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
base: dev
Are you sure you want to change the base?
Conversation
… growth - Previously, calculating next capacity for large values like 1_073_741_824 caused overflow and returned Integer.MIN_VALUE. - Rewrote `ensureCapacity` to use `Long` for arithmetic to guard against exceeding Int.MAX_VALUE. - Introduced `nextPowerOfTwoCapacity(minCapacity: Int)` to safely calculate the next power-of-two ≥ minCapacity. - Capped capacity at Int.MAX_VALUE to prevent buffer allocation errors. - Added comprehensive unit tests covering negative, small, exact, and large input values to validate correct capacity growth behavior.
The method under test ( |
As you are considering the case of very large buffers, you may want to consider that it is not actually possible to create an array of size 2^31 (MaxInt + 1), but only of size 2^31-1 (MaxInt). This maximum size is however not a power of 2. Meaning that you almost half the maximum supported size to 2^30 (or approx 1bln values instead of 2bln). Ps. yes there is a CI setup (using Jetbrains private infrastructure) |
Hi, and thanks for your comment! You're absolutely right — it's not a power of two. I tried to stay as close as possible to the existing implementation. At the moment, I'm running into an issue when serializing very large data sets. Specifically, when working with buffers larger than 1_073_741_824, the current implementation runs into an integer overflow, which results in the following exception:
In the future, it might be worth considering an alternative buffer implementation — ideally one that scales automatically and supports sizes beyond the current limitation. I've developed such a buffer myself: streambuffer, which could be a useful foundation for handling very large data more robustly. |
For this implementation it may be worthwhile to clip the range value to |
Right, my initial goal was to address the existing issue with the In the short term, it might be helpful to at least fix the issue up to Of course, a proper refactoring would still be ideal to fully support large data sets. |
ensureCapacity
to useLong
for arithmetic to guard against exceeding Int.MAX_VALUE.nextPowerOfTwoCapacity(minCapacity: Int)
to safely calculate the next power-of-two ≥ minCapacity.Note: You can compare the fixed behavior with the old logic using: