From 5b35c1a74e64f1b33cc18db43b1a7bbf842a619a Mon Sep 17 00:00:00 2001 From: ToruNiina Date: Thu, 19 Dec 2019 22:02:17 +0900 Subject: [PATCH] fix: prohibit modification on inline table According to toml-lang/toml:36d3091b3 "Clarify that inline tables are immutable", check if it adds key-value pair to an inline table. This is one of the unreleased (after-0.5.0) toml feature. But this is marked as "Clarify", so TOML-lang intended that inline tables are immutable in all version. --- toml/parser.hpp | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/toml/parser.hpp b/toml/parser.hpp index 9e511ad..1ea650f 100644 --- a/toml/parser.hpp +++ b/toml/parser.hpp @@ -1331,7 +1331,7 @@ insert_nested_key(typename Value::table_type& root, const Value& v, tab->insert(std::make_pair(k, v)); return ok(true); } - else + else // k is not the last one, we should insert recursively { // if there is no corresponding value, insert it first. // related: you don't need to write @@ -1347,6 +1347,27 @@ insert_nested_key(typename Value::table_type& root, const Value& v, // type checking... if(tab->at(k).is_table()) { + // According to toml-lang/toml:36d3091b3 "Clarify that inline + // tables are immutable", check if it adds key-value pair to an + // inline table. + // This is one of the unreleased (after-0.5.0) toml feature. + // But this is marked as "Clarify", so TOML-lang intended that + // inline tables are immutable in all version. + { + // here, if the value is a (multi-line) table, the region + // should be something like `[table-name]`. + if(get_region(tab->at(k)).str().front() == '{') + { + throw syntax_error(format_underline(concat_to_string( + "toml::insert_value: inserting to an inline table (", + format_dotted_keys(first, std::next(iter)), + ") but inline tables are immutable"), { + {std::addressof(get_region(tab->at(k))), + "inline tables are immutable"}, + {std::addressof(get_region(v)), "inserting this"} + }), v.location()); + } + } tab = std::addressof((*tab)[k].as_table()); } else if(tab->at(k).is_array()) // inserting to array-of-tables?