Skip to content

fix: strip whitespace at either end of a quoted or escaped range bound - #73

Merged
CaffeinatedCoder merged 1 commit into
mainfrom
claude/quoted-bound-whitespace-4088f1
Oct 17, 2026
Merged

CaffeinatedCoder merged 1 commit into
mainfrom
claude/quoted-bound-whitespace-4088f1

Conversation

@CaffeinatedCoder

Copy link
Copy Markdown
Owner

Closes #45

Problem

A quoted range bound with whitespace at either end threw FormatException, but PostgreSQL reads it:

DateRange.Parse("[\" 2026-01-01\",2026-02-01)", null); // FormatException
SELECT '[" 2026-01-01",2026-02-01)'::daterange; -- [2026-01-01,2026-02-01)

I measured this on PostgreSQL 17 for int4range, int8range, numrange, daterange (including -infinity), tsrange, tstzrange and a custom timerange. The full table is in the issue. Every type behaves the same:

  • range_in keeps the whitespace in the bound, whether it's quoted, escaped or bare.
  • Every element input function strips space, tab, LF, CR, VT and FF from both ends.
  • A no-break space or em space isn't stripped, so the bound throws.
  • A bound of only whitespace throws.

The library dropped only unquoted whitespace. A quoted or escaped space reached the strict ISO grammar, so all 8 temporal range types threw on it. The numeric types already accepted it, because int.Parse and decimal.Parse trim.

This fails loudly and the fix only accepts more, so it fits 9.1.0.

Change

  • src/CodoMetis.ValueRanges/Internals/RangeFormat.cs: ScanBound reads the bound the way range_in does, then trims LiteralWhiteSpace (the same six ASCII characters) from the result. The significant bookkeeping is gone. An absent bound is still the only infinite one. The element grammar is still strict ISO 8601. The NodaTime ranges parse through the same scanner, so LocalDateRange, LocalDateTimeRange, InstantRange and YearMonthRange change with it. That's why the NodaTime changelog has a line too, and why I added package/nodatime.
  • docs/serialization.md#quote-a-bound: the whitespace bullet now covers quoted and escaped bounds. Two "Good to know" bullets cover ASCII-only whitespace and the behaviour before 9.1. Claim J33-bound-whitespace-stripped in test/oracles/claims.json quotes the new sentence.
  • Changelogs: one ### Fixed line in the root, core and NodaTime changelogs.

Tests

  • RangeLiteralGrammarContractTests (conventions) now sweeps every range type through every entry point (Parse, span, TryParse, JSON, TypeConverter, RangeSet.Parse). It reads quoted, escaped and mixed bounds with each ASCII whitespace character. It checks that a no-break or em space throws, and that a quoted or escaped whitespace-only bound throws.
  • LiteralGrammarParityTests.BoundWhitespace_ReadsAsPostgresReadsIt (integration) sends 72 literals per type to a live PostgreSQL and compares by value. It covers 11 type pairings, with 8 whitespace characters × 9 forms each, and includes the NodaTime types.
  • One repro test each in RangeParseFormatTests and NodaTimeParseFormatTests.
  • Revert proof: I reverted only ScanBound. The conventions sweep then failed 192 rows (8 temporal types × 4 forms × 6 characters). The core and NodaTime repro tests failed with ' 2026-01-01' is not a valid DateOnly. The live tests failed 289 rows with library refused, PostgreSQL [2026-01-01,…]. That revert is now the seed test/oracles/seeds/quoted-bound-whitespace-kept.patch. I refreshed quoted-empty-bound-is-infinite.patch against the new code, because the old one no longer applied. dotnet run tools/oracles.cs seeds quoted reports both seeds caught by both oracles.
  • Full dotnet test, including both integration projects, passes locally.

Follow-up: temporal value sets have the same refusal (DateSet.Parse("{\" 2026-01-01\"}")), filed as #70.

🤖 Generated with Claude Code

PostgreSQL's range_in keeps whitespace in a bound whether it is quoted,
escaped or bare, and every element input function behind the seven range
types strips ASCII whitespace from both ends (measured on PostgreSQL 17,
recorded in #45). The parser dropped only unquoted whitespace, so
[" 2026-01-01",2026-02-01) reached the strict ISO grammar and threw.

ScanBound now reads the bound as range_in does and trims LiteralWhiteSpace
from the result. A no-break space is still bound text, and a bound of only
whitespace is still present and empty, so both still throw.

The grammar contract sweep and the live parity test cover all six ASCII
whitespace characters, quoted, escaped and bare, for every range type. The
revert is kept as the quoted-bound-whitespace-kept seed; the
quoted-empty-bound-is-infinite seed is refreshed against the new code.

Closes #45

Found-By: RV
Defect-Class: LIT
Co-Authored-By: Claude Opus 5.5 <[email protected]>
@CaffeinatedCoder CaffeinatedCoder added this to the 9.1.0 milestone Oct 17, 2026
@CaffeinatedCoder CaffeinatedCoder added bug Something isn't working package/nodatime Affects CodoMetis.ValueRanges.NodaTime area/literals Parse, TryParse and formatting package/core Affects CodoMetis.ValueRanges severity/medium Inconsistency or failure in a narrow scenario labels Oct 17, 2026
@CaffeinatedCoder
CaffeinatedCoder merged commit e734384 into main Oct 17, 2026
@CaffeinatedCoder
CaffeinatedCoder deleted the claude/quoted-bound-whitespace-4088f1 branch October 17, 2026 18:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/literals Parse, TryParse and formatting bug Something isn't working package/core Affects CodoMetis.ValueRanges package/nodatime Affects CodoMetis.ValueRanges.NodaTime severity/medium Inconsistency or failure in a narrow scenario

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Quoted range bound with a leading space is refused; PostgreSQL accepts it

1 participant