diff --git a/src/config_file.cpp b/src/config_file.cpp index d58652370..7cd18acdf 100644 --- a/src/config_file.cpp +++ b/src/config_file.cpp @@ -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 += '.'; } diff --git a/test/parsers_test.cpp b/test/parsers_test.cpp index 7eaaaebc5..6c62ef944 100644 --- a/test/parsers_test.cpp +++ b/test/parsers_test.cpp @@ -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__)