Skip to content

mruby-socket: reject a big integer getaddrinfo hint instead of ignoring it - #7118

Merged
matz merged 1 commit into
mruby:masterfrom
takumin:socket-bigint-hint
Aug 12, 2026
Merged

mruby-socket: reject a big integer getaddrinfo hint instead of ignoring it#7118
matz merged 1 commit into
mruby:masterfrom
takumin:socket-bigint-hint

Conversation

@takumin

@takumin takumin commented Aug 12, 2026

Copy link
Copy Markdown
Contributor

Addrinfo.getaddrinfo converts its family, socktype and protocol hints only under mrb_integer_p, which asks about representation rather than class. Where mruby-bigint is built in, a hint too wide for mrb_int arrives as MRB_TT_BIGINT, the conversion is skipped, and the field keeps the default from struct addrinfo hints = {0}. The caller asked for something unrepresentable and got an unhinted lookup back.

That inverts the narrowing check added in #6960: a value that fits mrb_int but not C int raises, while a larger one passes silently. On the default 64 bit build, which carries bigint:

Addrinfo.getaddrinfo("localhost", nil, 2 ** 40)  # RangeError: getaddrinfo family out of range: 1099511627776
Addrinfo.getaddrinfo("localhost", nil, 2 ** 70)  # same results as no hint at all, afamily 2

It is not specific to MRB_INT32; any build carrying mruby-bigint has it, and MRB_INT32 only lowers the threshold to 2 ** 32.

Change

getaddrinfo_hint now takes the mrb_value and rejects a big integer before reading it, so an out of range hint is reported the same way whatever its representation. The three call sites test mrb_integer_p(v) || mrb_bigint_p(v). mrb_bigint_p is FALSE without MRB_USE_BIGINT, so a build without bigint compiles to what it had before.

flags is unaffected, because mrb_get_args already raises RangeError for a big integer passed to its i specifier. It moves to the new signature only to keep one entry point.

Values of other classes are still ignored rather than rejected, as before; narrowing that is a separate question from this one.

Test

The new assertion fails on master (KO: 1) and passes with the change. Its shift width comes from a variable because a literal wide shift is constant folded, and the fold fails where bigint is absent.

build tests result
default host, 64 bit with bigint 2059 KO: 0, new test runs
MRB_INT32 with bigint 2136 KO: 0, file not loaded
MRB_INT32 without bigint, mruby-socket added 1774 KO: 0, file not loaded

The two MRB_INT32 builds do not reach this file: the literal 1 << 40 in the assertion above the new one folds to a value the 32 bit VM cannot load, so the whole file is dropped without a failure. That is the subject of #7117, and it is left alone here so the two changes do not touch the same lines. Applying that fix locally on top of this branch gives 2147 (KO: 0, new test runs and passes) with bigint and 1785 (KO: 0, new test skips) without it.

Summary by CodeRabbit

  • Bug Fixes
    • Improved validation of address resolution hints, including large integer values.
    • Values outside the supported range now raise a clear RangeError instead of being processed incorrectly.
    • Applied consistent validation across address family, socket type, protocol, and flags.

…ng it

`Addrinfo.getaddrinfo` converts its `family`, `socktype` and `protocol`
hints only under `mrb_integer_p`, which asks about representation rather
than class. Where `mruby-bigint` is built in, a hint too wide for
`mrb_int` arrives as `MRB_TT_BIGINT`, the conversion is skipped, and the
field keeps its zeroed default: the caller asked for something
unrepresentable and got an unhinted lookup back.

That inverts the narrowing check added in mruby#6960, since a value that fits
`mrb_int` but not C `int` raises while a larger one passes silently:

```ruby
Addrinfo.getaddrinfo("localhost", nil, 2 ** 40)  # RangeError
Addrinfo.getaddrinfo("localhost", nil, 2 ** 70)  # AF_UNSPEC results
```

Widen the guard to `mrb_bigint_p` as well and hand the value itself to
`getaddrinfo_hint`. A big integer outgrows `mrb_int`, so it never fits a
C `int` either, and it can be reported with the same message as any other
out of range hint. `mrb_bigint_p` is `FALSE` without `MRB_USE_BIGINT`, so
a build without bigint keeps the code it had.

`flags` is unaffected, because `mrb_get_args` already rejects a big
integer for its `i` specifier; it moves to the new signature only to keep
one entry point.

The added test builds its shift width from a variable, because a literal
wide shift is constant folded and the fold fails where bigint is absent,
which would drop every test in the file.
@takumin
takumin requested a review from matz as a code owner August 12, 2026 12:35
@coderabbitai

coderabbitai Bot commented Aug 12, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 279ef4cf-09cd-4e11-9003-3c8d2844677c

📥 Commits

Reviewing files that changed from the base of the PR and between 8ce99aa and 02d5569.

📒 Files selected for processing (2)
  • mrbgems/mruby-socket/src/socket.c
  • mrbgems/mruby-socket/test/addrinfo.rb

📝 Walkthrough

Walkthrough

Addrinfo.getaddrinfo now validates integer hints against the C int range. Oversized bigint values raise RangeError for family, socktype, and protocol hints. Tests cover these cases when bigint support is available.

Changes

Addrinfo hint validation

Layer / File(s) Summary
Validate and test integer hints
mrbgems/mruby-socket/src/socket.c, mrbgems/mruby-socket/test/addrinfo.rb
getaddrinfo_hint accepts Ruby values, rejects bigint and out-of-range values with RangeError, and applies validation to family, socktype, and protocol hints. Tests verify oversized bigint inputs.

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

Possibly related PRs

  • mruby/mruby#7117: Both changes update Addrinfo tests for oversized integer hints and unsupported bigint configurations.

Suggested reviewers: matz

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely describes the main change: rejecting oversized bigint hints in getaddrinfo instead of ignoring them.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@matz
matz merged commit 1c034f7 into mruby:master Aug 12, 2026
20 of 21 checks passed
@takumin
takumin deleted the socket-bigint-hint branch August 12, 2026 12:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants