Skip to content

Ignore a run of trailing NULs in Parse, as decimal and double do [patch] - #194

Merged
matt-edmondson merged 1 commit into
mainfrom
fix/parse-trailing-nul-193
Nov 10, 2026
Merged

matt-edmondson merged 1 commit into
mainfrom
fix/parse-trailing-nul-193

Conversation

@matt-edmondson

Copy link
Copy Markdown
Contributor

Fixes #193

What changed

Parse(ReadOnlySpan<char>, NumberStyles, IFormatProvider?), which every Parse/TryParse overload goes through, now calls a new TrimTrailingNuls before TrimAllowedWhite. It drops the run of '\0' that ends the text, the way System.Number's trailing-zeros check does:

  • It applies under every style, NumberStyles.None included, so it doesn't depend on AllowTrailingWhite
  • It runs before the white-space trim, so "1e3 \0" loses its NUL and then its space
  • Text made only of NULs is left as it is, so it still fails

Tests

PreciseNumberParseTrailingNulTests checks each case against decimal.TryParse with the same style, through Parse and TryParse and through both the string and span overloads:

  • Accepted: "5\0" (None and Float), "1.5\0\0\0", "1e3 \0", "12345\0…" (Integer), "-0.25\0"
  • Still rejected: "1.5\0 ", "\0 5", "\0", "\0\0\0", "5 \0" (None), "5\01"

With the fix reverted, all 6 accepted cases fail. Full suite: 598 passed.


Generated by Claude Code

Text from a fixed-size or NUL-padded buffer ends with '\0' characters,
which the BCL parsers drop. Parse and TryParse now drop one trailing run of
them before anything else, under every style, unless the text is nothing
but NULs. NULs anywhere else, or followed by white space, are still errors.

Fixes #193

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

sonarqubecloud Bot commented Nov 9, 2026

Copy link
Copy Markdown

@matt-edmondson
matt-edmondson merged commit d9b85db into main Nov 10, 2026
15 checks passed
@matt-edmondson
matt-edmondson deleted the fix/parse-trailing-nul-193 branch November 10, 2026 23:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Parse/TryParse reject trailing NUL characters ("5\0", "1.5\0\0\0") that decimal, double, long and BigInteger all accept

2 participants