From b24742b3ec19c4f19f097a63c06570e7f6de6604 Mon Sep 17 00:00:00 2001 From: Johannes Spangenberg Date: Sun, 16 Jul 2017 14:30:48 +0200 Subject: [PATCH 1/4] fix suppressing I/O error in parse_config_file corresponding issue: https://svn.boost.org/trac10/ticket/13125 --- src/parsers.cpp | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/parsers.cpp b/src/parsers.cpp index dd62c66d37..3e435eea3f 100644 --- a/src/parsers.cpp +++ b/src/parsers.cpp @@ -152,7 +152,16 @@ namespace boost { namespace program_options { { boost::throw_exception(reading_file(filename)); } - return parse_config_file(strm, desc, allow_unregistered); + + basic_parsed_options result + = parse_config_file(strm, desc, allow_unregistered); + + if (strm.bad()) + { + boost::throw_exception(reading_file(filename)); + } + + return result; } template From 1083bac001498ae4d76105b94dd10e05e91bbab7 Mon Sep 17 00:00:00 2001 From: Johannes Spangenberg Date: Sun, 3 Mar 2019 17:39:48 +0100 Subject: [PATCH 2/4] add test about suppressing I/O errors --- test/parsers_test.cpp | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/test/parsers_test.cpp b/test/parsers_test.cpp index cc1bdd3f2e..f76ef9da9b 100644 --- a/test/parsers_test.cpp +++ b/test/parsers_test.cpp @@ -316,12 +316,22 @@ void test_unregistered() check_value(a3[1], "m1.v1", "1"); } +void test_fail_directory(const char* some_dir) +{ + options_description desc; + TEST_CHECK_THROW( + parse_config_file(some_dir, desc), + reading_file, + "providing directory as config file must cause exception") +} + int main(int, char* av[]) { test_command_line(); test_config_file(av[1]); test_environment(); test_unregistered(); + test_fail_directory("."); return 0; } From ea179b00ce6b47fb8b3f8ea1957f9d0b929f14dc Mon Sep 17 00:00:00 2001 From: Johannes Spangenberg Date: Sun, 3 Mar 2019 18:10:41 +0100 Subject: [PATCH 3/4] extend documentation of parse_config_file --- include/boost/program_options/parsers.hpp | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/include/boost/program_options/parsers.hpp b/include/boost/program_options/parsers.hpp index 0576469278..f9d7addda7 100644 --- a/include/boost/program_options/parsers.hpp +++ b/include/boost/program_options/parsers.hpp @@ -175,7 +175,9 @@ namespace boost { namespace program_options { /** Parse a config file. - Read from given stream. + Read from given stream. Errors while reading the stream are not + handled in any special manner. The method `bad()` on the given + stream can be used to check if an error has been occurred. */ template #if ! BOOST_WORKAROUND(__ICL, BOOST_TESTED_AT(700)) @@ -188,7 +190,8 @@ namespace boost { namespace program_options { /** Parse a config file. Read from file with the given name. The character type is - passed to the file stream. + passed to the file stream. An exception of type `reading_file` + is thrown when the file cannot be read. */ #ifdef BOOST_NO_CXX11_FUNCTION_TEMPLATE_DEFAULT_ARGS template From 348ec67c30147a8a85e4510e7ef867334afb7122 Mon Sep 17 00:00:00 2001 From: Johannes Spangenberg Date: Sun, 3 Mar 2019 20:14:10 +0100 Subject: [PATCH 4/4] fix failed template argument deduction in test --- test/parsers_test.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/parsers_test.cpp b/test/parsers_test.cpp index 9c8594193e..259ffa5a78 100644 --- a/test/parsers_test.cpp +++ b/test/parsers_test.cpp @@ -379,7 +379,7 @@ void test_fail_directory(const char* some_dir) { options_description desc; TEST_CHECK_THROW( - parse_config_file(some_dir, desc), + parse_config_file(some_dir, desc), reading_file, "providing directory as config file must cause exception") }