Fix LTF8 9-byte write bug: wrong bit shift (>> 28 instead of >> 24)#1765
Merged
Fix LTF8 9-byte write bug: wrong bit shift (>> 28 instead of >> 24)#1765
Conversation
The writeUnsignedLTF8(long, OutputStream) method incorrectly shifts by 28 bits instead of 24 bits when encoding byte 5 of a 9-byte LTF8 value. This causes data corruption for values requiring the full 9-byte encoding (values >= 2^56 with certain bit patterns). The 9-byte encoding should write bytes at shifts: 56, 48, 40, 32, 24, 16, 8, 0 but the code wrote: 56, 48, 40, 32, 28, 16, 8, 0 The corresponding read path (readUnsignedLTF8) correctly uses << 24, so the reader and writer disagreed for affected values. The correct behavior is confirmed by htslib's reference implementation: https://github.com/samtools/htslib/blob/master/cram/cram_io.c#L364-L373 Existing tests did not catch this because their 9-byte test values (-4757, Long.MAX_VALUE, -1) have identical nibbles at bit positions 28 and 24, masking the bug. A targeted test with value 0x0123456789ABCDEF is added to exercise the difference.
yfarjoun
requested changes
Mar 31, 2026
Contributor
yfarjoun
left a comment
There was a problem hiding this comment.
asking for a test without a leading zero
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.
The writeUnsignedLTF8(long, OutputStream) method incorrectly shifts by 28 bits instead of 24 bits when encoding byte 5 of a 9-byte LTF8 value. This causes data corruption for values requiring the full 9-byte encoding (values >= 2^56 with certain bit patterns).
The 9-byte encoding should write bytes at shifts:
56, 48, 40, 32, 24, 16, 8, 0
but the code wrote:
56, 48, 40, 32, 28, 16, 8, 0
The corresponding read path (readUnsignedLTF8) correctly uses << 24, so the reader and writer disagreed for affected values.
The correct behavior is confirmed by htslib's reference implementation:
https://github.com/samtools/htslib/blob/master/cram/cram_io.c#L364-L373
Existing tests did not catch this because their 9-byte test values (-4757, Long.MAX_VALUE, -1) have identical nibbles at bit positions 28 and 24, masking the bug. A targeted test with value 0x0123456789ABCDEF is added to exercise the difference.
Description
Please explain the changes you made here.
Explain the motivation for making this change. What existing problem does the pull request solve?
Things to think about before submitting: