Skip to content

string.c: do not limit String#to_f/Float() input to 80 bytes - #7847

Open
yuyongyan29-dev wants to merge 1 commit into
mruby:masterfrom
yuyongyan29-dev:fix/str-to-f-long-input-b
Open

yuyongyan29-dev wants to merge 1 commit into
mruby:masterfrom
yuyongyan29-dev:fix/str-to-f-long-input-b

Conversation

@yuyongyan29-dev

@yuyongyan29-dev yuyongyan29-dev commented Nov 20, 2026 •

Copy link
Copy Markdown

mrb_str_len_to_dbl() copied its input into a fixed 80-byte buffer. Longer numeric strings returned Infinity (even negative ones), or lost the rest of the fraction and the exponent once a . had been seen.

Copy into a NUL-terminated buffer sized from the input instead, using mrb_malloc() when it does not fit on the stack. Short inputs, underscores, and Float() error checks behave as before.

Adds tests for long strings and for shared substrings in String#to_f and Kernel#Float.

Fixes #7846

Summary by CodeRabbit

  • Bug Fixes
    • Fixed conversion of long numeric strings to floating-point values, including values with leading zeros, decimal points, exponents, and valid digit separators.
    • Improved conversion of numeric values from sliced strings and corrected handling of very small values.
    • Invalid numeric formats now produce consistent errors when strict validation is enabled; permissive conversion stops at invalid separators or trailing non-whitespace text.

Input was copied into a fixed buffer of DBL_DIG*4+20 bytes; longer
strings returned INFINITY, or lost the remaining fraction and exponent
once a dot had been seen (mruby#7846).

Copy into a NUL-terminated buffer sized from the input, falling back to
mrb_malloc() when it does not fit on the stack.
@coderabbitai

coderabbitai Bot commented Nov 20, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

mrb_str_len_to_dbl now parses long numeric strings using dynamically sized storage. New tests cover Float and String#to_f conversions, including long inputs, underscores, malformed strings, and shared substrings.

Changes: Long numeric string parsing

Layer / File(s) Summary
Dynamic float parsing and regression coverage
src/string.c, mrbgems/mruby-kernel-ext/test/kernel.rb, test/t/string.rb
mrb_str_len_to_dbl uses a stack buffer or allocates storage for longer input. It removes valid digit-separated underscores and handles invalid placement and trailing input according to badcheck. Regression tests cover long numeric forms and shared substrings for Float and String#to_f.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Change: Bug fix · Severity of issue fixed: Medium

Suggested reviewers: hasumikin

Merge Risk: Minimal · up to 37bd2

String#to_f and Float() now handle numeric strings longer than 80 bytes correctly, with regression tests for long inputs, underscores, malformed strings and shared substrings. No merge-blocking issue was identified.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title names the main change: removing the 80-byte input limit on String#to_f and Float() so longer numeric strings are parsed correctly. The changeset in src/string.c replaces the fixed buffer in …
Linked Issues check Passed [#7846] The issue requires String#to_f and Float() to return correct values for numeric strings longer than 79 characters. The fixed 80-byte buffer caused the failure. In src/string.c, `mrb_str_le…
Out of Scope Changes check Passed The whole-PR diff against the merge base changes only src/string.c, test/t/string.rb, and mrbgems/mruby-kernel-ext/test/kernel.rb. The src/string.c changes all serve the fix in mrb_str_len_to_dbl().…
Full details: Title check

Explanation

The title names the main change: removing the 80-byte input limit on String#to_f and Float() so longer numeric strings are parsed correctly. The changeset in src/string.c replaces the fixed buffer in mrb_str_len_to_dbl() with an input-sized buffer, and the tests cover long strings and shared substrings. The title is specific and matches the objectives.

Full details: Linked Issues check

Explanation

[#7846] The issue requires String#to_f and Float() to return correct values for numeric strings longer than 79 characters. The fixed 80-byte buffer caused the failure. In src/string.c, mrb_str_len_to_dbl() now copies input into sbuf when it fits. Otherwise it allocates bufsize = copylen + 1 with mrb_malloc(). The copy is NUL-terminated, so mrb_read_float() receives the full input. The no-underscore path copies the full span p2..pend. The underscore path copies the stripped result. The stripped result is never longer than the input, so the allocation is sufficient. The old INFINITY return and the dot truncation path are removed. The change applies to all inputs without spaces, not only inputs with underscores. Tests in test/t/string.rb and mrbgems/mruby-kernel-ext/test/kernel.rb cover long zero runs, long fractions, exponents, underflow, underscores, and shared substrings. Heap storage is freed on every exit path (bad, the parse failure return, and success). The issue's reproduction cases should now produce the expected values. I did not run the test suite. The result rests on source reading.

Full details: Out of Scope Changes check

Explanation

The whole-PR diff against the merge base changes only src/string.c, test/t/string.rb, and mrbgems/mruby-kernel-ext/test/kernel.rb. The src/string.c changes all serve the fix in mrb_str_len_to_dbl(). These include the stack/heap buffer selection, the underscore handling rewrite, the parse-range variables, and the heap release on exit paths. The renamed dot variable is removed because the truncation path no longer exists. The test additions target the issue's long-string and shared-substring cases. No unrelated change appears in the reviewed scope.

Finishing Touches

Generate unit tests (beta)

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

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

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 9d18dbe0-ce51-4ccf-a0ea-47ee2b7dcc92
Commits

Reviewing files that changed from the base of the PR and between 8587881 and 37bd24a.

Files selected for processing: 3

  • mrbgems/mruby-kernel-ext/test/kernel.rb
  • src/string.c
  • test/t/string.rb

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

String#to_f and Float() return wrong values for numeric strings longer than 79 characters

1 participant