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 b9b3428b Avoid narrowing-sensitive arithmetic in FormattingInfo (#685)
b9b3428b is described below

commit b9b3428b3b1c9dc59aacf0d93609228481067ea2
Author: jmestwa-coder <[email protected]>
AuthorDate: Fri May 22 07:28:31 2026 +0530

    Avoid narrowing-sensitive arithmetic in FormattingInfo (#685)
    
    * Improve the FormatingInfo interface in the next ABI version
---
 src/main/cpp/formattinginfo.cpp                   | 26 +++++++++++++++++------
 src/main/cpp/patternlayout.cpp                    |  4 +---
 src/main/cpp/rollingpolicybase.cpp                |  2 +-
 src/main/include/log4cxx/pattern/formattinginfo.h | 11 ++++++++++
 src/test/cpp/pattern/patternparsertestcase.cpp    | 17 +++++++++++++--
 src/test/cpp/rolling/filenamepatterntestcase.cpp  |  4 ++--
 6 files changed, 49 insertions(+), 15 deletions(-)

diff --git a/src/main/cpp/formattinginfo.cpp b/src/main/cpp/formattinginfo.cpp
index 56ef54a8..7e322961 100644
--- a/src/main/cpp/formattinginfo.cpp
+++ b/src/main/cpp/formattinginfo.cpp
@@ -17,6 +17,7 @@
 
 #include <log4cxx/logstring.h>
 #include <log4cxx/pattern/formattinginfo.h>
+#include <algorithm>
 #include <limits.h>
 
 using namespace LOG4CXX_NS;
@@ -77,26 +78,37 @@ FormattingInfoPtr FormattingInfo::getDefault()
  * @param fieldStart start of field in buffer.
  * @param buffer buffer to be modified.
  */
+#if LOG4CXX_ABI_VERSION <= 15
 void FormattingInfo::format(const int fieldStart, LogString& buffer) const
 {
-       if (INT_MAX < buffer.length())
+       adjustField(static_cast<LogString::size_type>(std::max(fieldStart, 0)), 
buffer);
+}
+#endif
+void FormattingInfo::adjustField(const LogString::size_type fieldStart, 
LogString& buffer) const
+{
+       if (fieldStart > buffer.length())
+       {
                return;
-       int rawLength = static_cast<int>(buffer.length()) - fieldStart;
+       }
+
+       const LogString::size_type rawLength = buffer.length() - fieldStart;
+       const LogString::size_type maxLength = 
static_cast<LogString::size_type>(m_priv->maxLength);
+       const LogString::size_type minLength = 
static_cast<LogString::size_type>(m_priv->minLength);
 
-       if (rawLength > m_priv->maxLength)
+       if (rawLength > maxLength)
        {
                buffer.erase(buffer.begin() + fieldStart,
-                       buffer.begin() + fieldStart + (rawLength - 
m_priv->maxLength));
+                       buffer.begin() + fieldStart + (rawLength - maxLength));
        }
-       else if (rawLength < m_priv->minLength)
+       else if (rawLength < minLength)
        {
                if (m_priv->leftAlign)
                {
-                       buffer.append(m_priv->minLength - rawLength, (logchar) 
0x20 /* ' ' */);
+                       buffer.append(minLength - rawLength, (logchar) 0x20 /* 
' ' */);
                }
                else
                {
-                       buffer.insert(fieldStart, m_priv->minLength - 
rawLength, 0x20 /* ' ' */);
+                       buffer.insert(fieldStart, minLength - rawLength, 0x20 
/* ' ' */);
                }
        }
 }
diff --git a/src/main/cpp/patternlayout.cpp b/src/main/cpp/patternlayout.cpp
index 543fe9ab..5d67854a 100644
--- a/src/main/cpp/patternlayout.cpp
+++ b/src/main/cpp/patternlayout.cpp
@@ -120,8 +120,7 @@ void PatternLayout::format( 
LOG4CXX_FORMAT_LAYOUT_FORMAL_PARAMETERS ) const
        {
                auto startField = output.length();
                item->format(event, output);
-               if (startField < INT_MAX)
-                       
item->getFormattingInfo().format(static_cast<int>(startField), output);
+               item->getFormattingInfo().adjustField(startField, output);
        }
 
 }
@@ -271,4 +270,3 @@ pattern::PatternConverterPtr 
PatternLayout::createColorStartPatternConverter(con
 }
 
 
-
diff --git a/src/main/cpp/rollingpolicybase.cpp 
b/src/main/cpp/rollingpolicybase.cpp
index 61897519..bb951e65 100644
--- a/src/main/cpp/rollingpolicybase.cpp
+++ b/src/main/cpp/rollingpolicybase.cpp
@@ -122,7 +122,7 @@ void RollingPolicyBase::formatFileName(
        {
                auto startField = toAppendTo.length();
                item->format(obj, toAppendTo);
-               item->getFormattingInfo().format((int)startField, toAppendTo);
+               item->getFormattingInfo().adjustField(startField, toAppendTo);
        }
 }
 #if LOG4CXX_ABI_VERSION <= 15
diff --git a/src/main/include/log4cxx/pattern/formattinginfo.h 
b/src/main/include/log4cxx/pattern/formattinginfo.h
index f542e128..449f820c 100644
--- a/src/main/include/log4cxx/pattern/formattinginfo.h
+++ b/src/main/include/log4cxx/pattern/formattinginfo.h
@@ -85,13 +85,24 @@ class LOG4CXX_EXPORT FormattingInfo : public virtual 
LOG4CXX_NS::helpers::Object
                 */
                int getMaxLength() const;
 
+#if LOG4CXX_ABI_VERSION <= 15
                /**
                 * Adjust the content of the buffer based on the specified 
lengths and alignment.
+                * @deprecated This function is deprecated and will be removed 
in a future version.
                 *
                 * @param fieldStart start of field in buffer.
                 * @param buffer buffer to be modified.
                 */
+               [[ deprecated( "Use adjustField() instead" ) ]]
                void format(const int fieldStart, LogString& buffer) const;
+#endif
+               /**
+                * Adjust the content of the buffer based on the specified 
lengths and alignment.
+                *
+                * @param fieldStart start of field in buffer.
+                * @param buffer buffer to be modified.
+                */
+               void adjustField(const LogString::size_type fieldStart, 
LogString& buffer) const;
 };
 LOG4CXX_PTR_DEF(FormattingInfo);
 }
diff --git a/src/test/cpp/pattern/patternparsertestcase.cpp 
b/src/test/cpp/pattern/patternparsertestcase.cpp
index c0c46fde..4b07db24 100644
--- a/src/test/cpp/pattern/patternparsertestcase.cpp
+++ b/src/test/cpp/pattern/patternparsertestcase.cpp
@@ -27,6 +27,7 @@
 #include <log4cxx/spi/loggerrepository.h>
 #include <log4cxx/pattern/patternparser.h>
 #include <log4cxx/pattern/patternconverter.h>
+#include <log4cxx/pattern/formattinginfo.h>
 #include <log4cxx/helpers/relativetimedateformat.h>
 #include <log4cxx/helpers/simpledateformat.h>
 #include <log4cxx/helpers/transcoder.h>
@@ -81,6 +82,7 @@ LOGUNIT_CLASS(PatternParserTestCase)
        LOGUNIT_TEST(testMultiOption);
        LOGUNIT_TEST(testThreadUsername);
        LOGUNIT_TEST(testInvalidPatterns);
+       LOGUNIT_TEST(testFormattingInfoUsesSizeTypeFieldStart);
        LOGUNIT_TEST_SUITE_END();
 
        LoggingEventPtr event;
@@ -175,9 +177,9 @@ public:
 
                for (auto item : converters)
                {
-                       auto fieldStart = static_cast<int>(actual.length());
+                       auto fieldStart = actual.length();
                        item->format(event, actual);
-                       item->getFormattingInfo().format(fieldStart, actual);
+                       item->getFormattingInfo().adjustField(fieldStart, 
actual);
                }
 
                LOGUNIT_ASSERT_EQUAL(expected, actual);
@@ -297,6 +299,17 @@ public:
                        LOG4CXX_STR("%6.6666c"));
        }
 
+       void testFormattingInfoUsesSizeTypeFieldStart()
+       {
+               LogString actual(LOG4CXX_STR("prefix"));
+               auto fieldStart = actual.length();
+               actual.append(LOG4CXX_STR("abcdef"));
+
+               FormattingInfo(false, 0, 3).adjustField(fieldStart, actual);
+
+               LOGUNIT_ASSERT_EQUAL(LOG4CXX_STR("prefixdef"), actual);
+       }
+
 };
 
 //
diff --git a/src/test/cpp/rolling/filenamepatterntestcase.cpp 
b/src/test/cpp/rolling/filenamepatterntestcase.cpp
index cceda124..36daedde 100644
--- a/src/test/cpp/rolling/filenamepatterntestcase.cpp
+++ b/src/test/cpp/rolling/filenamepatterntestcase.cpp
@@ -74,9 +74,9 @@ public:
 
                for (auto item :  converters)
                {
-                       LogString::size_type i = result.length();
+                       auto fieldStart = result.length();
                        item->format(obj, result);
-                       item->getFormattingInfo().format((int)i, result);
+                       item->getFormattingInfo().adjustField(fieldStart, 
result);
                }
 
                return result;

Reply via email to