Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix stack overflow by limiting the maximum depth of dotted keys #242

Merged
merged 1 commit into from
Nov 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,8 @@ UTF-8 decoding is performed using a state machine based on Bjoern Hoehrmann's '[
- **[@whiterabbit963](https://github.com/whiterabbit963)** - Fixed a bug with value_or conversions
- **[@ximion](https://github.com/ximion)** - Added support for installation with meson
- **[@a-is](https://github.com/a-is)** - Fixed a bug
-**[@capuanob](https://github.com/capuanob)** - Integrated this project into OSSFuzz
- **[@capuanob](https://github.com/capuanob)** - Integrated this project into OSSFuzz
- **[@tyler92]** - Fixed stack overflow that occurred during fuzzing tests
<br>

## Contact
Expand Down
2 changes: 1 addition & 1 deletion fuzzing/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ cmake -S . -B build -DBUILD_FUZZER=ON && cmake --build build --target install
# Build the corpus using the existing toml files in the source
mkdir -p corpus
find $SRC/tomlplusplus -name "*.toml" -exec cp {} corpus \;
zip -q $OUT/toml_fuzzer_seed_corpus.zip corpus/*
zip -q -j $OUT/toml_fuzzer_seed_corpus.zip corpus/*
8 changes: 7 additions & 1 deletion include/toml++/impl/parser.inl
Original file line number Diff line number Diff line change
Expand Up @@ -1063,7 +1063,8 @@ TOML_IMPL_NAMESPACE_START
class parser
{
private:
static constexpr size_t max_nested_values = TOML_MAX_NESTED_VALUES;
static constexpr size_t max_nested_values = TOML_MAX_NESTED_VALUES;
static constexpr size_t max_dotted_keys_depth = TOML_MAX_DOTTED_KEYS_DEPTH;

utf8_buffered_reader reader;
table root;
Expand Down Expand Up @@ -3085,6 +3086,11 @@ TOML_IMPL_NAMESPACE_START
// store segment
key_buffer.push_back(key_segment, key_begin, key_end);

if TOML_UNLIKELY(key_buffer.size() > max_dotted_keys_depth)
set_error_and_return_default("exceeded maximum dotted keys depth of "sv,
max_dotted_keys_depth,
" (TOML_MAX_DOTTED_KEYS_DEPTH)"sv);

// eof or no more key to come
if (is_eof() || *cp != U'.')
break;
Expand Down
4 changes: 4 additions & 0 deletions include/toml++/impl/preprocessor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1182,6 +1182,10 @@
// 256 is crazy high! if you're hitting this limit with real input, TOML is probably the wrong tool for the job...
#endif

#ifndef TOML_MAX_DOTTED_KEYS_DEPTH
#define TOML_MAX_DOTTED_KEYS_DEPTH 1024
#endif

#ifdef TOML_CHAR_8_STRINGS
#if TOML_CHAR_8_STRINGS
#error TOML_CHAR_8_STRINGS was removed in toml++ 2.0.0; all value setters and getters now work with char8_t strings implicitly.
Expand Down
12 changes: 11 additions & 1 deletion toml.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1091,6 +1091,10 @@
// 256 is crazy high! if you're hitting this limit with real input, TOML is probably the wrong tool for the job...
#endif

#ifndef TOML_MAX_DOTTED_KEYS_DEPTH
#define TOML_MAX_DOTTED_KEYS_DEPTH 1024
#endif

#ifdef TOML_CHAR_8_STRINGS
#if TOML_CHAR_8_STRINGS
#error TOML_CHAR_8_STRINGS was removed in toml++ 2.0.0; all value setters and getters now work with char8_t strings implicitly.
Expand Down Expand Up @@ -13554,7 +13558,8 @@ TOML_IMPL_NAMESPACE_START
class parser
{
private:
static constexpr size_t max_nested_values = TOML_MAX_NESTED_VALUES;
static constexpr size_t max_nested_values = TOML_MAX_NESTED_VALUES;
static constexpr size_t max_dotted_keys_depth = TOML_MAX_DOTTED_KEYS_DEPTH;

utf8_buffered_reader reader;
table root;
Expand Down Expand Up @@ -15575,6 +15580,11 @@ TOML_IMPL_NAMESPACE_START
// store segment
key_buffer.push_back(key_segment, key_begin, key_end);

if TOML_UNLIKELY(key_buffer.size() > max_dotted_keys_depth)
set_error_and_return_default("exceeded maximum dotted keys depth of "sv,
max_dotted_keys_depth,
" (TOML_MAX_DOTTED_KEYS_DEPTH)"sv);

// eof or no more key to come
if (is_eof() || *cp != U'.')
break;
Expand Down
1 change: 1 addition & 0 deletions tools/generate_single_header.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ def main():
r'TOML_LIB_PATCH',
r'TOML_LIB_SINGLE_HEADER',
r'TOML_MAX_NESTED_VALUES',
r'TOML_MAX_DOTTED_KEYS_DEPTH',
r'TOML_NAMESPACE_END',
r'TOML_NAMESPACE_START',
r'TOML_OPTIONAL_TYPE',
Expand Down
Loading