Skip to content

Out-of-bounds read in toml::path::subpath(size_t, size_t) due to missing bounds clamping #319

Closed

Description

@VAHPEM

Environment

toml++ version and/or commit hash: v3.4.0 (also on current master, commit 1e8829b — identical code)

Compiler: gcc 13.3.0 (defect is compiler-independent; it is a missing bounds check in library code)

C++ standard mode: 17

Target arch: x86_64-linux-gnu

Library configuration overrides: none (reproduced in both header-only and library mode with TOML_HEADER_ONLY=0)

Relevant compilation flags: -O1 -g -fsanitize=address,undefined -fno-omit-frame-pointer

Describe the bug

toml::path::subpath(size_t start, size_t length) does not clamp its arguments before doing iterator arithmetic, so out-of-range start/length produce iterators past the end of components_ and the range insert reads out of bounds (heap-buffer-overflow).

The sibling index-based manipulators truncate(), truncated() and leaf() all clamp their argument to components_.size() first; subpath(size_t, size_t) is the only one that does not.

Running the PoC below under AddressSanitizer:

==ERROR: AddressSanitizer: heap-buffer-overflow ... READ of size 1
    #0 toml::v3::path_component::path_component(toml::v3::path_component const&)  path.inl:63
    ... std::vector<toml::v3::path_component>::_M_range_insert ...
    #8 toml::v3::path::subpath(const_iterator, const_iterator) const   path.inl:452
    #9 toml::v3::path::subpath(unsigned long, unsigned long) const     path.inl:459
    #10 main                                                           poc.cpp:8
0x... is located 3872 bytes after 160-byte region ...

In assertion-enabled builds the same input instead trips TOML_ASSERT(type_ == path_component_type::key) in path_component::key(); in NDEBUG builds it silently builds malformed components (seen downstream as std::length_error).

Steps to reproduce (or a small repro code sample)

poc.cpp:

#include <toml++/toml.hpp>

int main()
{
    toml::path p{ "a.b.c" };          // 3 components
    auto sp = p.subpath(100, 1);      // start far past the end
    (void)sp;
    return 0;
}
g++ -O1 -g -DNDEBUG -std=c++17 -fsanitize=address -fno-omit-frame-pointer poc.cpp -I include -o poc
ASAN_OPTIONS=detect_leaks=0 ./poc

Additional information

Clamping start/length to components_.size() (the same way truncate()/leaf() already do) fixes it:

diff --git a/include/toml++/impl/path.inl b/include/toml++/impl/path.inl
--- a/include/toml++/impl/path.inl
+++ b/include/toml++/impl/path.inl
@@ -456,4 +456,8 @@
 	TOML_EXTERNAL_LINKAGE
 	path path::subpath(size_t start, size_t length) const
 	{
+		if (start >= components_.size())
+			return {};
+		if (start + length > components_.size())
+			length = components_.size() - start;
 		return subpath(begin() + static_cast<int>(start), begin() + static_cast<int>(start + length));
 	}

Happy to open a pull request with this fix plus a regression test if that's useful.

Activity

  1. added
    bug
    on Oct 23, 2026
  2. added a commit that references this issue on Nov 5, 2026
    86e991d
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions