Skip to content

GH-3471: Fix ByteBuffer handling in VariantUtil and VariantBuilder#3472

Open
rayokota wants to merge 1 commit intoapache:masterfrom
rayokota:fix-variant
Open

GH-3471: Fix ByteBuffer handling in VariantUtil and VariantBuilder#3472
rayokota wants to merge 1 commit intoapache:masterfrom
rayokota:fix-variant

Conversation

@rayokota
Copy link
Copy Markdown

@rayokota rayokota commented Apr 11, 2026

Rationale for this change

Fix ByteBuffer handling in VariantUtil and VariantBuilder.

What changes are included in this PR?

  • Fix VariantUtil.getMetadataMap reading string bytes from the wrong position
    when the metadata ByteBuffer has a non-zero position(). Changed
    slice(metadata, stringStart + offset) to
    slice(metadata, pos + stringStart + offset) to match the array-backed branch.
  • Fix VariantBuilder.appendBinary mutating the caller's ByteBuffer by using
    binary.duplicate(), consistent with appendEncodedValue.

Are these changes tested?

  • Added TestVariantObject.testMetadataWithNonZeroPositionReadOnly: constructs a
    metadata ByteBuffer with a non-zero position and read-only flag, then verifies
    ImmutableMetadata (which calls getMetadataMap) correctly parses the
    dictionary. Fails without the fix.
  • Added TestVariantScalarBuilder.testBinaryBuilderDoesNotMutateCallerBuffer:
    verifies that appendBinary does not change the caller's ByteBuffer position
    or remaining count.

Are there any user-facing changes?

  • No

Closes #3471

@nssalian
Copy link
Copy Markdown

CC: @alamb to help kick off test workflows.

Copy link
Copy Markdown
Contributor

@steveloughran steveloughran left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is good. does relate to the work in #3481 which needs to pick up the same fix.
getting your patch in first adds the regression tests we need

int remainingBefore = buf.remaining();
VariantBuilder vb = new VariantBuilder();
vb.appendBinary(buf);
Assert.assertEquals(positionBefore, buf.position());
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit add a "position" and "remaining" description for better diagnostics on CI runs

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.

VariantUtil.getMetadataMap reads from wrong position for non-array ByteBuffer; VariantBuilder.appendBinary mutates caller's ByteBuffer

3 participants