You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by rm...@apache.org on 2022/01/15 01:53:07 UTC

[logging-log4cxx] branch master updated: Fix NPE when attempting to cast a null pointer (#101)

This is an automated email from the ASF dual-hosted git repository.

rmiddleton pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/logging-log4cxx.git


The following commit(s) were added to refs/heads/master by this push:
     new 94f57db  Fix NPE when attempting to cast a null pointer (#101)
94f57db is described below

commit 94f57dbc21c256af2352cac94aa668be841c7239
Author: Robert Middleton <rm...@users.noreply.github.com>
AuthorDate: Fri Jan 14 20:53:00 2022 -0500

    Fix NPE when attempting to cast a null pointer (#101)
---
 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);