Skip to content

Reject a NUL inside the exponent in Parse [patch] - #206

Open
matt-edmondson wants to merge 1 commit into
mainfrom
fix/exponent-nul
Open

matt-edmondson wants to merge 1 commit into
mainfrom
fix/exponent-nul

Conversation

@matt-edmondson

Copy link
Copy Markdown
Contributor

Fixes #203

What changed

TrimTrailingNuls rightly keeps a NUL that is followed by white space, a sign or a currency symbol. Once those are trimmed, though, the exponent text is "1\0", and long.Parse ignores trailing NULs by itself, so "3e1\0 " parsed as 30 and "3e1\0-" as -30. decimal and double both throw for these. FromParsedParts now throws FormatException for an exponent text containing a NUL before handing it to long.Parse. NULs at the very end ("3e1\0", "3e1\0\0") are trimmed earlier and still parse as 30.

Tests

  • New ParseRejectsANulInsideTheExponent covers the issue's five inputs. Each asserts that decimal and double reject it, that both TryParse overloads return false, and that Parse throws FormatException. All five fail without the fix.
  • ParseIgnoresTrailingNulsAsDecimalDoes gains "3e1\0" and "3e1\0\0".
  • The $ case uses an invariant NumberFormatInfo with CurrencySymbol = "$" rather than en-US, following PreciseNumberParseAffixTests, so it also runs in globalization-invariant mode.
  • Full suite: 689 passed.

Generated by Claude Code

long.Parse ignores trailing NULs by itself, so a NUL that the parse kept
because white space, a sign or a currency symbol followed it reached the
exponent as "1\0" and was accepted: "3e1\0 " parsed as 30 and "3e1\0-"
as -30, where decimal and double throw. An exponent containing a NUL is
now a FormatException; NULs at the very end are still ignored.

Fixes #203

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017vfJW4B4xxTAPzAHBD4zKk
@sonarqubecloud

Copy link
Copy Markdown

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

Labels

None yet

Projects

None yet

2 participants