Skip to content

fix: cap the nested-empty array walk at PostgreSQL's six dimensions - #266

Merged
CaffeinatedCoder merged 1 commit into
mainfrom
fix/nested-array-depth-cap
Oct 19, 2026
Merged

CaffeinatedCoder merged 1 commit into
mainfrom
fix/nested-array-depth-cap

Conversation

@CaffeinatedCoder

Copy link
Copy Markdown
Owner

Closes #248
Closes #250

Problem

SetFormat.IsNestOfEmptyArrays (#128, #240) recursed once per brace, with no limit.

Both came in with 10.0.0; 9.1.0 doesn't have this code.

Change

  • The walk stops at MaxDimensions (6), PostgreSQL's MAXDIM. A seventh level is not a nest, so the literal falls through to the element parser, which rejects a nested brace immediately. The recursion never goes deeper than six frames.
  • Audit: no other code in src/ recurses over literal text. The range, multirange and element-quoting parsers are loops. The other self-calls are overloads or ValueSetSqlGuard's walk of EF's SQL tree.
  • Measured (pg_input_is_valid(…, 'int4[]')): six levels of empties read as {} on 17 and 18 and are rejected by 16. Seven levels are rejected everywhere, with or without an element.
  • docs/value-sets.md: the nested-empty bullet now says ragged nests and nests deeper than six levels fail.

Tests

  • New oracle NestedLiteralDepthContractTests (conventions). Every discovered set type gets 7-, 20- and 1,000-level literals, empty and around an element, through Parse, TryParse (string and span) and the TypeConverter. The old recursion survives these depths, so a revert makes the test fail rather than crash the host.
  • More rows in SetDimensionDecorationTests, NestedEmptyArrays_… and the corpus probe: six levels are read on 17+, seven are rejected.
  • Cap removed: 2 unit rows fail, the contract test fails ("DateSet Parse(string) reads {{{{{{{}}}}}}}"), and 29 live tests fail. Cap in place: the probe passes on PostgreSQL 16 and 18.
  • New seed nested-empty-walk-uncapped.patch: CAUGHT by all three oracles. ragged-nest-read-as-empty.patch no longer applied and is re-planted on the new walk; still CAUGHT.
  • By hand (not committed, so a revert can't crash CI): 100,000 nested braces, empty or around an element, give false from TryParse for Int32Set and StringSet, even on a 256 KB thread, and FormatException from the TypeConverter.
  • Core 1378, conventions 400, ASP.NET Core 92: all passed.

No changelog entry: this never shipped. The 10.0.0 line for #128 still says what reads.

🤖 Generated with Claude Code

SetFormat.IsNestOfEmptyArrays (#128, #240) recursed once per brace with no limit. A literal of a few
thousand nested braces overflowed the stack, which .NET cannot catch, so Int32Set.TryParse killed
the process instead of returning false, and a 24 KB [FromHeader] Int32Set header killed a Kestrel app
(#248, critical). And a nest of empty arrays deeper than six levels read as {}, though PostgreSQL 16,
17 and 18 all refuse it: "number of array dimensions exceeds the maximum allowed (6)" (#250).

The walk now stops at MaxDimensions, PostgreSQL's MAXDIM: a seventh level is not a nest, the literal
falls to the element parser, which refuses a nested brace at once, and the recursion never exceeds six
frames. No other walk in src/ recurses over literal text (range, multirange and element quoting are
loops; ValueSetSqlGuard walks EF's SQL tree). Measured: six levels read as {} on 17 and 18, seven are
refused everywhere. By hand: 100,000 nested braces, empty or around an element, return false through
TryParse and throw FormatException through the TypeConverter, even on a 256 KB thread.

NestedLiteralDepthContractTests (new oracle) feeds every set type seven-level, twenty-level and
thousand-level literals through Parse, TryParse and the TypeConverter: depths the old recursion
survives, so it fails rather than crashes the host. SetDimensionDecorationTests, the nested-empty
version test and the corpus gain six-level (read on 17+) and seven-level (refused) rows. With the cap
removed: 2 unit rows, the contract test and 29 live tests fail. Seed nested-empty-walk-uncapped.patch
is CAUGHT by all three oracles; ragged-nest-read-as-empty.patch is re-planted on the new walk.

Closes #248
Closes #250

Found-By: CA
Defect-Class: LIT

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@CaffeinatedCoder CaffeinatedCoder added this to the 10.0.0 milestone Oct 19, 2026
@CaffeinatedCoder CaffeinatedCoder added bug Something isn't working silent Answers wrongly without throwing — fixes go in the next major, never deferred area/value-sets Value set families and wrapper arities area/literals Parse, TryParse and formatting package/core Affects CodoMetis.ValueRanges severity/critical Data loss, corruption, or silently wrong results in common use labels Oct 19, 2026
@CaffeinatedCoder
CaffeinatedCoder merged commit 35e712c into main Oct 19, 2026
@CaffeinatedCoder
CaffeinatedCoder deleted the fix/nested-array-depth-cap branch October 19, 2026 05:17
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 area/value-sets Value set families and wrapper arities bug Something isn't working package/core Affects CodoMetis.ValueRanges severity/critical Data loss, corruption, or silently wrong results in common use silent Answers wrongly without throwing — fixes go in the next major, never deferred

Projects

None yet

Development

Successfully merging this pull request may close these issues.

A nest of empty arrays deeper than PostgreSQL's 6 dimensions reads as {} A deeply nested value set literal overflows the stack and kills the process

1 participant