Skip to content

fix(parse): require both closing brackets on an array-of-tables header - #66

Closed
MFA-G wants to merge 1 commit into
squirrelchat:mistressfrom
MFA-G:fix/array-table-single-bracket
Closed

fix(parse): require both closing brackets on an array-of-tables header#66
MFA-G wants to merge 1 commit into
squirrelchat:mistressfrom
MFA-G:fix/array-table-single-bracket

Conversation

@MFA-G

@MFA-G MFA-G commented Sep 5, 2026

Copy link
Copy Markdown

Fixes #65.

Problem

An array-of-tables header closed by a single ] is accepted when it is the last thing in the document, and one trailing character is silently discarded:

parse('[[a]')   // => { a: [ {} ] }   expected: throw
parse('[[a]x')  // => { a: [ {} ] }   expected: throw ('x' is dropped)

TOML 1.1.0 requires array-table-close = ws %x5D.5D, so [[a] cannot derive from array-table. BurntSushi/toml, CPython tomllib and Rust toml all reject it.

Cause

let k = parseKey(ctx, ']')      // consumes through the first ']'
if (isTableArray) {
    if (toml.charCodeAt(ctx.p - 1) !== 0x5d /* ] */) { throw ... }
    ctx.p++
}

ctx.p - 1 is the ] that parseKey has already consumed, so the comparison is always true and the guard can never fire. The second bracket is never required, and ctx.p++ advances past whatever happens to be there. That accounts for the whole table: at EOF the increment is a no-op ([[a]), with exactly one trailing character it swallows it ([[a]x), and with more the leftovers produce an unrelated error ([[a]xyz).

Fix

The old comparison is not dead weight — parseKey calls skipVoid(ctx, true, true) after the key, so ctx.p - 1 is what rejects tokens between the two brackets ([[uwu] ], covered by an existing test). The new condition keeps it and adds the missing requirement that ctx.p is the second bracket:

if (toml.charCodeAt(ctx.p - 1) !== 0x5d || toml.charCodeAt(ctx.p) !== 0x5d) {

charCodeAt past the end returns NaN, so EOF fails the check without a separate length test. The error pointer moves from ctx.p - 1 (the consumed bracket) to ctx.p (where the missing one should be).

Validation

  • Existing suite: 141 passed, 1 skipped, no changes to the existing expectations.
  • Added a case next to rejects invalid arrays of table covering [[uwu], [[uwu]x, [[uwu] and [[uwu]\nowo = 1.
  • toml-test 1.1.0 with the skips from run-toml-test.bash: 690 → 692 passing. The two newly passing cases are invalid/table/array-no-close-05 ([[a]) and -06 ([[a]x), which landed in toml-test via Add invalid array-of-tables cases at end-of-input toml-lang/toml-test#205. The remaining failures (linetab-number-01..04, integer/long, utf8-bom-01/02) are pre-existing and untouched.
  • Positive cases still parse: [[a]], [[ a ]], [[a.b]], [[a]] # c, [[a]]\n[[a]], CRLF variants.

Cost

One extra charCodeAt on the array-of-tables header path only — not on key-value lines, values, or plain [table] headers.

The guard tested `ctx.p - 1`, the bracket `parseKey` had already
consumed, so it was always `]` and could never fire. The second bracket
was therefore never required and `ctx.p++` advanced past whatever
followed: `[[a]` parsed as `{a: [{}]}` at EOF, and `[[a]x` parsed the
same way with the `x` silently discarded.

Keep the old comparison (it rejects tokens between the two brackets,
since `parseKey` skips trailing whitespace) and add the check for the
second bracket at `ctx.p`. Fixes toml-test 1.1.0
`invalid/table/array-no-close-05` and `-06`; the four pre-existing
`array-no-close-01..04` cases all have a following line, so none
exercised the end-of-input path.
@cyyynthia

Copy link
Copy Markdown
Member

AI garbage is not welcome here. And the fix isn't even done right but that's to be expected from a useless clanker.

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.

Array-of-tables header accepted with a single closing bracket at EOF: [[a] parses as {a: [{}]}

2 participants