Skip to content

Commit 1c8b746

Browse files
fix: lower TOML_MAX_NESTED_VALUES to prevent stack overflow on deeply nested arrays/inline tables (#293)
Co-authored-by: Daniel Bodorin <danielbodorin@users.noreply.github.com>
1 parent 6626578 commit 1c8b746

5 files changed

Lines changed: 22 additions & 4 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ template:
2828
- fixed `is_homogeneous()` overloads with `first_nonmatch` outparam being broken in optimized builds (#231) (@Forbinn)
2929
- fixed unclear error message when parsing integers that would overflow (#224) (@chrimbo)
3030
- fixed CMake `install` target installing `meson.build` files (#236) (@JWCS)
31+
- lowered `TOML_MAX_NESTED_VALUES` default from 256 to 128 to prevent stack overflow on deeply nested arrays/inline tables in sanitizer builds (@danielbodorin)
3132

3233
## v3.4.0
3334

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,7 @@ UTF-8 decoding is performed using a state machine based on Bjoern Hoehrmann's '[
288288
- **[@bjadamson](https://github.com/bjadamson)** - Reported some bugs and helped design a new feature
289289
- **[@bobfang1992](https://github.com/bobfang1992)** - Reported a bug and created a [wrapper in python](https://github.com/bobfang1992/pytomlpp)
290290
- **[@capuanob](https://github.com/capuanob)** - Integrated this project into OSSFuzz
291+
- **[@danielbodorin](https://github.com/danielbodorin)** - Fixed stack overflow from deeply nested arrays/inline tables
291292
- **[@GiulioRomualdi](https://github.com/GiulioRomualdi)** - Added cmake+meson support
292293
- **[@jonestristand](https://github.com/jonestristand)** - Designed and implemented the `toml::path`s feature
293294
- **[@kcsaul](https://github.com/kcsaul)** - Fixed a bug

include/toml++/impl/preprocessor.hpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1177,9 +1177,11 @@
11771177
#endif
11781178

11791179
#ifndef TOML_MAX_NESTED_VALUES
1180-
#define TOML_MAX_NESTED_VALUES 256
1180+
#define TOML_MAX_NESTED_VALUES 128
11811181
// this refers to the depth of nested values, e.g. inline tables and arrays.
1182-
// 256 is crazy high! if you're hitting this limit with real input, TOML is probably the wrong tool for the job...
1182+
// 128 is very generous; real TOML files rarely exceed single-digit nesting.
1183+
// keep this value low enough to avoid stack overflows in sanitizer-instrumented builds
1184+
// where each recursion cycle may consume ~3KB of stack.
11831185
#endif
11841186

11851187
#ifndef TOML_MAX_DOTTED_KEYS_DEPTH

tests/user_feedback.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,18 @@ b = []
168168
constexpr auto start = "fl =[ "sv;
169169
memcpy(s.data(), start.data(), start.length());
170170
parsing_should_fail(FILE_LINE_ARGS, std::string_view{ s });
171+
172+
// deeply nested inline tables should also fail gracefully, not stack overflow
173+
{
174+
// build: fl = {a={a={a={a=...{a=1}...}}}
175+
std::string nested_tables = "fl = ";
176+
for (size_t i = 0; i < 2048; i++)
177+
nested_tables += "{a=";
178+
nested_tables += "1";
179+
for (size_t i = 0; i < 2048; i++)
180+
nested_tables += "}";
181+
parsing_should_fail(FILE_LINE_ARGS, std::string_view{ nested_tables });
182+
}
171183
}
172184

173185
SECTION("tomlplusplus/issues/112") // https://github.com/marzer/tomlplusplus/issues/112

toml.hpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1086,9 +1086,11 @@
10861086
#endif
10871087

10881088
#ifndef TOML_MAX_NESTED_VALUES
1089-
#define TOML_MAX_NESTED_VALUES 256
1089+
#define TOML_MAX_NESTED_VALUES 128
10901090
// this refers to the depth of nested values, e.g. inline tables and arrays.
1091-
// 256 is crazy high! if you're hitting this limit with real input, TOML is probably the wrong tool for the job...
1091+
// 128 is very generous; real TOML files rarely exceed single-digit nesting.
1092+
// keep this value low enough to avoid stack overflows in sanitizer-instrumented builds
1093+
// where each recursion cycle may consume ~3KB of stack.
10921094
#endif
10931095

10941096
#ifndef TOML_MAX_DOTTED_KEYS_DEPTH

0 commit comments

Comments
 (0)