From 1b7ca8566b85777d1fc5a18a9568b31d5ef9e56d Mon Sep 17 00:00:00 2001 From: ToruNiina Date: Sun, 27 Jun 2021 15:58:40 +0900 Subject: [PATCH] fix: out_of_range with malformed toml file #164 --- toml/parser.hpp | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/toml/parser.hpp b/toml/parser.hpp index f6d6d83..fb29022 100644 --- a/toml/parser.hpp +++ b/toml/parser.hpp @@ -1268,7 +1268,10 @@ insert_nested_key(typename Value::table_type& root, const Value& v, } // the above if-else-if checks tab->at(k) is an array auto& a = tab->at(k).as_array(); - if(!(a.front().is_table())) + // If table element is defined as [[array_of_tables]], it + // cannot be an empty array. If an array of tables is + // defined as `aot = []`, it cannot be appended. + if(a.empty() || !(a.front().is_table())) { throw syntax_error(format_underline(concat_to_string( "toml::insert_value: array of table (\"", @@ -1510,6 +1513,7 @@ parse_inline_table(location& loc) { return err(kv_r.unwrap_err()); } + const auto& kvpair = kv_r.unwrap(); const std::vector& keys = kvpair.first.first; const auto& key_reg = kvpair.first.second; @@ -1526,10 +1530,19 @@ parse_inline_table(location& loc) using lex_table_separator = sequence, character<','>>; const auto sp = lex_table_separator::invoke(loc); + if(!sp) { maybe::invoke(loc); - if(loc.iter() != loc.end() && *loc.iter() == '}') + + if(loc.iter() == loc.end()) + { + throw syntax_error(format_underline( + "toml::parse_inline_table: missing table separator `}` ", + {{source_location(loc), "should be `}`"}}), + source_location(loc)); + } + else if(*loc.iter() == '}') { loc.advance(); // skip `}` return ok(std::make_pair(