Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix suppressing I/O error in parse_config_file #29

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions include/boost/program_options/parsers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<class charT>
#if ! BOOST_WORKAROUND(__ICL, BOOST_TESTED_AT(700))
Expand All @@ -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<class charT>
Expand Down
11 changes: 10 additions & 1 deletion src/parsers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<charT> result
= parse_config_file(strm, desc, allow_unregistered);

if (strm.bad())
{
boost::throw_exception(reading_file(filename));
}

return result;
}

template
Expand Down
10 changes: 10 additions & 0 deletions test/parsers_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,15 @@ 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<char>(some_dir, desc),
reading_file,
"providing directory as config file must cause exception")
}



int main(int, char* av[])
Expand All @@ -383,6 +392,7 @@ int main(int, char* av[])
test_config_file(av[1]);
test_environment();
test_unregistered();
test_fail_directory(".");
return 0;
}