util/Exception: sanitize message strings

This should prevent leaking unsanitized strings from libraries.
This commit is contained in:
Max Kellermann
2024-06-18 17:20:06 +02:00
committed by Max Kellermann
parent abb23ba894
commit 7b938b4d14
2 changed files with 53 additions and 3 deletions

View File

@@ -6,6 +6,8 @@
#include <gtest/gtest.h>
using std::string_view_literals::operator""sv;
TEST(ExceptionTest, RuntimeError)
{
ASSERT_EQ(GetFullMessage(std::make_exception_ptr(std::runtime_error("Foo"))), "Foo");
@@ -22,6 +24,22 @@ TEST(ExceptionTest, DerivedError)
ASSERT_EQ(GetFullMessage(std::make_exception_ptr(DerivedError("Foo"))), "Foo");
}
TEST(ExceptionTest, GetFullMessageSanitize)
{
try {
try {
throw " ABC \n DEF ";
} catch (...) {
std::throw_with_nested(std::runtime_error{"foo\r\n\tbar"});
}
FAIL();
} catch (...) {
ASSERT_EQ(GetFullMessage(std::current_exception()),
"foo bar; ABC DEF"sv);
}
}
TEST(ExceptionTest, FindNestedDirect)
{
struct Foo {};