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 2b049d03 Fix size_t from int implict conversion in 
MDCPatternConverter::format()
2b049d03 is described below

commit 2b049d03fd476ad72693ce7a9b323b4eabce8f19
Author: Ray Chern <[email protected]>
AuthorDate: Mon Jul 20 14:03:26 2026 +0800

    Fix size_t from int implict conversion in MDCPatternConverter::format()
---
 src/main/cpp/mdcpatternconverter.cpp |  4 +++-
 src/test/cpp/mdctestcase.cpp         | 17 +++++++++++++++++
 2 files changed, 20 insertions(+), 1 deletion(-)

diff --git a/src/main/cpp/mdcpatternconverter.cpp 
b/src/main/cpp/mdcpatternconverter.cpp
index a4850eaf..c3386117 100644
--- a/src/main/cpp/mdcpatternconverter.cpp
+++ b/src/main/cpp/mdcpatternconverter.cpp
@@ -51,7 +51,9 @@ void MDCPatternConverter::format( 
LOG4CXX_FORMAT_EVENT_FORMAL_PARAMETERS ) const
                const size_t separCount = 2;
                const size_t quoteCount = 2;
                LogString separ = LOG4CXX_STR("{");
-               size_t remainingLength = info.getMaxLength() - 1;
+               size_t remainingLength = info.getMaxLength() > 0
+                       ? static_cast<size_t>(info.getMaxLength()) - 1
+                       : 0;
                for (auto key : event->getMDCKeySet())
                {
                        LogString value;
diff --git a/src/test/cpp/mdctestcase.cpp b/src/test/cpp/mdctestcase.cpp
index e629ce4e..ccefcd6a 100644
--- a/src/test/cpp/mdctestcase.cpp
+++ b/src/test/cpp/mdctestcase.cpp
@@ -19,6 +19,8 @@
 #include <log4cxx/mdc.h>
 #include <log4cxx/file.h>
 #include <log4cxx/logger.h>
+#include <log4cxx/pattern/formattinginfo.h>
+#include <log4cxx/pattern/mdcpatternconverter.h>
 #include <log4cxx/patternlayout.h>
 #include "insertwide.h"
 #include "logunit.h"
@@ -33,6 +35,7 @@ LOGUNIT_CLASS(MDCTestCase)
        LOGUNIT_TEST_SUITE(MDCTestCase);
        LOGUNIT_TEST(test1);
        LOGUNIT_TEST(test2);
+       LOGUNIT_TEST(test3);
        LOGUNIT_TEST_SUITE_END();
 
 public:
@@ -74,6 +77,20 @@ public:
                l.format(output, 
std::make_shared<spi::LoggingEvent>(LOG4CXX_STR("MDC.LayoutTest"), 
Level::getInfo(), LOG4CXX_STR("Message"), 
spi::LocationInfo::getLocationUnavailable()));
                LOGUNIT_ASSERT_EQUAL(LOG4CXX_STR("INFO  MDC.LayoutTest - 
{\"key1\":\"value1\"} Message"), output);
        }
+
+       /// A maximum field length of zero must suppress all MDC content.
+       void test3()
+       {
+               pattern::MDCPatternConverter converter;
+               bool leftAlign{false};
+               const int minLength{0}, maxLength{0};
+               
converter.setFormattingInfo(std::make_shared<pattern::FormattingInfo>(leftAlign,
 minLength, maxLength));
+               MDC item1("key1", "value1");
+               auto e = 
std::make_shared<spi::LoggingEvent>(LOG4CXX_STR("MDC.LayoutTest"), 
Level::getInfo(), LOG4CXX_STR("Message"), 
spi::LocationInfo::getLocationUnavailable());
+               LogString output;
+               converter.format(e, output);
+               LOGUNIT_ASSERT_EQUAL(LOG4CXX_STR(""), output);
+       }
 };
 
 LOGUNIT_TEST_SUITE_REGISTRATION(MDCTestCase);

Reply via email to