You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by ts...@apache.org on 2020/08/09 09:56:14 UTC

[logging-log4cxx] branch master updated: Fixed an infinite loop in "Transcoder::encode" (#34)

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

tschoening 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 b2190f1  Fixed an infinite loop in "Transcoder::encode" (#34)
b2190f1 is described below

commit b2190f1216401b9f49a3a3f332b0cd395c94b2a4
Author: Thorsten Schöning <62...@users.noreply.github.com>
AuthorDate: Sun Aug 9 11:56:05 2020 +0200

    Fixed an infinite loop in "Transcoder::encode" (#34)
    
    Fixed an infinite loop in "Transcoder::encode(const LogString&, std::wstring&)" by appending LOSSCHAR. The most likely alternative would be to throw on any invalid input instead, but that's not the case right now and LOSSCHAR used in other places as well.
    
    https://issues.apache.org/jira/browse/LOGCXX-398
    
    Co-authored-by: Robert Middleton <ro...@rm5248.com>
---
 src/changes/changes.xml                     |  1 +
 src/main/cpp/transcoder.cpp                 | 13 ++++++++++---
 src/test/cpp/helpers/transcodertestcase.cpp | 14 ++++++++++++++
 3 files changed, 25 insertions(+), 3 deletions(-)

diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 937fc4a..fb2f76f 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -58,6 +58,7 @@
 			<action issue="LOGCXX-411" type="fix">Crash when logging on multiple threads.</action>
 			<action issue="LOGCXX-400" type="fix">C++11 does not allow char literals with highest bit set unless cast</action>
 			<action issue="LOGCXX-399" type="fix">Non-ascii character output wrong.</action>
+			<action issue="LOGCXX-398" type="fix">Infinite loop in Transcoder::encode(const LogString&amp; src, std::wstring&amp; dst)</action>
 			<action issue="LOGCXX-394" type="fix">Levels are not thread safe</action>
 			<action issue="LOGCXX-388" type="fix">Hierarchy::updateParents loops forever on illegal logger-name like '.logger1'</action>
 			<action issue="LOGCXX-382" type="fix">Mingw build type conversion error</action>
diff --git a/src/main/cpp/transcoder.cpp b/src/main/cpp/transcoder.cpp
index d423fb4..76e9361 100644
--- a/src/main/cpp/transcoder.cpp
+++ b/src/main/cpp/transcoder.cpp
@@ -509,11 +509,18 @@ void Transcoder::encode(const LogString& src, std::wstring& dst)
 	dst.append(src);
 #else
 
-	for (LogString::const_iterator i = src.begin();
-		i != src.end();)
+	for (LogString::const_iterator i = src.begin(); i != src.end();)
 	{
 		unsigned int cp = Transcoder::decode(src, i);
-		encode(cp, dst);
+		if (cp != 0xFFFF)
+		{
+			encode(cp, dst);
+		}
+		else
+		{
+			dst.append(1, LOSSCHAR);
+			i++;
+		}
 	}
 
 #endif
diff --git a/src/test/cpp/helpers/transcodertestcase.cpp b/src/test/cpp/helpers/transcodertestcase.cpp
index 4f3a061..e806299 100644
--- a/src/test/cpp/helpers/transcodertestcase.cpp
+++ b/src/test/cpp/helpers/transcodertestcase.cpp
@@ -43,6 +43,7 @@ LOGUNIT_CLASS(TranscoderTestCase)
 	LOGUNIT_TEST(encode2);
 #if LOG4CXX_WCHAR_T_API
 	LOGUNIT_TEST(encode3);
+	LOGUNIT_TEST(encode3_1);
 #endif
 	LOGUNIT_TEST(encode4);
 #if LOG4CXX_WCHAR_T_API
@@ -163,6 +164,19 @@ public:
 		LOGUNIT_ASSERT_EQUAL(manyAs, encoded.substr(0, BUFSIZE - 3));
 		LOGUNIT_ASSERT_EQUAL(std::wstring(L"Hello"), encoded.substr(BUFSIZE - 3));
 	}
+
+	void encode3_1()
+	{
+		// Test invalid multibyte string
+		LogString greeting;
+		greeting.push_back( 0xff );
+		std::wstring encoded;
+		Transcoder::encode(greeting, encoded);
+
+		std::wstring expected;
+		expected.push_back( log4cxx::helpers::Transcoder::LOSSCHAR );
+		LOGUNIT_ASSERT_EQUAL(encoded, expected );
+	}
 #endif
 
 	void encode4()