Data: qualify TOML parser limits (#46)

This commit is contained in:
faumaray committed 2026-12-03 15:49:25 +03:00
1 parent b3c566ed4d
commit 9ccfbf7838
2 files changed
+358 -6

No files matched your search

+298 -1
View File
@@ -232,4 +232,301 @@ Void::Testing::Test toml_reports_exact_origin_and_rejects_errors()
Void::Testing::expect_eq(
*super_after->root.find("a")->as_table()->find("y")->as_integer(),
std::int64_t{2});
}
}
namespace toml_limits_test_detail
{
[[nodiscard]] std::string repeated_key_path(std::size_t segments)
{
std::string out;
for (std::size_t i = 0; i < segments; ++i) {
if (i != 0) {
out.push_back('.');
}
out.push_back('a');
}
return out;
}
[[nodiscard]] std::string nested_arrays(std::size_t depth)
{
return std::string(depth, '[') + "1" + std::string(depth, ']');
}
[[nodiscard]] std::string nested_inline_tables(std::size_t depth)
{
std::string out;
for (std::size_t i = 0; i < depth; ++i) {
out += "{ k = ";
}
out.push_back('1');
for (std::size_t i = 0; i < depth; ++i) {
out += " }";
}
return out;
}
} // namespace toml_limits_test_detail
Void::Testing::Test toml_depth_limit_is_uniform_for_key_paths()
{
using namespace Void::Data::Toml;
using toml_limits_test_detail::repeated_key_path;
const std::string at_limit = repeated_key_path(64);
const std::string over_limit = repeated_key_path(65);
// Table headers count every segment as one nesting level.
const auto header_ok = parse("[" + at_limit + "]\nx = 1\n");
Void::Testing::require(header_ok.has_value());
const auto header_over = parse("[" + over_limit + "]\n");
Void::Testing::require(!header_over.has_value());
Void::Testing::expect_eq(header_over.error().origin.line,
std::size_t{1});
Void::Testing::expect_eq(header_over.error().message,
std::string{"key path exceeds max_depth"});
// Array-of-tables headers obey the same limit.
const auto aot_over = parse("[[" + over_limit + "]]\n");
Void::Testing::require(!aot_over.has_value());
// A dotted key of N segments nests its value N-1 levels, so it may use
// one more segment than a table header at the same limit.
const auto dotted_ok = parse(repeated_key_path(65) + " = 1\n");
Void::Testing::require(dotted_ok.has_value());
const auto dotted_over = parse(repeated_key_path(66) + " = 1\n");
Void::Testing::require(!dotted_over.has_value());
Void::Testing::expect_eq(dotted_over.error().message,
std::string{"key path exceeds max_depth"});
// Explicit small limits behave the same way.
Void::Testing::require(!parse("a.b.c = 1\n", ParseOptions{.max_depth = 1})
.has_value());
Void::Testing::require(parse("a.b = 1\n", ParseOptions{.max_depth = 1})
.has_value());
Void::Testing::require(parse("[a.b]\n", ParseOptions{.max_depth = 2})
.has_value());
Void::Testing::require(!parse("[a.b.c]\n", ParseOptions{.max_depth = 2})
.has_value());
}
Void::Testing::Test toml_depth_limit_is_uniform_for_values()
{
using namespace Void::Data::Toml;
using toml_limits_test_detail::nested_arrays;
using toml_limits_test_detail::nested_inline_tables;
Void::Testing::require(
parse("a = " + nested_arrays(64) + "\n").has_value());
Void::Testing::require(
!parse("a = " + nested_arrays(65) + "\n").has_value());
Void::Testing::require(
parse("a = " + nested_inline_tables(64) + "\n").has_value());
Void::Testing::require(
!parse("a = " + nested_inline_tables(65) + "\n").has_value());
}
Void::Testing::Test toml_depth_limit_combines_tables_and_values()
{
using namespace Void::Data::Toml;
using toml_limits_test_detail::nested_arrays;
// A value inside [t] starts one level deep, so only 63 nested arrays fit
// under the default limit of 64.
Void::Testing::require(
parse("[t]\na = " + nested_arrays(63) + "\n").has_value());
Void::Testing::require(
!parse("[t]\na = " + nested_arrays(64) + "\n").has_value());
// Dotted key paths contribute their intermediate tables too.
Void::Testing::require(parse("a.b = " + nested_arrays(63) + "\n")
.has_value());
Void::Testing::require(!parse("a.b = " + nested_arrays(64) + "\n")
.has_value());
}
Void::Testing::Test toml_input_byte_limit_boundaries()
{
using namespace Void::Data::Toml;
const std::string doc = "a = 1\n";
Void::Testing::require(
parse(doc, ParseOptions{.max_input_bytes = doc.size()}).has_value());
const auto under =
parse(doc, ParseOptions{.max_input_bytes = doc.size() - 1});
Void::Testing::require(!under.has_value());
Void::Testing::expect_eq(under.error().origin.line, std::size_t{1});
Void::Testing::expect_eq(under.error().message,
std::string{"TOML input exceeds max_input_bytes"});
// A huge but valid scalar fits under the default 16 MiB limit.
const std::string scalar_body(1024U * 1024U, 'x');
const std::string huge = "s = \"" + scalar_body + "\"\n";
const auto huge_ok = parse(huge);
Void::Testing::require(huge_ok.has_value());
Void::Testing::require(huge_ok->root.find("s") != nullptr);
Void::Testing::expect_eq(huge_ok->root.find("s")->as_string()->size(),
scalar_body.size());
Void::Testing::require(
!parse(huge, ParseOptions{.max_input_bytes = huge.size() - 1})
.has_value());
// A huge numeric scalar is rejected as out of range, not accepted.
const std::string huge_int = "i = " + std::string(64, '9') + "\n";
const auto int_over = parse(huge_int);
Void::Testing::require(!int_over.has_value());
Void::Testing::expect_eq(int_over.error().message,
std::string{"integer literal out of int64 range"});
}
Void::Testing::Test toml_node_limit_is_opt_in()
{
using namespace Void::Data::Toml;
// a = [1, 2, 3] creates one table entry plus three array elements.
const std::string doc = "a = [1, 2, 3]\n";
Void::Testing::require(
parse(doc, ParseOptions{.max_nodes = 4}).has_value());
const auto over = parse(doc, ParseOptions{.max_nodes = 3});
Void::Testing::require(!over.has_value());
Void::Testing::expect(
over.error().message.find("max_nodes") != std::string::npos);
// Default (zero) keeps the limit disabled.
Void::Testing::require(parse(doc).has_value());
// Dotted keys and array-of-tables entries count their intermediate
// containers as well.
Void::Testing::require(
parse("a.b.c = 1\n", ParseOptions{.max_nodes = 3}).has_value());
Void::Testing::require(
!parse("a.b.c = 1\n", ParseOptions{.max_nodes = 2}).has_value());
Void::Testing::require(
parse("[[p]]\n", ParseOptions{.max_nodes = 2}).has_value());
Void::Testing::require(
!parse("[[p]]\n", ParseOptions{.max_nodes = 1}).has_value());
}
Void::Testing::Test toml_rejects_invalid_utf8_with_stable_diagnostics()
{
using namespace Void::Data::Toml;
const std::string invalid = "a = 1\n\xFF = 2\n";
const auto first = parse(invalid);
const auto second = parse(invalid);
Void::Testing::require(!first.has_value());
Void::Testing::require(!second.has_value());
Void::Testing::expect_eq(first.error().origin.line, std::size_t{2});
Void::Testing::expect_eq(first.error().origin.column, std::size_t{1});
Void::Testing::expect_eq(
first.error().message,
std::string{"invalid UTF-8 byte sequence in TOML document"});
Void::Testing::expect_eq(second.error().origin.line,
first.error().origin.line);
Void::Testing::expect_eq(second.error().origin.column,
first.error().origin.column);
Void::Testing::expect_eq(second.error().message, first.error().message);
// Overlong encoding, UTF-16 surrogate and truncated sequence.
Void::Testing::require(!parse(std::string{"s = \"\xC0\x80\"\n"})
.has_value());
Void::Testing::require(!parse(std::string{"s = \"\xED\xA0\x80\"\n"})
.has_value());
Void::Testing::require(!parse(std::string{"s = \"\xE2\x82\"\n"})
.has_value());
// Valid multi-byte UTF-8 remains accepted.
const auto valid = parse("s = \"h\xC3\xA9llo \xF0\x9F\x98\x80\"\n");
Void::Testing::require(valid.has_value());
Void::Testing::expect_eq(valid->root.find("s")->as_string()->size(),
std::size_t{11});
}
Void::Testing::Test toml_duplicate_diagnostics_are_stable()
{
using namespace Void::Data::Toml;
const std::string dup = "a = 1\nb = 2\na = 3\n";
const auto first = parse(dup);
const auto second = parse(dup);
Void::Testing::require(!first.has_value());
Void::Testing::require(!second.has_value());
Void::Testing::expect_eq(first.error().origin.line, std::size_t{3});
Void::Testing::expect_eq(first.error().origin.column, std::size_t{1});
Void::Testing::expect_eq(first.error().message,
std::string{"duplicate key 'a'"});
Void::Testing::expect_eq(second.error().message, first.error().message);
Void::Testing::expect_eq(second.error().origin.line,
first.error().origin.line);
const auto dup_table = parse("[pkg]\nname = \"x\"\n[pkg]\n");
Void::Testing::require(!dup_table.has_value());
Void::Testing::expect_eq(dup_table.error().origin.line, std::size_t{3});
const auto dup_dotted = parse("a.b = 1\na.b = 2\n");
Void::Testing::require(!dup_dotted.has_value());
Void::Testing::expect_eq(dup_dotted.error().origin.line, std::size_t{2});
}
Void::Testing::Test toml_valid_documents_regression()
{
using namespace Void::Data::Toml;
constexpr std::string_view kDoc = R"toml(
title = "limits"
numbers = [1, 2.5, -3, 0x10]
matrix = [[1, 2], [3, 4]]
inline = { a = 1, b.c = "d", nested = { e = true } }
"quoted.key" = "value"
unicode = "héllo 😀"
[server]
host = "localhost"
ports = [8080, 8443]
[server.tls]
enabled = true
cipher = "TLS_AES_256_GCM_SHA384"
[[items]]
name = "one"
[[items]]
name = "two"
tags = ["a", "b"]
[items.meta]
count = 2
)toml";
const auto parsed = parse(kDoc);
Void::Testing::require(parsed.has_value());
const auto& root = parsed->root;
Void::Testing::require(root.find("matrix") != nullptr);
Void::Testing::expect_eq(root.find("matrix")->as_array()->size(),
std::size_t{2});
Void::Testing::require(root.find("inline") != nullptr);
const auto* inline_tbl = root.find("inline")->as_table();
Void::Testing::require(inline_tbl != nullptr);
Void::Testing::expect_eq(
*inline_tbl->find("b")->as_table()->find("c")->as_string(),
std::string{"d"});
const auto* items = root.find("items")->as_array_of_tables();
Void::Testing::require(items != nullptr);
Void::Testing::expect_eq(items->tables.size(), std::size_t{2});
Void::Testing::expect_eq(
*items->tables[1].find("meta")->as_table()->find("count")->as_integer(),
std::int64_t{2});
// Canonical serialization round-trips without changing the document.
const std::string once = serialize(*parsed);
const auto reparsed = parse(once);
Void::Testing::require(reparsed.has_value());
Void::Testing::expect_eq(serialize(*reparsed), once);
}