Skip to content

Fix: decode multi-byte varints correctly in get_prefix - #62

Open
sumanjeet0012 wants to merge 1 commit into
multiformats:masterfrom
sumanjeet0012:fix-issue-46
Open

sumanjeet0012 wants to merge 1 commit into
multiformats:masterfrom
sumanjeet0012:fix-issue-46

Conversation

@sumanjeet0012

@sumanjeet0012 sumanjeet0012 commented Jul 11, 2026 •

Copy link
Copy Markdown
Contributor

Description

Closes #46.

This pull request addresses issue, where get_prefix was returning an incomplete/incorrect prefix for hash functions utilizing multi-byte varint codes (e.g., hash functions with codes > 0x7F like sha2-224, blake2b-*, etc.).

The original code assumed a hardcoded 2-byte prefix size, expecting exactly one byte for the code varint and exactly one byte for the length varint.

Proposed Changes

  • Updates get_prefix to properly decode both the hash code varint and the digest length varint from the input stream using BytesIO, guaranteeing accuracy regardless of varint length.
  • Adds test cases (test_get_prefix_multi_byte_code & test_get_prefix_type_error) ensuring accuracy against multi-byte codes (using sha2_224 as an example).

@acul71 acul71 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.

Maintainer review (acul71)

Verdict: Request changes

The get_prefix fix looks correct (decode both varints via BytesIO / varint.decode_stream) and addresses #46. Local lint/typecheck/test (230 passed)/docs-ci are green, and there are no merge conflicts with master.

Blocker

  • Missing newsfragment: please add newsfragments/46.bugfix.rst (user-facing description, trailing newline).

Optional

  • Add a second multi-byte test (e.g. blake2b-256, 3-byte code varint) as cited in #46
  • Align TypeError message with decode to include the offending type

Please push the newsfragment and we can re-review for approval.

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.

get_prefix() hardcodes 2-byte prefix — breaks for multi-byte varint codes

2 participants