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 861e944d Handle zero-length writes in ByteArrayOutputStream (#684)
861e944d is described below

commit 861e944dc879b2df54d1db160ea045f9fb88927c
Author: jmestwa-coder <[email protected]>
AuthorDate: Fri May 22 07:36:48 2026 +0530

    Handle zero-length writes in ByteArrayOutputStream (#684)
---
 src/main/cpp/bytearrayoutputstream.cpp | 14 ++++++++------
 src/test/cpp/helpers/casttestcase.cpp  | 14 ++++++++++++++
 2 files changed, 22 insertions(+), 6 deletions(-)

diff --git a/src/main/cpp/bytearrayoutputstream.cpp 
b/src/main/cpp/bytearrayoutputstream.cpp
index 0c7ce443..2c74a23b 100644
--- a/src/main/cpp/bytearrayoutputstream.cpp
+++ b/src/main/cpp/bytearrayoutputstream.cpp
@@ -19,7 +19,6 @@
 #include <log4cxx/helpers/bytearrayoutputstream.h>
 #include <log4cxx/helpers/exception.h>
 #include <log4cxx/helpers/bytebuffer.h>
-#include <string.h>
 
 using namespace LOG4CXX_NS;
 using namespace LOG4CXX_NS::helpers;
@@ -51,15 +50,19 @@ void ByteArrayOutputStream::flush( 
LOG4CXX_FLUSH_OUTPUT_STREAM_FORMAL_PARAMETERS
 void ByteArrayOutputStream::write( 
LOG4CXX_WRITE_OUTPUT_STREAM_FORMAL_PARAMETERS )
 {
        const size_t count = buf.remaining();
-       const size_t sz = m_priv->array.size();
 
-       if (count > m_priv->array.max_size() - sz)
+       if (count == 0)
+       {
+               return;
+       }
+
+       if (count > m_priv->array.max_size() - m_priv->array.size())
        {
                throw 
IllegalArgumentException(LOG4CXX_STR("ByteArrayOutputStream::write overflow"));
        }
 
-       m_priv->array.resize(sz + count);
-       memcpy(&m_priv->array[sz], buf.current(), count);
+       const char* const current = buf.current();
+       m_priv->array.insert(m_priv->array.end(), current, current + count);
        buf.increment_position(count);
 }
 
@@ -69,4 +72,3 @@ std::vector<unsigned char> 
ByteArrayOutputStream::toByteArray() const
 }
 
 
-
diff --git a/src/test/cpp/helpers/casttestcase.cpp 
b/src/test/cpp/helpers/casttestcase.cpp
index 01597108..de6b654f 100644
--- a/src/test/cpp/helpers/casttestcase.cpp
+++ b/src/test/cpp/helpers/casttestcase.cpp
@@ -38,6 +38,7 @@ LOGUNIT_CLASS(CastTestCase)
        LOGUNIT_TEST(testNullParameter);
        LOGUNIT_TEST(testRollingFileAppender);
        LOGUNIT_TEST(testByteArrayOutputStreamWriteAfterDefaultConstruction);
+       LOGUNIT_TEST(testByteArrayOutputStreamIgnoresEmptyWrite);
        LOGUNIT_TEST_SUITE_END();
 
 public:
@@ -105,6 +106,19 @@ public:
                }
        }
 
+       void testByteArrayOutputStreamIgnoresEmptyWrite()
+       {
+               ByteArrayOutputStreamPtr stream = 
std::make_shared<ByteArrayOutputStream>();
+
+               char payload[] = { 'x' };
+               ByteBuffer buf(payload, 0);
+
+               stream->write(buf);
+
+               LOGUNIT_ASSERT_EQUAL(size_t(0), stream->toByteArray().size());
+               LOGUNIT_ASSERT_EQUAL(size_t(0), buf.remaining());
+       }
+
 };
 
 LOGUNIT_TEST_SUITE_REGISTRATION(CastTestCase);

Reply via email to