Skip to content

Read utreexo's _int value as a plain int (closes #3129) - #3131

Merged
fametrano merged 1 commit into
btclib-org:mainfrom
dodoshell:utreexo-int-subclass
Nov 16, 2026
Merged

fametrano merged 1 commit into
btclib-org:mainfrom
dodoshell:utreexo-int-subclass

Conversation

@dodoshell

Copy link
Copy Markdown
Contributor

What this changes

btclib.utreexo._int, behind _u64 and _leaf_count, checked
0 <= value < ceiling with the value's own comparisons, so an int
subclass that answers "in range" passed: Stump, Proof and
UpdateData took -1 or 2**64 as a leaf count or position, and kept the
subclass. _int now reads the value through _int_from_integer, as
bytes_from_octets does since #3110, and checks and returns that plain
int.

Closes #3129

How it was verified

test_an_int_subclass_is_read_as_its_plain_int, for -1 and 1 << 64,
builds Stump, Proof and UpdateData (prev_num_leaves,
to_destroy, new_add) with a subclass whose __lt__ and __ge__
answer True, and expects BTClibValueError from each; an accepted value
comes back as a plain int. On 41961eb9 without the fix the test
fails for both values; with it, it passes.

Checks

  • uv run pre-commit run --all-files is clean (ruff, mypy strict,
    markdownlint, the copyright notice, uv.lock)
  • uv run pytest passes
  • I have read and understood every line of this change, and can
    answer a review of it without the model

Anything the reviewer should know

I ran everything on Windows with Python 3.13, since pyyaml does not
build under 3.15 here, in a checkout with LF line endings;
check-changelog ran through uv run python, as its
#!/usr/bin/env python3 line does not start here. The issue spells the
fix as int.__add__(value, 0) inline in _int; it calls
_int_from_integer instead, which does the same type check and the same
int.__add__, so the workaround lives in one place.

An int subclass whose own comparisons answer "in range" passed _int's
range check, so Stump, Proof and UpdateData took -1 or 2**64 as a leaf
count or position, and kept the subclass.

_int now reads the value through _int_from_integer, as
bytes_from_octets does (btclib-org#3110), and checks and returns that plain int.

Signed-off-by: dodoshell <326978213+dodoshell@users.noreply.github.com>
@chatgpt-codex-connector

Copy link
Copy Markdown
Codex Review Summary: ❌ Failed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ❌ Failed 2026-11-16T12:03:42.473741Z
We couldn’t complete this review. Comment @codex review to try again.
1fe3420 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex usage limits have been reached for code reviews. Please check with the admins of this repo to increase the limits by adding credits.
Credits must be used to enable repository wide code reviews.

@coderabbitai

coderabbitai Bot commented Nov 16, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Summary

Summary by CodeRabbit

  • Bug Fixes
    • Utreexo position values now receive consistent integer validation, including values supplied by int subclasses.
    • Invalid values are rejected even when custom comparisons would otherwise make them appear within the allowed range.
    • Accepted subclass values are stored as regular integers.

The change updates Utreexo integer validation to convert inputs before checking their range. Regression tests cover invalid and valid values from an int subclass.

Changes: Utreexo integer validation

Layer / File(s) Summary
Normalize integer inputs and test bounds
src/btclib/utreexo.py, tests/utreexo_test.py
_int now uses _int_from_integer before applying its existing range check. Tests verify that subclass comparisons cannot bypass validation and that accepted subclass values are stored as plain int values.

Estimated code review effort: 2 (Simple) | ~8 minutes

Change: Bug fix · Severity of issue fixed: Low

Suggested reviewers: fametrano

Merge Risk: Minimal · up to 1fe34

This change makes Utreexo integer validation reject int subclasses that fake in-range comparisons and ensures accepted values are plain ints. The change is small, local and covered by regression tests, and no merge-blocking risk was found.

Security architecture risk: Minimal · up to 1fe34

The change checks and returns ordinary integers rather than trusting integer-subclass comparisons. Existing range limits and error categories remain intact, with no introduced or worsened architecture-level risk identified.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title "Read utreexo's _int value as a plain int (closes #3129)" matches the main change: _int now reads the value through _int_from_integer and returns a plain int, which fixes the int-subclass by…
Description check Passed The description explains the bug (an int subclass with overridden comparisons passes the range check in _int, letting Stump, Proof and UpdateData accept -1 or 2**64 and keep the subclass), the fix (_i…
Linked Issues check Passed [#3129] The requirement is that btclib.utreexo._int refuse an int subclass whose comparisons report a value in range, raising BTClibValueError as it does for the plain int, and that a test drive…
Out of Scope Changes check Passed The whole-PR diff against the merge base 41961eb9 changes two files. src/btclib/utreexo.py replaces the _has_type import with _int_from_integer and rewrites the body of _int, which is the ta…
Full details: Title check

Explanation

The title "Read utreexo's _int value as a plain int (closes #3129)" matches the main change: _int now reads the value through _int_from_integer and returns a plain int, which fixes the int-subclass bypass in btclib.utreexo. The title does not state the title-check requirements in full, but the referenced standard was not available to verify beyond the title's content, so the title is judged on its descriptiveness and relevance.

Full details: Description check

Explanation

The description explains the bug (an int subclass with overridden comparisons passes the range check in _int, letting Stump, Proof and UpdateData accept -1 or 2**64 and keep the subclass), the fix (_int reads the value through _int_from_integer and returns a plain int), the regression test, and the verification steps. It is directly related to the changeset.

Full details: Linked Issues check

Explanation

[#3129] The requirement is that btclib.utreexo._int refuse an int subclass whose comparisons report a value in range, raising BTClibValueError as it does for the plain int, and that a test drive _int's callers with such a subclass. At head, _int in src/btclib/utreexo.py calls _int_from_integer(value, what) before the 0 &lt;= value &lt; ceiling check and returns that value. The range check and return therefore act on the plain int, not on the subclass. The new test test_an_int_subclass_is_read_as_its_plain_int in tests/utreexo_test.py uses an int subclass whose __lt__ and __ge__ return True, with values -1 and 1 << 64. It asserts BTClibValueError from Stump, UpdateData (prev_num_leaves, to_destroy, new_add) and Proof, and asserts that accepted values are stored as plain int. These calls reach _int through the callers named in the issue. Scope: the change is limited to src/btclib/utreexo.py as the issue requires. Uncertainty: _int_from_integer is defined in src/btclib/utils.py. That file was truncated in this review, so its body was not read directly. The base code uses it for out_size and its docstring says a bool is refused, which matches the bool rejection that _int had before. The bool and type-error behavior of _int is inferred from that use, not read from the helper.

Full details: Out of Scope Changes check

Explanation

The whole-PR diff against the merge base 41961eb9 changes two files. src/btclib/utreexo.py replaces the _has_type import with _int_from_integer and rewrites the body of _int, which is the target of #3129. tests/utreexo_test.py adds the override import, the _InRange helper class, and the parametrized regression test for the same issue. The docstring of _int changes to match the new behavior. No unrelated code path changes. The test import of typing_extensions.override is supporting test code for this objective.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Recent review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between adb4c93 and 1fe3420.

Files selected for processing: 2

  • src/btclib/utreexo.py
  • tests/utreexo_test.py

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.

@fametrano

Copy link
Copy Markdown
Member

Claimed: a maintainer's session is reviewing this pull request and may push signed fixes on top as fast-forward commits, after asking you first about anything that is yours to change.

@fametrano

Copy link
Copy Markdown
Member

Head 1fe3420 passed a maintainer session's local review with no change requested, and every check on it passed or was skipped.

The session ran the gates on that head offline, in a sandbox: lock sync, the hooks on both changed files, and the union of the bindings and no-bindings coverage at 100%. Both suite lanes stopped only on the integration tests that bind a loopback port, which the sandbox refuses, so the suite was not run locally and the checks of this head stand for it. Two local reviewers cleared the head: a Claude Code reviewer and a fresh Codex reviewer. Their inputs, run on this head and on the merge base, show that every caller of _int now refuses an out-of-range int subclass and returns a plain int. Plain ints, bools and other types are refused exactly as before. The new test fails without the fix and passes with it.

Two of the three reviews on GitHub are absent from this head. The Claude review bot skips a pull request from a fork. Codex's review failed on its usage limit. CodeRabbit read the head and left no finding. Thank you for the fix: it is waiting on a maintainer's approval.

@fametrano

Copy link
Copy Markdown
Member

Released: the maintainer's session has finished its review of this head and pushed nothing to the branch. It now waits on a maintainer's approval.

@fametrano fametrano left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approved at 1fe3420.

@fametrano
fametrano merged commit c0a95f5 into btclib-org:main Nov 16, 2026
22 checks passed
@fametrano

Copy link
Copy Markdown
Member

Landed as c0a95f5. Thank you, @dodoshell: utreexo's _int now reads its value as a plain int, through the helper main already had, and #3129 is closed by it.

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.

utreexo's _int takes an int subclass's own comparisons for its range check

2 participants