gitedmond opened a new pull request, #55865: URL: https://github.com/apache/arrow/pull/55865
### Rationale for this change `YearMonthDay::Exec` reserves a single element in each of its three child `Int64Builder`s, then appends every non-null row with `UnsafeAppend`, which does not check capacity. The builders round the reservation up to 32 elements, so for inputs longer than 32 rows the kernel writes past the end of the child buffers: rows from index 32 on come back wrong, earlier rows of `month`/`day` can be overwritten, and the heap is corrupted. `ISOCalendar::Exec` builds the same kind of struct and already reserves `in.length` for each child. ### What changes are included in this PR? - Reserve `in.length` elements in each `year_month_day` child builder, matching `ISOCalendar::Exec`. Null rows go through `StructBuilder::AppendNull`, which uses the children's checked `AppendEmptyValue`, so this covers every row. - Add `ScalarTemporalTest.YearMonthDayLongInput`: 100 rows, with and without nulls, for `date32`, `date64`, `timestamp[s]` and `timestamp[ns, UTC]`. The expected struct is built from `year`, `month` and `day` with the input's null bitmap (struct equality only compares children at valid parent slots, and `AppendNull` writes zeros rather than nulls into the children). ### Are these changes tested? Yes. All runs were on Linux x86_64 (WSL2 Ubuntu 24.04, GCC 13.3), Debug build with `-DARROW_USE_ASAN=ON`: - Before the fix, with the new test applied to current `main`, `YearMonthDayLongInput` aborts with `AddressSanitizer: heap-buffer-overflow ... WRITE of size 8`. The write is at `scalar_temporal_unary.cc:334` (`UnsafeAppend`), `0 bytes after 256-byte region` allocated by the `Reserve(1)` at line 384. This matches the valgrind report in the issue. - After the fix, `YearMonthDayLongInput` passes with `--gtest_repeat=3` and no sanitizer reports, and the full `arrow-compute-scalar-temporal-test` passes (56 tests, 1 skipped: `StrftimeOtherLocale`, because the locale isn't installed). - The Python reproducer from the issue, against released pyarrow 26.0.1, gives the reported output (zeros from row 32 on) and then segfaults. With `ARROW_DEFAULT_MEMORY_POOL=system` it aborts with glibc `corrupted size vs. prev_size`. - After the fix, I built pyarrow from this branch (Release) and checked `year_month_day` against `year`/`month`/`day` for 32, 40 and 1,000,000 rows. I ran each with and without nulls, for `date32`, `date64`, `timestamp[s]` and `timestamp[ns, UTC]`. All 24 cases were correct under both the default and the `system` memory pool, with no crash. ### Are there any user-facing changes? Yes: `pc.year_month_day` now returns correct results for inputs longer than 32 rows. **This PR contains a "Critical Fix".** Before this change, `year_month_day` produced incorrect data and wrote past the end of heap buffers (memory corruption, crashes) for any input longer than 32 rows. ### Was AI used for this PR? In accordance to the [AI generation guidelines](https://arrow.apache.org/docs/dev/developers/overview.html#ai-generated-code), please disclose below whether and how AI was used in this PR. **PR code and description written by:** - [ ] Human - [x] AI **Reviewed before submission by:** - [x] Human - [ ] AI - [ ] Not reviewed I used Claude Code to set up the build, write the regression test and the one-line fix, and draft this description. I reproduced the bug myself on pyarrow 26.0.1 and reviewed the AddressSanitizer failure before the fix and the passing runs after it. I also reviewed the diff and the reasoning for why `Reserve(in.length)` is enough when the input has nulls. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
