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

swebb2066 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 25b0d401 Fix null deref in ByteArrayOutputStream default constructor 
(#679)
25b0d401 is described below

commit 25b0d4015726556b91e7909dd41fa8f503a158d0
Author: metsw24-max <[email protected]>
AuthorDate: Tue May 19 07:08:51 2026 +0530

    Fix null deref in ByteArrayOutputStream default constructor (#679)
---
 src/main/cpp/bytearrayoutputstream.cpp |  1 +
 src/test/cpp/helpers/casttestcase.cpp  | 26 ++++++++++++++++++++++++++
 2 files changed, 27 insertions(+)

diff --git a/src/main/cpp/bytearrayoutputstream.cpp 
b/src/main/cpp/bytearrayoutputstream.cpp
index f98f09ae..0c7ce443 100644
--- a/src/main/cpp/bytearrayoutputstream.cpp
+++ b/src/main/cpp/bytearrayoutputstream.cpp
@@ -32,6 +32,7 @@ struct ByteArrayOutputStream::ByteArrayOutputStreamPriv
 IMPLEMENT_LOG4CXX_OBJECT(ByteArrayOutputStream)
 
 ByteArrayOutputStream::ByteArrayOutputStream()
+       : m_priv(std::make_unique<ByteArrayOutputStreamPriv>())
 {
 }
 
diff --git a/src/test/cpp/helpers/casttestcase.cpp 
b/src/test/cpp/helpers/casttestcase.cpp
index 53e6de54..01597108 100644
--- a/src/test/cpp/helpers/casttestcase.cpp
+++ b/src/test/cpp/helpers/casttestcase.cpp
@@ -17,6 +17,7 @@
 
 #include "../logunit.h"
 #include <log4cxx/helpers/bytearrayoutputstream.h>
+#include <log4cxx/helpers/bytebuffer.h>
 #include <log4cxx/helpers/fileoutputstream.h>
 #include <log4cxx/rolling/rollingfileappender.h>
 #include <iostream>
@@ -36,6 +37,7 @@ LOGUNIT_CLASS(CastTestCase)
        LOGUNIT_TEST(testBadCast);
        LOGUNIT_TEST(testNullParameter);
        LOGUNIT_TEST(testRollingFileAppender);
+       LOGUNIT_TEST(testByteArrayOutputStreamWriteAfterDefaultConstruction);
        LOGUNIT_TEST_SUITE_END();
 
 public:
@@ -79,6 +81,30 @@ public:
                LOGUNIT_ASSERT(appender);
        }
 
+       /**
+        * The default constructor of ByteArrayOutputStream left its private
+        * unique_ptr default-initialised (nullptr), so the first call into
+        * write() / toByteArray() dereferenced a null pointer. Verify that
+        * a freshly constructed instance can accept input and round-trip it
+        * back through toByteArray() without crashing.
+        */
+       void testByteArrayOutputStreamWriteAfterDefaultConstruction()
+       {
+               ByteArrayOutputStreamPtr stream = 
std::make_shared<ByteArrayOutputStream>();
+
+               char payload[] = { 'l', 'o', 'g', '4', 'c', 'x', 'x' };
+               ByteBuffer buf(payload, sizeof(payload));
+
+               stream->write(buf);
+
+               ByteList result = stream->toByteArray();
+               LOGUNIT_ASSERT_EQUAL(sizeof(payload), result.size());
+               for (size_t i = 0; i < sizeof(payload); ++i)
+               {
+                       LOGUNIT_ASSERT_EQUAL(static_cast<unsigned 
char>(payload[i]), result[i]);
+               }
+       }
+
 };
 
 LOGUNIT_TEST_SUITE_REGISTRATION(CastTestCase);

Reply via email to