Fix UB on empty section header [] in config file parser (#158)

This commit is contained in:
Copilot
2026-05-28 21:42:27 +01:00
committed by GitHub
parent ae25c112b7
commit e173046605
2 changed files with 7 additions and 0 deletions
+2
View File
@@ -97,6 +97,8 @@ namespace boost { namespace program_options { namespace detail {
// Handle section name
if (*s.begin() == '[' && *s.rbegin() == ']') {
m_prefix = s.substr(1, s.size()-2);
if (m_prefix.empty())
boost::throw_exception(invalid_config_file_syntax(s, invalid_syntax::unrecognized_line));
if (*m_prefix.rbegin() != '.')
m_prefix += '.';
}
+5
View File
@@ -298,6 +298,11 @@ void test_config_file(const char* config_file)
check_value(a2[4], "m1.v1", "1");
check_value(a2[5], "m1.v2", "2");
check_value(a2[6], "m1.v3", "3");
// Test that an empty section header [] is rejected as invalid syntax (issue #156)
const char content_empty_section[] = "gv1 = 0\n[]\nv1 = 1\n";
stringstream ss_empty(content_empty_section);
BOOST_CHECK_THROW(parse_config_file(ss_empty, desc), invalid_config_file_syntax);
}
#if defined(__CYGWIN__)