-
Notifications
You must be signed in to change notification settings - Fork 665
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
Open
bernardladenthin
wants to merge
2
commits into
Kotlin:dev
Choose a base branch
from
bernardladenthin:dev
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -43,12 +43,18 @@ internal class ByteArrayOutput { | |||||||
| private var position: Int = 0 | ||||||||
|
|
||||||||
| private fun ensureCapacity(elementsToAppend: Int) { | ||||||||
| if (position + elementsToAppend <= array.size) { | ||||||||
| val requiredCapacityLong = position.toLong() + elementsToAppend.toLong() | ||||||||
| if (requiredCapacityLong > Int.MAX_VALUE) { | ||||||||
| throw IllegalArgumentException("Required capacity exceeds maximum array size (Int.MAX_VALUE).") | ||||||||
| } | ||||||||
|
|
||||||||
| val requiredCapacity = requiredCapacityLong.toInt() | ||||||||
| if (requiredCapacity <= array.size) { | ||||||||
| return | ||||||||
| } | ||||||||
| val newArray = ByteArray((position + elementsToAppend).takeHighestOneBit() shl 1) | ||||||||
| array.copyInto(newArray) | ||||||||
| array = newArray | ||||||||
|
|
||||||||
| val newCapacity = nextPowerOfTwoCapacity(requiredCapacity) | ||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
It seems like the only reason to have a dedicated function is to test it, but since the function is trivial, I would rather embed it here, and simplify this expression (note that we don't have to cast |
||||||||
| array = array.copyOf(newCapacity) | ||||||||
| } | ||||||||
|
|
||||||||
| public fun toByteArray(): ByteArray { | ||||||||
|
|
@@ -86,4 +92,29 @@ internal class ByteArrayOutput { | |||||||
| ensureCapacity(1) | ||||||||
| array[position++] = byteValue.toByte() | ||||||||
| } | ||||||||
|
|
||||||||
| companion object { | ||||||||
| /** | ||||||||
| * Calculates the next power-of-two capacity based on the required minimum size. | ||||||||
| * | ||||||||
| * This function ensures the returned value is at least as large as `minCapacity`, | ||||||||
| * and is always a power of two, unless `minCapacity` is less than or equal to zero, | ||||||||
| * in which case it returns 0. If the calculated power of two exceeds `Integer.MAX_VALUE`, | ||||||||
| * it returns `Integer.MAX_VALUE`. | ||||||||
| * | ||||||||
| * It's useful for resizing arrays with exponential growth. | ||||||||
| * | ||||||||
| * @param minCapacity The minimum required capacity. | ||||||||
| * @return A capacity value that is a power of two and ≥ minCapacity, or 0 if `minCapacity` is ≤ 0. | ||||||||
| */ | ||||||||
| fun nextPowerOfTwoCapacity(minCapacity: Int): Int { | ||||||||
| if (minCapacity <= 0) return 0 | ||||||||
|
|
||||||||
| val highestOneBit = minCapacity.takeHighestOneBit() | ||||||||
| val maxHighestOneBit = Integer.MAX_VALUE.takeHighestOneBit() | ||||||||
|
|
||||||||
| // Check if shifting would exceed the maximum allowed value | ||||||||
| return if (highestOneBit < maxHighestOneBit) highestOneBit shl 1 else Integer.MAX_VALUE | ||||||||
| } | ||||||||
| } | ||||||||
| } | ||||||||
57 changes: 57 additions & 0 deletions
57
formats/cbor/commonTest/src/kotlinx/serialization/cbor/internal/StreamsTest.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| package kotlinx.serialization.cbor.internal | ||
|
|
||
| import kotlinx.serialization.* | ||
| import kotlin.test.* | ||
|
|
||
| class StreamsTest { | ||
|
|
||
| @Test | ||
| fun powerOfTwoCapacity_negativeValue() { | ||
| assertEquals(0, ByteArrayOutput.nextPowerOfTwoCapacity(-1)) | ||
| assertEquals(0, ByteArrayOutput.nextPowerOfTwoCapacity(-17)) | ||
| } | ||
|
|
||
| @Test | ||
| fun powerOfTwoCapacity_zeroValue() { | ||
| assertEquals(0, ByteArrayOutput.nextPowerOfTwoCapacity(0)) | ||
| } | ||
|
|
||
| @Test | ||
| fun powerOfTwoCapacity_exactPowerOfTwo() { | ||
| assertEquals(16, ByteArrayOutput.nextPowerOfTwoCapacity(8)) | ||
| assertEquals(32, ByteArrayOutput.nextPowerOfTwoCapacity(16)) | ||
| assertEquals(64, ByteArrayOutput.nextPowerOfTwoCapacity(32)) | ||
| } | ||
|
|
||
| @Test | ||
| fun powerOfTwoCapacity_nonPowerOfTwo() { | ||
| assertEquals(16, ByteArrayOutput.nextPowerOfTwoCapacity(9)) | ||
| assertEquals(64, ByteArrayOutput.nextPowerOfTwoCapacity(33)) | ||
| assertEquals(128, ByteArrayOutput.nextPowerOfTwoCapacity(65)) | ||
| } | ||
|
|
||
| @Test | ||
| fun powerOfTwoCapacity_smallValues() { | ||
| assertEquals(2, ByteArrayOutput.nextPowerOfTwoCapacity(1)) | ||
| assertEquals(4, ByteArrayOutput.nextPowerOfTwoCapacity(2)) | ||
| assertEquals(4, ByteArrayOutput.nextPowerOfTwoCapacity(3)) | ||
| } | ||
|
|
||
| @Test | ||
| fun powerOfTwoCapacity_boundaryValues() { | ||
| assertEquals(0, ByteArrayOutput.nextPowerOfTwoCapacity(0)) | ||
| assertEquals(2, ByteArrayOutput.nextPowerOfTwoCapacity(1)) | ||
| assertEquals(4, ByteArrayOutput.nextPowerOfTwoCapacity(3)) | ||
| assertEquals(8, ByteArrayOutput.nextPowerOfTwoCapacity(5)) | ||
| } | ||
|
|
||
| @Test | ||
| fun powerOfTwoCapacity_largeValues() { | ||
| assertEquals(1073741824, ByteArrayOutput.nextPowerOfTwoCapacity(536870912)) | ||
| assertEquals(1073741824, ByteArrayOutput.nextPowerOfTwoCapacity(1073741823)) | ||
| assertEquals(Integer.MAX_VALUE, ByteArrayOutput.nextPowerOfTwoCapacity(1073741824)) | ||
| assertEquals(Integer.MAX_VALUE, ByteArrayOutput.nextPowerOfTwoCapacity(1073741825)) | ||
| assertEquals(Integer.MAX_VALUE, ByteArrayOutput.nextPowerOfTwoCapacity(Integer.MAX_VALUE-1)) | ||
| assertEquals(Integer.MAX_VALUE, ByteArrayOutput.nextPowerOfTwoCapacity(Integer.MAX_VALUE)) | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
It's better to:
elementsToAppendvalue to the error message too, as it might help to debug an issue, when someone will have to)SerializationExceptionto depict that it's a problem with serializing some particular object, and it's not some internal assertion being failed.