Skip to content

API: Binary truncate overflows with a positioned buffer and large width #18113

Description

@laserninja

Apache Iceberg version

main (development), commit c4784768b

Query engine

Other: Java API

Please describe the bug

A valid binary partition transform throws for an input buffer with a nonzero position when the width is sufficiently large.

ByteBuffer input = ByteBuffer.wrap(new byte[] {0, 1, 2});
input.position(1);
Transforms.<ByteBuffer>truncate(Integer.MAX_VALUE)
    .bind(Types.BinaryType.get()).apply(input);

Expected: preserve the remaining bytes [1, 2], because the width exceeds the remaining length.
Actual: IllegalArgumentException: newLimit < 0: (-2147483648 < 0).

The same exception occurs while computing a PartitionKey for a binary column. BinaryUtil.truncateBinaryUnsafe adds position to width before clamping to the limit, so the addition overflows.

Proposed fix: clamp width to remaining bytes before adding the position. Preserve the input state and the returned buffer's shared backing storage.

Willingness to contribute

  • I can contribute a fix for this bug independently

This report and its reproduction were prepared with AI assistance and verified locally.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions