@@ -2262,14 +2262,21 @@ TOML_IMPL_NAMESPACE_START
22622262 }
22632263
22642264 // range check
2265- if TOML_UNLIKELY (result > static_cast <uint64_t >((std::numeric_limits<int64_t >::max)()) + (sign < 0 ? 1ull : 0ull ))
2265+ static constexpr auto i64_max = static_cast <uint64_t >((std::numeric_limits<int64_t >::max)());
2266+ if TOML_UNLIKELY (result > i64_max + (sign < 0 ? 1u : 0u ))
22662267 set_error_and_return_default (" '" sv,
22672268 traits::full_prefix,
22682269 std::string_view{ digits, length },
22692270 " ' is not representable in 64 bits" sv);
22702271
22712272 if constexpr (traits::is_signed)
2273+ {
2274+ // avoid signed multiply UB when parsing INT64_MIN
2275+ if TOML_UNLIKELY (sign < 0 && result == i64_max + 1u )
2276+ return (std::numeric_limits<int64_t >::min)();
2277+
22722278 return static_cast <int64_t >(result) * sign;
2279+ }
22732280 else
22742281 return static_cast <int64_t >(result);
22752282 }
@@ -3252,13 +3259,28 @@ TOML_IMPL_NAMESPACE_START
32523259
32533260 else if (auto tbl = matching_node.as_table (); !is_arr && tbl && !implicit_tables.empty ())
32543261 {
3255- if (auto found = impl::find (implicit_tables.begin (), implicit_tables.end (), tbl);
3256- found && (tbl->empty () || tbl->is_homogeneous <table>()))
3262+ if (auto found = impl::find (implicit_tables.begin (), implicit_tables.end (), tbl); found)
32573263 {
3258- implicit_tables.erase (implicit_tables.cbegin () + (found - implicit_tables.data ()));
3259- tbl->source_ .begin = header_begin_pos;
3260- tbl->source_ .end = header_end_pos;
3261- return tbl;
3264+ bool ok = true ;
3265+ if (!tbl->empty ())
3266+ {
3267+ for (auto & [_, child] : *tbl)
3268+ {
3269+ if (!child.is_table () && !child.is_array_of_tables ())
3270+ {
3271+ ok = false ;
3272+ break ;
3273+ }
3274+ }
3275+ }
3276+
3277+ if (ok)
3278+ {
3279+ implicit_tables.erase (implicit_tables.cbegin () + (found - implicit_tables.data ()));
3280+ tbl->source_ .begin = header_begin_pos;
3281+ tbl->source_ .end = header_end_pos;
3282+ return tbl;
3283+ }
32623284 }
32633285 }
32643286
0 commit comments