Skip to content

MINOR: Preserve ByteBuffer order in little-endian reads - #3702

Open
fallintoplace wants to merge 1 commit into
apache:masterfrom
fallintoplace:fix-binary-little-endian-buffer-order
Open

MINOR: Preserve ByteBuffer order in little-endian reads#3702
fallintoplace wants to merge 1 commit into
apache:masterfrom
fallintoplace:fix-binary-little-endian-buffer-order

Conversation

@fallintoplace

Copy link
Copy Markdown

Rationale for this change

ByteBufferBackedBinary.get2BytesLittleEndian() currently changes the byte order of its backing ByteBuffer to little endian. Because the buffer can be caller-owned, reading the binary can unexpectedly affect subsequent reads through the original buffer.

What changes are included in this PR?

Read the short through a duplicate buffer so the original buffer order remains unchanged.

Are these changes tested?

Yes. The regression test verifies the returned value and preserves the original big-endian order for heap, direct, and read-only buffers. The complete TestBinary class and Spotless check pass.

Are there any user-facing changes?

Calling get2BytesLittleEndian() no longer changes the order of the caller-owned backing buffer.

@divjotarora divjotarora left a comment

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.

Nice catch! Seems this is a tricky area of the code, several other bugs due to not calling duplicate() were recently fixed 😬

}

return value.order(ByteOrder.LITTLE_ENDIAN).getShort(offset);
return value.duplicate().order(ByteOrder.LITTLE_ENDIAN).getShort(offset);

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.

Can we do something similar as ByteArraySliceBackedBinary and avoid duplicating?

return (short) (((value.get(offset + 1) & 0xff) << 8) | (value.get(offset) & 0xff));

@fallintoplace
fallintoplace force-pushed the fix-binary-little-endian-buffer-order branch from f0b0cb8 to 9e70166 Compare August 25, 2026 21:45
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.

2 participants