fix: BinaryReader 64-bit endianness + base-class generic#20
Merged
Conversation
Two correctness bugs in BinaryReader surfaced by the parity audit: 1. ReadInt64/ReadUInt64 hardcoded BNReadLE64 (little-endian) instead of the endian-agnostic BNRead64, so a BinaryReader whose Endianness is set to BigEndian silently read little-endian 64-bit values. The 8/16/32-bit siblings and the explicit *LE/*BE variants are all correct; only the two endian-agnostic 64-bit readers were wrong. Mirrors Python's read64. 2. Copy-paste typo: BinaryReader derived from AbstractSafeHandle<Architecture> instead of <BinaryReader> (BinaryWriter correctly uses <BinaryWriter>). AbstractSafeHandle<T_SELF> implements IEquatable/IComparable by casting `other as T_SELF`, so two BinaryReader wrappers never compared equal and BinaryReader was not IEquatable<BinaryReader> — diverging from Python's handle-address __eq__/__hash__.
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.
Summary
Two correctness bugs in
BinaryReader, surfaced by the parity audit.1. 64-bit reads ignored the reader's
EndiannessReadInt64/ReadUInt64hardcodedBNReadLE64(little-endian) instead of the endian-agnosticBNRead64, so aBinaryReaderwhoseEndiannessis set toBigEndiansilently read little-endian 64-bit values. The 8/16/32-bit siblings and the explicit*LE/*BEvariants are all correct — only the two endian-agnostic 64-bit readers were wrong. (Python'sread64→BNRead64.)2. Wrong base-class generic broke equality
Copy-paste typo:
BinaryReader : AbstractSafeHandle<Architecture>instead of<BinaryReader>(BinaryWritercorrectly uses<BinaryWriter>).AbstractSafeHandle<T_SELF>implementsIEquatable/IComparableby castingother as T_SELF, so twoBinaryReaderwrappers never compared equal andBinaryReaderwas notIEquatable<BinaryReader>— diverging from Python's handle-address__eq__/__hash__.Verification
ReadInt64/ReadUInt64now honourEndianness.LittleEndianvsBigEndian(computed independently from the raw bytes);EqualsObjectOnSelfReturnsTrueproves the base-class generic is nowBinaryReader.