fix: avoid okio resize overflow for files larger than 2 GB#140
Merged
Conversation
PathFileAccessor.preallocate() called okio's FileHandle.resize() to grow the file to the target size. okio 3.16.4 implements that grow path by allocating a ByteArray((size - current).toInt()) and writing it whole, which overflows Int and throws NegativeArraySizeException once the delta exceeds 2 GB. The reported failure was preallocate(2_918_598_656) for an Ubuntu server ISO. Switch to extending the file with a single sparse-byte write at (size - 1) when growing, mirroring ContentUriFileAccessor's approach. This also avoids the gratuitous multi-GB byte-array allocation on the happy path. Shrinks still use resize() (okio's shrink path uses RandomAccessFile.setLength and is safe). Adds a regression test that preallocates ~2.72 GB and asserts the file ends at the requested size.
Contributor
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Fixes a
NegativeArraySizeExceptionwhen starting a download larger than2 GB (reported on a 2.72 GB Ubuntu server ISO —
preallocate(2_918_598_656)).Root cause
PathFileAccessor.preallocate()called okio'sFileHandle.resize()togrow the file to the target size. okio 3.16.4 (
JvmFileHandle.kt:30)implements grow as:
For a 2.72 GB grow,
delta.toInt()overflows to-1376368640, throwingNegativeArraySizeException(-1376368640)— the exact value seen in thecrash log. Beyond the overflow, allocating a multi-GB byte array purely
to preallocate disk space is wasteful even when it doesn't crash.
iOS isn't affected — okio's
UnixFileHandle.protectedResizeusesftruncate(), which takes aLongand doesn't allocate.Fix
Extend the file with a single sparse-byte write at
(size - 1)whengrowing — same approach already used in
ContentUriFileAccessor.preallocate.The filesystem allocates space lazily as ranges are written. Shrinks
continue to use
resize()(okio's shrink path usesRandomAccessFile.setLengthand is safe).
Test Plan
PathFileAccessorPreallocateTest:preallocate_smallFile— sanity checkpreallocate_above2GB_doesNotOverflow— fails onmain, passes herepreallocate_zero_isNoOplibrary:core:jvmTesttests still passiosArm64,js,wasmWasi) still works