Skip to content

fix: BinaryReader 64-bit endianness + base-class generic#20

Merged
tinysec merged 1 commit into
masterfrom
fix/binaryreader-endian-and-base-class
Jul 8, 2026
Merged

fix: BinaryReader 64-bit endianness + base-class generic#20
tinysec merged 1 commit into
masterfrom
fix/binaryreader-endian-and-base-class

Conversation

@tinysec

@tinysec tinysec commented Jul 8, 2026

Copy link
Copy Markdown
Owner

Summary

Two correctness bugs in BinaryReader, surfaced by the parity audit.

1. 64-bit reads ignored the reader's Endianness

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. (Python's read64BNRead64.)

2. Wrong base-class generic broke equality

Copy-paste typo: BinaryReader : 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__.

Verification

  • Binding builds clean (0 warnings, 0 errors).
  • New E2E tests: ReadInt64/ReadUInt64 now honour Endianness.LittleEndian vs BigEndian (computed independently from the raw bytes); EqualsObjectOnSelfReturnsTrue proves the base-class generic is now BinaryReader.

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__.
@tinysec tinysec merged commit ba882a0 into master Jul 8, 2026
1 check passed
@tinysec tinysec deleted the fix/binaryreader-endian-and-base-class branch July 8, 2026 05:18
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.

1 participant