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 5dc9c0af Add bounds checks to StringHelper::format placeholder 
handling (#655)
5dc9c0af is described below

commit 5dc9c0af638d054ea3ebf3efc10bc524e4401158
Author: jmestwa-coder <[email protected]>
AuthorDate: Sun May 10 11:47:38 2026 +0530

    Add bounds checks to StringHelper::format placeholder handling (#655)
---
 src/main/cpp/stringhelper.cpp                 | 26 ++++++++++++++------------
 src/test/cpp/helpers/stringhelpertestcase.cpp | 15 +++++++++++++++
 2 files changed, 29 insertions(+), 12 deletions(-)

diff --git a/src/main/cpp/stringhelper.cpp b/src/main/cpp/stringhelper.cpp
index a5d31626..6971ba60 100644
--- a/src/main/cpp/stringhelper.cpp
+++ b/src/main/cpp/stringhelper.cpp
@@ -174,22 +174,24 @@ LogString StringHelper::format(const LogString& pattern, 
const std::vector<LogSt
 {
 
        LogString result;
-       int i = 0;
+       LogString::size_type i = 0;
 
-       while (pattern[i] != 0)
+       while (i < pattern.length())
        {
-               if (pattern[i] == 0x7B /* '{' */ && pattern[i + 1] >= 0x30 /* 
'0' */ &&
+               if (i + 2 < pattern.length() &&
+                       pattern[i] == 0x7B /* '{' */ && pattern[i + 1] >= 0x30 
/* '0' */ &&
                        pattern[i + 1] <= 0x39 /* '9' */ && pattern[i + 2] == 
0x7D /* '}' */)
                {
-                       int arg = pattern[i + 1] - 0x30 /* '0' */;
-                       result = result + params[arg];
-                       i += 3;
-               }
-               else
-               {
-                       result = result + pattern[i];
-                       i++;
+                       LogString::size_type arg = pattern[i + 1] - 0x30 /* '0' 
*/;
+                       if (arg < params.size())
+                       {
+                               result = result + params[arg];
+                               i += 3;
+                               continue;
+                       }
                }
+               result = result + pattern[i];
+               i++;
        }
 
        return result;
@@ -199,4 +201,4 @@ LogString StringHelper::format(const LogString& pattern, 
const std::vector<LogSt
 void StringHelper::toString(int n, Pool& pool, LogString& dst) { toString(n, 
dst); }
 void StringHelper::toString(int64_t n, Pool& pool, LogString& dst) { 
toString(n, dst); }
 void StringHelper::toString(size_t n, Pool& pool, LogString& dst) { 
toString(n, dst); }
-#endif
\ No newline at end of file
+#endif
diff --git a/src/test/cpp/helpers/stringhelpertestcase.cpp 
b/src/test/cpp/helpers/stringhelpertestcase.cpp
index aba933e3..d4cc2165 100644
--- a/src/test/cpp/helpers/stringhelpertestcase.cpp
+++ b/src/test/cpp/helpers/stringhelpertestcase.cpp
@@ -42,6 +42,8 @@ LOGUNIT_CLASS(StringHelperTestCase)
        LOGUNIT_TEST( testEndsWith3 );
        LOGUNIT_TEST( testEndsWith4 );
        LOGUNIT_TEST( testEndsWith5 );
+       LOGUNIT_TEST( testFormatEmptyPattern );
+       LOGUNIT_TEST( testFormatMissingArgument );
        LOGUNIT_TEST_SUITE_END();
 
 
@@ -129,6 +131,19 @@ public:
                LOGUNIT_ASSERT_EQUAL(false, 
StringHelper::startsWith(LOG4CXX_STR("foobar"), LOG4CXX_STR("abc")));
        }
 
+       void testFormatEmptyPattern()
+       {
+               std::vector<LogString> params;
+               LOGUNIT_ASSERT_EQUAL((LogString) LOG4CXX_STR(""), 
StringHelper::format(LOG4CXX_STR(""), params));
+       }
+
+       void testFormatMissingArgument()
+       {
+               std::vector<LogString> params(1);
+               params[0] = LOG4CXX_STR("first");
+               LOGUNIT_ASSERT_EQUAL((LogString) LOG4CXX_STR("first {1}"), 
StringHelper::format(LOG4CXX_STR("{0} {1}"), params));
+       }
+
 
 };
 

Reply via email to