Make String#to_d accept embedded NUL bytes and reject ASCII-incompatible strings - #557
Merged
Conversation
BigDecimal.interpret_loosely used StringValueCStr, which raises ArgumentError for an embedded NUL even though the conversion is meant to be lenient. Parse up to the first NUL instead, matching String#to_f and String#to_i. Also reject ASCII-incompatible encodings the way the core numeric conversions do, since the parser reads the string as bytes. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
The parser reads the string as bytes, so a UTF-16 string such as
"123".encode("UTF-16LE") was silently parsed as "1". Raise
Encoding::CompatibilityError as Float() and Integer() do.
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
tompng
force-pushed
the
to_d_null_byte
branch
from
September 12, 2026 13:58
09f0496 to
2db4827
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Depends on the RB_GC_GUARD PR (the first commit here is the same commit); the diff will shrink to two commits once that is merged.
String#to_dwith embedded NULBigDecimal.interpret_looselyusedStringValueCStr, so"1\0".to_draisedArgumentError: string contains null byteeven though the conversion is meant to be lenient. It now parses up to the first NUL, matchingString#to_fandString#to_i:ASCII-incompatible encodings
The parser reads the string as bytes, and
StringValueCStronly rejects NUL characters for wide encodings, soBigDecimal("123".encode("UTF-16LE"))was silently parsed as1. BothBigDecimal()andString#to_dnow raiseEncoding::CompatibilityErrorviarb_must_asciicompat, the same asFloat(),Integer()andString#to_f. This applies withexception: falseas well, again matchingFloat(str, exception: false).🤖 Generated with Claude Code