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

Gabriel39 pushed a commit to branch orc
in repository https://gitbox.apache.org/repos/asf/doris-thirdparty.git


The following commit(s) were added to refs/heads/orc by this push:
     new f346be42a52 [test](orc) Convert the timezone writer test's timestamps 
without setenv("TZ") (#446)
f346be42a52 is described below

commit f346be42a525f07dbaf099c29aa6962e84c0386e
Author: Mingyu Chen (Rayner) <[email protected]>
AuthorDate: Tue Dec 1 18:40:34 2026 +0800

    [test](orc) Convert the timezone writer test's timestamps without 
setenv("TZ") (#446)
    
    ### Background
    
    Apache Doris compiles ORC's C++ unit tests into its BE unit test binary
    (`doris_be_test`), where they run beside the Doris suites in one
    multi-threaded process.
    
    ### What problem does this PR solve?
    
    Issue Number: None
    Related PR: None
    PR Type: Test
    
    Problem Summary:
    
    `WriterTest.writeTimestampWithTimezone` turned each case's wall-clock string
    into seconds with `setenv("TZ", writerTimezone)`, `tzset()` and `mktime()`,
    and back with `setenv("TZ", readerTimezone)` and `localtime_r()`, then
    restored or removed `TZ`. Changing the environment is not safe while another
    thread reads it. In `doris_be_test` on macOS, brpc's bvar sampler thread 
runs
    `sysctl -n vm.loadavg` through `posix_spawn()` every second, which reads
    `environ`, and ASAN caught it reading the array that `setenv()` had just
    reallocated:
    
    ```
    ERROR: AddressSanitizer: heap-use-after-free ... READ of size 8 ... thread 
T1
      #0 PosixSpawnImpl ... #3 bvar::read_load_average ... #10 
bvar::detail::SamplerCollector::sampling_thread
    freed by thread T0 here:
      #0 realloc ...
    ```
    
    Each case now passes the UTC offsets of the writer's and the reader's zone 
at
    its wall-clock time, as the tz database gives them (what the old `isDst`
    hints stood for), and the test converts with `timegm()` and `gmtime_r()`,
    which do not read `TZ`. The check is unchanged: reading in the reader's zone
    must give back the wall-clock time that was written in the writer's zone.
    
    ### Details
    
    - **Frequency:** once across about ten full macOS runs of `doris_be_test`
      (ASAN). Not seen on Linux, where brpc reads `/proc/loadavg` instead of
      starting a process.
    - **Root cause:** the test changed the process environment while other
      threads could read it; `setenv()` is not thread-safe.
    - **Coverage:** unchanged: the same sixteen writer/reader zone pairs and
      times. The offsets come from the tz database, not from ORC's own timezone
      code, so the test still checks ORC against an independent source. The only
      ambiguous time (Los Angeles, 2012-11-04 01:59:59, which happened twice)
      keeps the daylight-time reading its `isDst` hint asked for.
    
    ### Testing
    
    - macOS arm64, clang 22.1.8, ASAN, inside Doris's `doris_be_test`:
      `OrcTest/WriterTest.*` passes.
    - The sixteen offsets were checked with Python's `zoneinfo`.
    - Not run: ORC's own CTest build, and Linux.
    
    ### Release note
    
    None
    
    ### Check List (For Author)
    
    - Behavior changed: No
    - Does this need documentation: No
    - Pre-submission status: Partially verified: ORC's own CTest build and 
Linux were not run
    
    ### AI Disclosure
    
    - Tool: Claude Code (Claude Opus 5.5)
    - Generated: the change, the investigation and this description
    - Human review: None yet
    
    Co-authored-by: morningman <[email protected]>
    Co-authored-by: Claude Opus 5.5 <[email protected]>
---
 c++/test/TestWriter.cc | 88 +++++++++++++++++++++++---------------------------
 1 file changed, 40 insertions(+), 48 deletions(-)

diff --git a/c++/test/TestWriter.cc b/c++/test/TestWriter.cc
index 803f14de780..49771423c1f 100644
--- a/c++/test/TestWriter.cc
+++ b/c++/test/TestWriter.cc
@@ -617,11 +617,14 @@ namespace orc {
 // a parameter to the getTimezoneByName function. Secondly, the
 // function of setting timezone in Windows is different from Linux.
 #ifndef _MSC_VER
+  // `writerOffset` and `readerOffset` are the UTC offsets in seconds of the 
two zones at `tsStr`,
+  // as the tz database gives them; converting with them through timegm() and 
gmtime_r() leaves the
+  // process environment alone. setenv("TZ") is not safe while another thread 
reads the
+  // environment, and the processes that run these tests can have such threads 
(in doris_be_test,
+  // brpc's bvar sampler runs commands through posix_spawn() on macOS).
   void testWriteTimestampWithTimezone(FileVersion fileVersion, const char* 
writerTimezone,
-                                      const char* readerTimezone, const 
std::string& tsStr,
-                                      int isDst = 0) {
-    char* tzBk = getenv("TZ");  // backup TZ env
-
+                                      int64_t writerOffset, const char* 
readerTimezone,
+                                      int64_t readerOffset, const std::string& 
tsStr) {
     MemoryOutputStream memStream(DEFAULT_MEM_STREAM_SIZE);
     MemoryPool* pool = getDefaultPool();
     std::unique_ptr<Type> 
type(Type::buildTypeFromString("struct<col1:timestamp>"));
@@ -638,14 +641,10 @@ namespace orc {
     auto& tsBatch = 
dynamic_cast<TimestampVectorBatch&>(*structBatch.fields[0]);
 
     // write timestamp in the writer timezone
-    setenv("TZ", writerTimezone, 1);
-    tzset();
     struct tm tm;
     memset(&tm, 0, sizeof(struct tm));
     strptime(tsStr.c_str(), "%Y-%m-%d %H:%M:%S", &tm);
-    // mktime() does depend on external hint for daylight saving time
-    tm.tm_isdst = isDst;
-    tsBatch.data[0] = mktime(&tm);
+    tsBatch.data[0] = timegm(&tm) - writerOffset;
     tsBatch.nanoseconds[0] = 0;
     structBatch.numElements = rowCount;
     tsBatch.numElements = rowCount;
@@ -659,60 +658,53 @@ namespace orc {
     EXPECT_EQ(true, rowReader->next(*batch));
 
     // verify we get same wall clock in reader timezone
-    setenv("TZ", readerTimezone, 1);
-    tzset();
     memset(&tm, 0, sizeof(struct tm));
-    time_t ttime = tsBatch.data[0];
-    localtime_r(&ttime, &tm);
+    time_t ttime = tsBatch.data[0] + readerOffset;
+    gmtime_r(&ttime, &tm);
     char buf[20];
     strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", &tm);
     EXPECT_TRUE(strncmp(buf, tsStr.c_str(), tsStr.size()) == 0);
-
-    // restore TZ env
-    if (tzBk) {
-      setenv("TZ", tzBk, 1);
-      tzset();
-    } else {
-      unsetenv("TZ");
-      tzset();
-    }
   }
 
   TEST_P(WriterTest, writeTimestampWithTimezone) {
-    const int IS_DST = 1, NOT_DST = 0;
-    testWriteTimestampWithTimezone(fileVersion, "GMT", "GMT", "2001-11-12 
18:31:01");
+    // UTC offsets: GMT, Asia/Shanghai, and America/Los_Angeles in standard 
and daylight time.
+    const int64_t GMT = 0, CST = 8 * 3600, PST = -8 * 3600, PDT = -7 * 3600;
+    testWriteTimestampWithTimezone(fileVersion, "GMT", GMT, "GMT", GMT, 
"2001-11-12 18:31:01");
     // behavior for Apache Orc (writer & reader timezone can change)
-    testWriteTimestampWithTimezone(fileVersion, "America/Los_Angeles", 
"America/Los_Angeles",
+    testWriteTimestampWithTimezone(fileVersion, "America/Los_Angeles", PST, 
"America/Los_Angeles",
+                                   PST, "2001-11-12 18:31:01");
+    testWriteTimestampWithTimezone(fileVersion, "Asia/Shanghai", CST, 
"Asia/Shanghai", CST,
+                                   "2001-11-12 18:31:01");
+    testWriteTimestampWithTimezone(fileVersion, "America/Los_Angeles", PST, 
"Asia/Shanghai", CST,
                                    "2001-11-12 18:31:01");
-    testWriteTimestampWithTimezone(fileVersion, "Asia/Shanghai", 
"Asia/Shanghai",
+    testWriteTimestampWithTimezone(fileVersion, "Asia/Shanghai", CST, 
"America/Los_Angeles", PST,
                                    "2001-11-12 18:31:01");
-    testWriteTimestampWithTimezone(fileVersion, "America/Los_Angeles", 
"Asia/Shanghai",
+    testWriteTimestampWithTimezone(fileVersion, "GMT", GMT, "Asia/Shanghai", 
CST,
                                    "2001-11-12 18:31:01");
-    testWriteTimestampWithTimezone(fileVersion, "Asia/Shanghai", 
"America/Los_Angeles",
+    testWriteTimestampWithTimezone(fileVersion, "Asia/Shanghai", CST, "GMT", 
GMT,
                                    "2001-11-12 18:31:01");
-    testWriteTimestampWithTimezone(fileVersion, "GMT", "Asia/Shanghai", 
"2001-11-12 18:31:01");
-    testWriteTimestampWithTimezone(fileVersion, "Asia/Shanghai", "GMT", 
"2001-11-12 18:31:01");
-    testWriteTimestampWithTimezone(fileVersion, "Asia/Shanghai", 
"America/Los_Angeles",
+    testWriteTimestampWithTimezone(fileVersion, "Asia/Shanghai", CST, 
"America/Los_Angeles", PST,
                                    "2018-01-01 23:59:59");
     // daylight saving started at 2012-03-11 02:00:00 in Los Angeles
-    testWriteTimestampWithTimezone(fileVersion, "America/Los_Angeles", 
"Asia/Shanghai",
-                                   "2012-03-11 01:59:59", NOT_DST);
-    testWriteTimestampWithTimezone(fileVersion, "America/Los_Angeles", 
"Asia/Shanghai",
-                                   "2012-03-11 03:00:00", IS_DST);
-    testWriteTimestampWithTimezone(fileVersion, "America/Los_Angeles", 
"Asia/Shanghai",
-                                   "2012-03-11 03:00:01", IS_DST);
-    // daylight saving ended at 2012-11-04 02:00:00 in Los Angeles
-    testWriteTimestampWithTimezone(fileVersion, "America/Los_Angeles", 
"Asia/Shanghai",
-                                   "2012-11-04 01:59:59", IS_DST);
-    testWriteTimestampWithTimezone(fileVersion, "America/Los_Angeles", 
"Asia/Shanghai",
-                                   "2012-11-04 02:00:00", NOT_DST);
-    testWriteTimestampWithTimezone(fileVersion, "America/Los_Angeles", 
"Asia/Shanghai",
-                                   "2012-11-04 02:00:01", NOT_DST);
+    testWriteTimestampWithTimezone(fileVersion, "America/Los_Angeles", PST, 
"Asia/Shanghai", CST,
+                                   "2012-03-11 01:59:59");
+    testWriteTimestampWithTimezone(fileVersion, "America/Los_Angeles", PDT, 
"Asia/Shanghai", CST,
+                                   "2012-03-11 03:00:00");
+    testWriteTimestampWithTimezone(fileVersion, "America/Los_Angeles", PDT, 
"Asia/Shanghai", CST,
+                                   "2012-03-11 03:00:01");
+    // daylight saving ended at 2012-11-04 02:00:00 in Los Angeles: 01:59:59 
happened twice, this
+    // is the first one, still in daylight time
+    testWriteTimestampWithTimezone(fileVersion, "America/Los_Angeles", PDT, 
"Asia/Shanghai", CST,
+                                   "2012-11-04 01:59:59");
+    testWriteTimestampWithTimezone(fileVersion, "America/Los_Angeles", PST, 
"Asia/Shanghai", CST,
+                                   "2012-11-04 02:00:00");
+    testWriteTimestampWithTimezone(fileVersion, "America/Los_Angeles", PST, 
"Asia/Shanghai", CST,
+                                   "2012-11-04 02:00:01");
     // other daylight saving time
-    testWriteTimestampWithTimezone(fileVersion, "America/Los_Angeles", 
"Asia/Shanghai",
-                                   "2014-06-06 12:34:56", IS_DST);
-    testWriteTimestampWithTimezone(fileVersion, "America/Los_Angeles", 
"America/Los_Angeles",
-                                   "2014-06-06 12:34:56", IS_DST);
+    testWriteTimestampWithTimezone(fileVersion, "America/Los_Angeles", PDT, 
"Asia/Shanghai", CST,
+                                   "2014-06-06 12:34:56");
+    testWriteTimestampWithTimezone(fileVersion, "America/Los_Angeles", PDT, 
"America/Los_Angeles",
+                                   PDT, "2014-06-06 12:34:56");
   }
 #endif
 


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to