Skip to content

Commit

Permalink
Check if an error object's value member is a null pointer before prin…
Browse files Browse the repository at this point in the history
…ting it
  • Loading branch information
zajo committed Sep 9, 2024
1 parent 7358896 commit cc09447
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 7 deletions.
25 changes: 23 additions & 2 deletions include/boost/leaf/detail/print.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,19 +47,40 @@ namespace leaf_detail

////////////////////////////////////////

template <class T, class PrintableInfo, class CharT, class Traits>
bool print_impl(std::basic_ostream<CharT, Traits> & os, char const * & prefix, char const * delimiter, char const * mid, PrintableInfo const & x)
template <class T, class CharT, class Traits>
void print_name(std::basic_ostream<CharT, Traits> & os, char const * & prefix, char const * delimiter)
{
static_assert(show_in_diagnostics<T>::value, "show_in_diagnostics violation");
BOOST_LEAF_ASSERT(delimiter);
char const * p = prefix;
prefix = nullptr;
os << (p ? p : delimiter) << parse<T>();
}

template <class T, class PrintableInfo, class CharT, class Traits>
bool print_impl(std::basic_ostream<CharT, Traits> & os, char const * & prefix, char const * delimiter, char const * mid, PrintableInfo const & x)
{
print_name<T>(os, prefix, delimiter);
if( mid )
os << mid << x;
return true;
}

template <class T, class PrintableInfo, class CharT, class Traits>
bool print_impl(std::basic_ostream<CharT, Traits> & os, char const * & prefix, char const * delimiter, char const * mid, PrintableInfo const * x)
{
print_name<T>(os, prefix, delimiter);
if( mid )
{
os << mid;
if( x )
os << x;
else
os << "<nullptr>";
}
return true;
}

////////////////////////////////////////

template <
Expand Down
72 changes: 67 additions & 5 deletions test/diagnostics_test1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,16 @@ struct hidden_non_printable_info_printable_value
printable_value value;
};

struct value_nullptr
{
char const * value = nullptr;
};

struct value_ptr
{
char const * value = "\"value\"";
};

namespace boost { namespace leaf {

template <> struct show_in_diagnostics<hidden_printable_info_printable_value>: std::false_type { };
Expand Down Expand Up @@ -162,6 +172,8 @@ int main()
[]() -> leaf::result<void>
{
return BOOST_LEAF_NEW_ERROR(
value_nullptr(),
value_ptr(),
42,
printable_info_printable_value(),
printable_info_non_printable_value(),
Expand All @@ -173,6 +185,8 @@ int main()
leaf::e_errno{ENOENT} );
},
[](
value_nullptr,
value_ptr,
int,
printable_info_printable_value,
printable_info_non_printable_value,
Expand Down Expand Up @@ -206,6 +220,8 @@ int main()
[]() -> leaf::result<void>
{
return BOOST_LEAF_NEW_ERROR(
value_nullptr(),
value_ptr(),
42,
printable_info_printable_value(),
printable_info_non_printable_value(),
Expand All @@ -217,6 +233,8 @@ int main()
leaf::e_errno{ENOENT} );
},
[](
value_nullptr,
value_ptr,
int,
leaf::e_source_location,
printable_info_printable_value,
Expand Down Expand Up @@ -251,6 +269,8 @@ int main()
[]() -> leaf::result<void>
{
return BOOST_LEAF_NEW_ERROR(
value_nullptr(),
value_ptr(),
42,
printable_info_printable_value(),
printable_info_non_printable_value(),
Expand All @@ -262,6 +282,8 @@ int main()
leaf::e_errno{ENOENT} );
},
[](
value_nullptr,
value_ptr,
int,
leaf::e_source_location,
printable_info_printable_value,
Expand All @@ -283,7 +305,9 @@ int main()
BOOST_TEST(cmp(s,
"Error with serial #3 reported at <removed variance>"
"\nCaught:"
BOOST_LEAF_CFG_DIAGNOSTICS_FIRST_DELIMITER "int: 42"
BOOST_LEAF_CFG_DIAGNOSTICS_FIRST_DELIMITER "value_nullptr: <nullptr>"
BOOST_LEAF_CFG_DIAGNOSTICS_DELIMITER "value_ptr: \"value\""
BOOST_LEAF_CFG_DIAGNOSTICS_DELIMITER "int: 42"
BOOST_LEAF_CFG_DIAGNOSTICS_DELIMITER "printable_info_printable_value: printed printable_value"
BOOST_LEAF_CFG_DIAGNOSTICS_DELIMITER "printable_info_non_printable_value: *** printable_info non_printable_value ***"
BOOST_LEAF_CFG_DIAGNOSTICS_DELIMITER "non_printable_info_printable_value: printed printable_value"
Expand All @@ -310,6 +334,8 @@ int main()
[]() -> leaf::result<void>
{
return BOOST_LEAF_NEW_ERROR(
value_nullptr(),
value_ptr(),
42,
printable_info_printable_value(),
printable_info_non_printable_value(),
Expand All @@ -323,6 +349,8 @@ int main()
leaf::e_errno{ENOENT} );
},
[](
value_nullptr,
value_ptr,
int,
leaf::e_source_location,
printable_info_printable_value,
Expand All @@ -345,7 +373,9 @@ int main()
BOOST_TEST(cmp(s,
"Error with serial #4 reported at <removed variance>"
"\nCaught:"
BOOST_LEAF_CFG_DIAGNOSTICS_FIRST_DELIMITER "int: 42"
BOOST_LEAF_CFG_DIAGNOSTICS_FIRST_DELIMITER "value_nullptr: <nullptr>"
BOOST_LEAF_CFG_DIAGNOSTICS_DELIMITER "value_ptr: \"value\""
BOOST_LEAF_CFG_DIAGNOSTICS_DELIMITER "int: 42"
BOOST_LEAF_CFG_DIAGNOSTICS_DELIMITER "printable_info_printable_value: printed printable_value"
BOOST_LEAF_CFG_DIAGNOSTICS_DELIMITER "printable_info_non_printable_value: *** printable_info non_printable_value ***"
BOOST_LEAF_CFG_DIAGNOSTICS_DELIMITER "non_printable_info_printable_value: printed printable_value"
Expand All @@ -361,7 +391,9 @@ int main()
BOOST_TEST(cmp(s,
"Error with serial #4 reported at <removed variance>"
"\nCaught:"
BOOST_LEAF_CFG_DIAGNOSTICS_FIRST_DELIMITER "int: 42"
BOOST_LEAF_CFG_DIAGNOSTICS_FIRST_DELIMITER "value_nullptr: <nullptr>"
BOOST_LEAF_CFG_DIAGNOSTICS_DELIMITER "value_ptr: \"value\""
BOOST_LEAF_CFG_DIAGNOSTICS_DELIMITER "int: 42"
BOOST_LEAF_CFG_DIAGNOSTICS_DELIMITER "printable_info_printable_value: printed printable_value"
BOOST_LEAF_CFG_DIAGNOSTICS_DELIMITER "printable_info_non_printable_value: *** printable_info non_printable_value ***"
BOOST_LEAF_CFG_DIAGNOSTICS_DELIMITER "non_printable_info_printable_value: printed printable_value"
Expand Down Expand Up @@ -389,6 +421,8 @@ int main()
[]() -> leaf::result<void>
{
return BOOST_LEAF_NEW_ERROR(
value_nullptr(),
value_ptr(),
42,
printable_info_printable_value(),
printable_info_non_printable_value(),
Expand All @@ -413,7 +447,9 @@ int main()
BOOST_TEST(cmp(s,
"Error with serial #5 reported at <removed variance>"
"\nDiagnostic details:"
BOOST_LEAF_CFG_DIAGNOSTICS_FIRST_DELIMITER "int: 42"
BOOST_LEAF_CFG_DIAGNOSTICS_FIRST_DELIMITER "value_nullptr: <nullptr>"
BOOST_LEAF_CFG_DIAGNOSTICS_DELIMITER "value_ptr: \"value\""
BOOST_LEAF_CFG_DIAGNOSTICS_DELIMITER "int: 42"
BOOST_LEAF_CFG_DIAGNOSTICS_DELIMITER "printable_info_printable_value: printed printable_value"
BOOST_LEAF_CFG_DIAGNOSTICS_DELIMITER "printable_info_non_printable_value: *** printable_info non_printable_value ***"
BOOST_LEAF_CFG_DIAGNOSTICS_DELIMITER "non_printable_info_printable_value: printed printable_value"
Expand Down Expand Up @@ -452,6 +488,8 @@ int main()
[]
{
BOOST_LEAF_THROW_EXCEPTION( my_exception(),
value_nullptr(),
value_ptr(),
42,
printable_info_printable_value(),
printable_info_non_printable_value(),
Expand All @@ -463,6 +501,8 @@ int main()
leaf::e_errno{ENOENT} );
},
[](
value_nullptr,
value_ptr,
int,
printable_info_printable_value,
printable_info_non_printable_value,
Expand Down Expand Up @@ -493,6 +533,8 @@ int main()
[]
{
BOOST_LEAF_THROW_EXCEPTION( my_exception(),
value_nullptr(),
value_ptr(),
42,
printable_info_printable_value(),
printable_info_non_printable_value(),
Expand All @@ -504,6 +546,8 @@ int main()
leaf::e_errno{ENOENT} );
},
[](
value_nullptr,
value_ptr,
int,
leaf::e_source_location,
printable_info_printable_value,
Expand Down Expand Up @@ -535,6 +579,8 @@ int main()
[]
{
BOOST_LEAF_THROW_EXCEPTION( my_exception(),
value_nullptr(),
value_ptr(),
42,
printable_info_printable_value(),
printable_info_non_printable_value(),
Expand All @@ -546,6 +592,8 @@ int main()
leaf::e_errno{ENOENT} );
},
[](
value_nullptr,
value_ptr,
int,
leaf::e_source_location,
printable_info_printable_value,
Expand All @@ -568,6 +616,8 @@ int main()
"Error with serial #8 reported at <removed variance>"
"\nCaught:"
BOOST_LEAF_CFG_DIAGNOSTICS_FIRST_DELIMITER "<removed variance>: \"my_exception what\""
BOOST_LEAF_CFG_DIAGNOSTICS_DELIMITER "value_nullptr: <nullptr>"
BOOST_LEAF_CFG_DIAGNOSTICS_DELIMITER "value_ptr: \"value\""
BOOST_LEAF_CFG_DIAGNOSTICS_DELIMITER "int: 42"
BOOST_LEAF_CFG_DIAGNOSTICS_DELIMITER "printable_info_printable_value: printed printable_value"
BOOST_LEAF_CFG_DIAGNOSTICS_DELIMITER "printable_info_non_printable_value: *** printable_info non_printable_value ***"
Expand All @@ -593,6 +643,8 @@ int main()
[]
{
BOOST_LEAF_THROW_EXCEPTION( my_exception(),
value_nullptr(),
value_ptr(),
42,
printable_info_printable_value(),
printable_info_non_printable_value(),
Expand All @@ -606,6 +658,8 @@ int main()
leaf::e_errno{ENOENT} );
},
[](
value_nullptr,
value_ptr,
int,
leaf::e_source_location,
printable_info_printable_value,
Expand All @@ -629,6 +683,8 @@ int main()
"Error with serial #9 reported at <removed variance>"
"\nCaught:"
BOOST_LEAF_CFG_DIAGNOSTICS_FIRST_DELIMITER "<removed variance>: \"my_exception what\""
BOOST_LEAF_CFG_DIAGNOSTICS_DELIMITER "value_nullptr: <nullptr>"
BOOST_LEAF_CFG_DIAGNOSTICS_DELIMITER "value_ptr: \"value\""
BOOST_LEAF_CFG_DIAGNOSTICS_DELIMITER "int: 42"
BOOST_LEAF_CFG_DIAGNOSTICS_DELIMITER "printable_info_printable_value: printed printable_value"
BOOST_LEAF_CFG_DIAGNOSTICS_DELIMITER "printable_info_non_printable_value: *** printable_info non_printable_value ***"
Expand All @@ -646,6 +702,8 @@ int main()
"Error with serial #9 reported at <removed variance>"
"\nCaught:"
BOOST_LEAF_CFG_DIAGNOSTICS_FIRST_DELIMITER "<removed variance>: \"my_exception what\""
BOOST_LEAF_CFG_DIAGNOSTICS_DELIMITER "value_nullptr: <nullptr>"
BOOST_LEAF_CFG_DIAGNOSTICS_DELIMITER "value_ptr: \"value\""
BOOST_LEAF_CFG_DIAGNOSTICS_DELIMITER "int: 42"
BOOST_LEAF_CFG_DIAGNOSTICS_DELIMITER "printable_info_printable_value: printed printable_value"
BOOST_LEAF_CFG_DIAGNOSTICS_DELIMITER "printable_info_non_printable_value: *** printable_info non_printable_value ***"
Expand All @@ -672,6 +730,8 @@ int main()
[]
{
BOOST_LEAF_THROW_EXCEPTION( my_exception(),
value_nullptr(),
value_ptr(),
42,
printable_info_printable_value(),
printable_info_non_printable_value(),
Expand All @@ -698,7 +758,9 @@ int main()
"\nCaught:"
BOOST_LEAF_CFG_DIAGNOSTICS_FIRST_DELIMITER "<removed variance>: \"my_exception what\""
"\nDiagnostic details:"
BOOST_LEAF_CFG_DIAGNOSTICS_FIRST_DELIMITER "int: 42"
BOOST_LEAF_CFG_DIAGNOSTICS_FIRST_DELIMITER "value_nullptr: <nullptr>"
BOOST_LEAF_CFG_DIAGNOSTICS_DELIMITER "value_ptr: \"value\""
BOOST_LEAF_CFG_DIAGNOSTICS_DELIMITER "int: 42"
BOOST_LEAF_CFG_DIAGNOSTICS_DELIMITER "printable_info_printable_value: printed printable_value"
BOOST_LEAF_CFG_DIAGNOSTICS_DELIMITER "printable_info_non_printable_value: *** printable_info non_printable_value ***"
BOOST_LEAF_CFG_DIAGNOSTICS_DELIMITER "non_printable_info_printable_value: printed printable_value"
Expand Down

0 comments on commit cc09447

Please sign in to comment.