Skip to content

fix: lower TOML_MAX_NESTED_VALUES to prevent stack overflow on deeply nested arrays/inline tables#293

Merged
marzer merged 1 commit intomarzer:masterfrom
danielbodorin:fix-recursion-stack-overflow
Mar 23, 2026
Merged

fix: lower TOML_MAX_NESTED_VALUES to prevent stack overflow on deeply nested arrays/inline tables#293
marzer merged 1 commit intomarzer:masterfrom
danielbodorin:fix-recursion-stack-overflow

Conversation

@danielbodorin
Copy link
Copy Markdown
Contributor

What does this change do?

Lowers the default \TOML_MAX_NESTED_VALUES\ from 256 to 128 to prevent stack overflows when parsing deeply nested arrays ([[[[...) or inline tables ({a={b={c=...).

Each recursion cycle in \parse_array\ → \parse_value\ → \parse_array\ (and the equivalent \parse_inline_table\ path) consumes ~3,280 bytes of stack. At the previous default of 256:

  • 256 × 3,280 = ~840KB — overflows 1MB stacks in sanitizer-instrumented builds (ASAN/MSAN)
  • Observed crashes at ~191-310 recursion levels in OSS-Fuzz and Windows fuzzer builds

At the new default of 128:

  • 128 × 3,280 = ~420KB — safe on 1MB stacks even under ASAN
  • No real TOML document needs 128 levels of container nesting
  • Users who need a different limit can still override via #define TOML_MAX_NESTED_VALUES \

PR #242 fixed a similar stack overflow for deeply nested dotted keys — this addresses the remaining array/inline table recursion path.

Fixes #292

Is it related to an existing bug report or feature request?

Yes — #292

Pre-merge checklist

  • I've read CONTRIBUTING.md
  • I've rebased my changes against the current HEAD of \origin/master\
  • I've added new test cases to verify my change
  • I've regenerated toml.hpp (how-to)
  • I've updated any affected documentation
  • I've rebuilt and run the tests with at least one of:
    • Clang 8 or higher
    • GCC 8 or higher
    • MSVC 19.20 (Visual Studio 2019) or higher
  • I've added my name to the list of contributors in README.md

…zer builds

Deeply nested arrays ([[[...) or inline tables ({a={b={c=...) cause
parse_array/parse_inline_table recursion that overflows the stack before
the existing TOML_MAX_NESTED_VALUES limit (256) is reached in
sanitizer-instrumented builds (ASAN/MSAN), where each recursion cycle
consumes ~3KB of stack.

Lower the default from 256 to 128. At 128 levels the worst-case stack
usage is ~420KB, leaving ample headroom on 1MB stacks even under ASAN.
No real TOML document needs 128 levels of container nesting.

Also add a regression test for deeply nested inline tables (arrays were
already covered by the issues/100 test).

Fixes marzer#292

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copy link
Copy Markdown
Owner

@marzer marzer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

Though, please do not use AI to write issues and PR descriptions on any future contributions to this repository. I would much rather read something written by a human, than obvious AI prose filled with bad markdown syntax and unicode abuse.

@danielbodorin
Copy link
Copy Markdown
Contributor Author

Thanks!

Though, please do not use AI to write issues and PR descriptions on any future contributions to this repository. I would much rather read something written by a human, than obvious AI prose filled with bad markdown syntax and unicode abuse.

Go it! Thanks!

@marzer marzer merged commit 1c8b746 into marzer:master Mar 23, 2026
15 checks passed
@danielbodorin danielbodorin deleted the fix-recursion-stack-overflow branch March 23, 2026 15:40
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.

Stack overflow on deeply nested arrays/inline tables

2 participants