This is an automated email from the ASF dual-hosted git repository. rmiddleton pushed a commit to branch null-cast-check in repository https://gitbox.apache.org/repos/asf/logging-log4cxx.git
commit daa8ebbbff92102324e3e03bb6ae8c0508ae65a3 Author: Robert Middleton <[email protected]> AuthorDate: Sun Jan 9 14:38:26 2022 -0500 Fix NPE when attempting to cast a null pointer --- src/main/include/log4cxx/helpers/object.h | 5 +++++ src/test/cpp/helpers/casttestcase.cpp | 11 ++++++++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/main/include/log4cxx/helpers/object.h b/src/main/include/log4cxx/helpers/object.h index 18b6728..b8382a5 100644 --- a/src/main/include/log4cxx/helpers/object.h +++ b/src/main/include/log4cxx/helpers/object.h @@ -121,6 +121,11 @@ template<typename Ret, bool = std::is_base_of<Type, helpers::Object>::value> std::shared_ptr<Ret> cast(const std::shared_ptr<Type>& incoming) { + if(!incoming) + { + return std::shared_ptr<Ret>(); + } + Ret* casted = reinterpret_cast<Ret*>(const_cast<void*>(incoming->cast(Ret::getStaticClass()))); if ( casted ) diff --git a/src/test/cpp/helpers/casttestcase.cpp b/src/test/cpp/helpers/casttestcase.cpp index e7d8f53..4d2e5ab 100644 --- a/src/test/cpp/helpers/casttestcase.cpp +++ b/src/test/cpp/helpers/casttestcase.cpp @@ -33,7 +33,7 @@ LOGUNIT_CLASS(CastTestCase) LOGUNIT_TEST_SUITE( CastTestCase ); LOGUNIT_TEST(testGoodCast); LOGUNIT_TEST(testBadCast); - + LOGUNIT_TEST(testNullParameter); LOGUNIT_TEST_SUITE_END(); public: @@ -59,6 +59,15 @@ public: LOGUNIT_ASSERT(!fos); } + void testNullParameter() + { + OutputStreamPtr out = nullptr; + + FileOutputStreamPtr fos = log4cxx::cast<FileOutputStream>(out); + + LOGUNIT_ASSERT(!fos); + } + }; LOGUNIT_TEST_SUITE_REGISTRATION(CastTestCase);
