Skip to content

Safe ByteBuffer array extraction#2892

Merged
adamw merged 8 commits into
masterfrom
safe-bytebuffer-array-extraction
May 26, 2026
Merged

Safe ByteBuffer array extraction#2892
adamw merged 8 commits into
masterfrom
safe-bytebuffer-array-extraction

Conversation

@adamw
Copy link
Copy Markdown
Member

@adamw adamw commented May 25, 2026

Closes #2880

Summary

  • Add a safe byteBufferToArray helper in sttp.client4.internal that correctly handles direct, read-only, partially-consumed, and sliced ByteBuffers (preserves position/limit). For a fresh, full heap buffer the backing array is returned directly (no copy).
  • Replace all unsafe ByteBuffer.array() calls across the codebase with the shared helper:
    • DigestAuthenticator.calculateHa2 (auth-int)
    • testing.forceBodyAsString / forceBodyAsByteArray
    • internal.enqueueBytes
    • Compressor / defaultCompressors
    • BodyToHttpClient
    • MultipartBodyBuilder (both non-stream and stream variants)
    • FinagleBackend
    • AbstractFetchBackend
  • Remove duplicate Compressor.byteBufferToArray and AbstractFetchBackend.byteBufferToArray
  • Remove now-unused ByteBufferBackedInputStream

Test plan

  • ByteBufferToArrayTest — 7 cases covering direct, read-only, partial, sliced buffers, fast-path optimization, aliasing, and position preservation
  • DigestAuthenticatorTest — auth-int digest with direct and partial byte buffers
  • EnqueueBytesTest — direct and partial byte buffers
  • ForceBodyAsTestforceBodyAsString and forceBodyAsByteArray with direct and partial buffers
  • All Scala 3 / JVM tests pass

🤖 Generated with Claude Code

adamw and others added 8 commits May 25, 2026 08:17
The old Compressor.byteBufferToArray had two bugs: the hasArray branch
ignored position/limit, and the else branch mutated the buffer's position.
Replace it with the safe internal.byteBufferToArray helper.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
When the buffer has a backing array with offset 0, position 0, and
remaining == array length, return the backing array directly (no copy).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Replace unsafe ByteBuffer.array() calls in BodyToHttpClient,
MultipartBodyBuilder, FinagleBackend, and AbstractFetchBackend with the
shared internal.byteBufferToArray helper. Remove the now-unused
ByteBufferBackedInputStream class.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
…alizing

Use ByteBufferBackedInputStream(b.duplicate()) to stream ByteBuffer
content lazily, avoiding eager materialization of potentially large
buffers. The duplicate() preserves the original buffer's position.

Also remove stale .claude/orca-system-prompt.md.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@adamw adamw merged commit 29b759b into master May 26, 2026
27 checks passed
@adamw adamw deleted the safe-bytebuffer-array-extraction branch May 26, 2026 14:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Several ByteBuffer.array() calls don't check hasArray() and ignore position/limit

1 participant