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

swebb2066 pushed a commit to branch silence_address_sanitzer
in repository https://gitbox.apache.org/repos/asf/logging-log4cxx.git

commit c08a23bf5f57eff59945fb80a424c8093007d05e
Author: Stephen Webb <[email protected]>
AuthorDate: Thu Sep 25 13:13:37 2025 +1000

    Prevent unit test causing an address sanitizer error report
---
 src/test/cpp/rolling/timebasedrollingtest.cpp | 164 ++++++++++++++------------
 1 file changed, 90 insertions(+), 74 deletions(-)

diff --git a/src/test/cpp/rolling/timebasedrollingtest.cpp 
b/src/test/cpp/rolling/timebasedrollingtest.cpp
index 5b1e0826..3f88db14 100644
--- a/src/test/cpp/rolling/timebasedrollingtest.cpp
+++ b/src/test/cpp/rolling/timebasedrollingtest.cpp
@@ -176,13 +176,14 @@ private:
         * @param[in,opt]       startWith
         * @param[in]           waitFactor
         */
-       void logMsgAndSleep(
-               Pool&           pool,
-               size_t          howOften,
-               std::string     srcFunc,
-               size_t          srcLine,
-               size_t          startWith       = 0,
-               float           waitFactor      = 0.5)
+       void logMsgAndSleep
+               ( Pool&              pool
+               , int                howOften
+               , const std::string& srcFunc
+               , int                srcLine
+               , int                startWith = 0
+               , float              waitFactor = 0.5
+               )
        {
 #undef  LOG4CXX_LOCATION
 #define LOG4CXX_LOCATION ::log4cxx::spi::LocationInfo( \
@@ -191,13 +192,13 @@ private:
        srcFunc.c_str(),        \
        srcLine)
 
-               for (size_t i = startWith; i < startWith + howOften; ++i)
+               for (int i = startWith; i < startWith + howOften; ++i)
                {
                        std::string message("Hello---");
                        message.append(pool.itoa(i));
 
                        LOG4CXX_DEBUG(logger, message);
-                       current_time += (APR_USEC_PER_SEC * waitFactor);
+                       current_time += int(APR_USEC_PER_SEC * waitFactor);
                }
 
 #undef  LOG4CXX_LOCATION
@@ -220,18 +221,19 @@ private:
         * We don't use a wrapper macro this time because the src line should 
have the same name in all
         * compilers and is easily to add for the caller.
         * </p>
-        * @param[in,out]       pool
-        * @param[in]           prefix
-        * @param[in]           fname
-        * @param[in]           witnessIdx
-        * @param[in]           srcLine
+        * @param[in,out] pool
+        * @param[in]     prefix
+        * @param[in]     fname
+        * @param[in]     witnessIdx
+        * @param[in]     srcLine
         */
-       void compareWitness(
-                               Pool&           pool,
-               const   LogString&      prefix,
-               const   LogString&      fname,
-                               size_t          witnessIdx,
-                               size_t          srcLine)
+       void compareWitness
+                       ( Pool&            pool
+                       , const LogString& prefix
+                       , const LogString& fname
+                       , int              witnessIdx
+                       , int              srcLine
+                       )
        {
                LogString witness(LOG4CXX_STR("" DIR_PRE_WITNESS));
                witness.append(prefix);
@@ -248,21 +250,22 @@ private:
         * This method is a wrapper around {@link compareWitness}, used to 
iterate over all files from a
         * given test.
         * </p>
-        * @param[in,out]       pool
-        * @param[in]           prefix
-        * @param[in]           fnames
-        * @param[in]           srcLine
+        * @param[in,out] pool
+        * @param[in]     prefix
+        * @param[in]     fnames
+        * @param[in]     srcLine
         */
        template<size_t N>
-       void compareWitnesses(
-                               Pool&           pool,
-               const   LogString&      prefix,
-                               LogString       (&fnames)[N],
-                               size_t          srcLine)
+       void compareWitnesses
+               ( Pool&            pool
+               , const LogString& prefix
+               , const LogString (&fnames)[N]
+               , int              srcLine
+               )
        {
-               for (int i = 0; i < N; ++i)
+               for (size_t i = 0; i < N; ++i)
                {
-                       this->compareWitness(pool, prefix, fnames[i], i, 
srcLine);
+                       this->compareWitness(pool, prefix, fnames[i], int(i), 
srcLine);
                }
        }
 
@@ -283,14 +286,15 @@ private:
         * @param[in]           srcLine
         */
        template<size_t N>
-       void checkFilesExist(
-                               Pool&           pool,
-               const   LogString&      prefix,
-                               LogString       (&fnames)[N],
-                               size_t          witnessIdx,
-                               size_t          srcLine)
+       void checkFilesExist
+               ( Pool&            pool
+               , const LogString& prefix
+               , const LogString  (&fnames)[N]
+               , int              witnessIdx
+               , int              srcLine
+               )
        {
-               for (int i = 0; i < N - 1; ++i)
+               for (size_t i = 0; i < N - 1; ++i)
                {
                        //std::wcerr << L"Check: " << fnames[i] << L"\n";
                        LOGUNIT_ASSERT_EQUAL_SRCL(true, 
File(fnames[i]).exists(pool), srcLine);
@@ -362,6 +366,15 @@ private:
                //current_time++; // Need to not be at the top of a second for 
rollover logic to work correctly
                current_time = 1; // Start at about unix epoch
        }
+       static TimeBasedRollingTest* s_activeTestCase;
+
+       static log4cxx_time_t getCurrentTime()
+       {
+               log4cxx_time_t result{ 
std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::system_clock::now().time_since_epoch()).count()
 };
+               if (auto p = s_activeTestCase)
+                       result = p->currentTime();
+               return result;
+       }
 
 public:
        log4cxx_time_t currentTime()
@@ -376,11 +389,13 @@ public:
                ));
                this->logger = 
LogManager::getLogger("org.apache.log4j.TimeBasedRollingTest");
                this->setUpCurrTime();
-               helpers::Date::setGetCurrentTimeFunction( std::bind( 
&TimeBasedRollingTest::currentTime, this ) );
+               
helpers::Date::setGetCurrentTimeFunction(&TimeBasedRollingTest::getCurrentTime);
+               s_activeTestCase = this;
        }
 
        void tearDown()
        {
+               s_activeTestCase = nullptr;
                LogManager::shutdown();
        }
 
@@ -389,9 +404,9 @@ public:
         */
        void test1()
        {
-                               Pool            pool;
-               const   size_t          nrOfFnames(4);
-                               LogString       fnames[nrOfFnames];
+               Pool       pool;
+               const int  nrOfFnames(4);
+               LogString fnames[nrOfFnames];
 
                PatternLayoutPtr                layout( new 
PatternLayout(PATTERN_LAYOUT));
                RollingFileAppenderPtr  rfa(    new RollingFileAppender());
@@ -417,9 +432,9 @@ public:
         */
        void test2()
        {
-                               Pool            pool;
-               const   size_t          nrOfFnames(4);
-                               LogString       fnames[nrOfFnames];
+               Pool      pool;
+               const int nrOfFnames(4);
+               LogString fnames[nrOfFnames];
 
                PatternLayoutPtr                layout1(new 
PatternLayout(PATTERN_LAYOUT));
                RollingFileAppenderPtr  rfa1(   new RollingFileAppender());
@@ -462,9 +477,9 @@ public:
         */
        void test3()
        {
-                               Pool            pool;
-               const   size_t          nrOfFnames(4);
-                               LogString       fnames[nrOfFnames];
+               Pool      pool;
+               const int nrOfFnames(4);
+               LogString fnames[nrOfFnames];
 
                PatternLayoutPtr                layout( new 
PatternLayout(PATTERN_LAYOUT));
                RollingFileAppenderPtr  rfa(    new RollingFileAppender());
@@ -494,10 +509,10 @@ public:
         */
        void test4()
        {
-                               Pool            pool;
-               const   size_t          nrOfFnames(4);
-               const   size_t          nrOfLogMsgs(((nrOfFnames - 1) * 2) - 1);
-                               LogString       fnames[nrOfFnames];
+               Pool      pool;
+               const int nrOfFnames(4);
+               const int nrOfLogMsgs(((nrOfFnames - 1) * 2) - 1);
+               LogString fnames[nrOfFnames];
 
                PatternLayoutPtr                layout1(new 
PatternLayout(PATTERN_LAYOUT));
                RollingFileAppenderPtr  rfa1(   new RollingFileAppender());
@@ -520,8 +535,8 @@ public:
                logger->removeAppender(rfa1);
                rfa1->close();
 
-               PatternLayoutPtr                layout2(new 
PatternLayout(PATTERN_LAYOUT));
-               RollingFileAppenderPtr  rfa2(   new RollingFileAppender());
+               PatternLayoutPtr        layout2(new 
PatternLayout(PATTERN_LAYOUT));
+               RollingFileAppenderPtr rfa2(    new RollingFileAppender());
                rfa2->setLayout(layout2);
 
                TimeBasedRollingPolicyPtr tbrp2 = TimeBasedRollingPolicyPtr(new 
TimeBasedRollingPolicy());
@@ -547,13 +562,13 @@ public:
         */
        void test5()
        {
-                               Pool            pool;
-               const   size_t          nrOfFnames(4);
-               const   size_t          nrOfLogMsgs((nrOfFnames * 2) - 1);
-                               LogString       fnames[nrOfFnames];
+               Pool      pool;
+               const int nrOfFnames(4);
+               const int nrOfLogMsgs((nrOfFnames * 2) - 1);
+               LogString fnames[nrOfFnames];
 
-               PatternLayoutPtr                layout( new 
PatternLayout(PATTERN_LAYOUT));
-               RollingFileAppenderPtr  rfa(    new RollingFileAppender());
+               PatternLayoutPtr       layout(new 
PatternLayout(PATTERN_LAYOUT));
+               RollingFileAppenderPtr rfa(new RollingFileAppender());
                rfa->setLayout(layout);
 
                TimeBasedRollingPolicyPtr tbrp = TimeBasedRollingPolicyPtr(new 
TimeBasedRollingPolicy());
@@ -579,13 +594,13 @@ public:
         */
        void test6()
        {
-                               Pool            pool;
-               const   size_t          nrOfFnames(4);
-               const   size_t          nrOfLogMsgs((nrOfFnames * 2) - 1);
-                               LogString       fnames[nrOfFnames];
+               Pool      pool;
+               const int nrOfFnames(4);
+               const int nrOfLogMsgs((nrOfFnames * 2) - 1);
+               LogString fnames[nrOfFnames];
 
-               PatternLayoutPtr                layout( new 
PatternLayout(PATTERN_LAYOUT));
-               RollingFileAppenderPtr  rfa(    new RollingFileAppender());
+               PatternLayoutPtr       layout(  new 
PatternLayout(PATTERN_LAYOUT));
+               RollingFileAppenderPtr rfa(     new RollingFileAppender());
                rfa->setAppend(false);
                rfa->setLayout(layout);
 
@@ -619,13 +634,12 @@ public:
                typedef std::vector<Test> Tests;
 
                Tests   tests(10);
-               size_t  numTest = 0;
 
                tests.at(4) = &TimeBasedRollingTest::test4;
                tests.at(5) = &TimeBasedRollingTest::test5;
                tests.at(6) = &TimeBasedRollingTest::test6;
 
-               for (size_t numTest = 1; numTest < tests.size(); ++numTest)
+               for (int numTest = 1; numTest < tests.size(); ++numTest)
                {
                        Test test(tests.at(numTest));
                        if (!test)
@@ -640,13 +654,13 @@ public:
 
        void rollIntoDir()
        {
-                               Pool            pool;
-               const   size_t          nrOfFnames(4);
-               const   size_t          nrOfLogMsgs((nrOfFnames * 2) - 1);
-                               LogString       fnames[nrOfFnames];
+               Pool      pool;
+               const int nrOfFnames(4);
+               const int nrOfLogMsgs((nrOfFnames * 2) - 1);
+               LogString fnames[nrOfFnames];
 
-               PatternLayoutPtr                layout( new 
PatternLayout(PATTERN_LAYOUT));
-               RollingFileAppenderPtr  rfa(    new RollingFileAppender());
+               PatternLayoutPtr       layout(  new 
PatternLayout(PATTERN_LAYOUT));
+               RollingFileAppenderPtr rfa(     new RollingFileAppender());
                rfa->setLayout(layout);
                rfa->setFile(LOG4CXX_STR("" DIR_PRE_OUTPUT "rollIntoDir.log"));
 
@@ -682,4 +696,6 @@ public:
 
 };
 
+TimeBasedRollingTest* TimeBasedRollingTest::s_activeTestCase{ nullptr };
+
 LOGUNIT_TEST_SUITE_REGISTRATION(TimeBasedRollingTest);

Reply via email to