Fix tonumber to handle bad input without throwing exceptions#346
Open
kellybirr wants to merge 2 commits into
Open
Fix tonumber to handle bad input without throwing exceptions#346kellybirr wants to merge 2 commits into
kellybirr wants to merge 2 commits into
Conversation
tonumber with an explicit base delegated to Convert.ToUInt32 (for bases
2/8/10/16), so script-influenceable input like tonumber('zz', 16) escaped
the interpreter as a raw FormatException — and input above uint.MaxValue
as an OverflowException — instead of returning nil per Lua semantics.
These exceptions cannot be caught by pcall and propagate into the
embedding host.
Replace the conversion with a small Lua-5.2-style numeral parser:
- bases 2..36 supported (previously only 2..10 and 16), with digits
0-9 and a-z/A-Z case-insensitively, per standard Lua
- optional surrounding whitespace and a leading '-' accepted; any other
input (empty, '0x' prefix, '+' sign, out-of-base digit, embedded
space) yields nil instead of raising or throwing
- large numerals accumulate into a double instead of overflowing uint
- a string DynValue wrapping a null (DynValue.NewString(null)) yields
nil instead of a NullReferenceException
- only 'base out of range' still raises, matching reference Lua
Note two deliberate behavior changes, both aligning with reference Lua:
out-of-base digits in bases 3-9 previously raised "invalid character"
and now return nil (TestMore/301-basic.t updated accordingly), and
values above uint.MaxValue previously threw where they now parse.
Adds a TonumberTests fixture covering valid numerals across bases,
sign/whitespace handling, nil-on-unparseable-input regressions, large
values, and the out-of-range-base errors.
fix: tonumber(e, base) no longer throws raw CLR exceptions on bad input
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.
fix: tonumber(e, base) no longer throws raw CLR exceptions on bad input
tonumber with an explicit base delegated to Convert.ToUInt32 (for bases
2/8/10/16), so script-influenceable input like tonumber('zz', 16) escaped
the interpreter as a raw FormatException — and input above uint.MaxValue
as an OverflowException — instead of returning nil per Lua semantics.
These exceptions cannot be caught by pcall and propagate into the
embedding host.
Replace the conversion with a small Lua-5.2-style numeral parser:
0-9 and a-z/A-Z case-insensitively, per standard Lua
input (empty, '0x' prefix, '+' sign, out-of-base digit, embedded
space) yields nil instead of raising or throwing
nil instead of a NullReferenceException
Note two deliberate behavior changes, both aligning with reference Lua:
out-of-base digits in bases 3-9 previously raised "invalid character"
and now return nil (TestMore/301-basic.t updated accordingly), and
values above uint.MaxValue previously threw where they now parse.
Adds a TonumberTests fixture covering valid numerals across bases,
sign/whitespace handling, nil-on-unparseable-input regressions, large
values, and the out-of-range-base errors.