[C++][Compute] year_month_day writes past its child buffers for inputs longer than 32 rows (wrong results, heap corruption)
维护者通常 1 天内回复
还没有人认领这个 Issue。
评估
调研方向
YearMonthDay::Exec 位于 cpp/src/arrow/compute/kernels/scalar_temporal_unary.cc,从三个子 Int64Builder 上的 Reserve(1) 调用开始阅读,然后与同一文件中的 ISOCalendar::Exec 进行比较,后者为每个子项预留 in.length。修改预留以使其一致,并确认 visitor 的 UnsafeAppend 路径现在保持在边界内。完成标准是:一个超过 32 行的测试(现有 fixture 较短)对 date32、date64 和 timestamp 输入返回正确的值,并且 fuzzing/valgrind 报告的越界写入已消失。
由索引模型根据 Issue 内容生成。
描述
Describe the bug, including details regarding any error messages, version, and platform.
year_month_day returns wrong values for any input with more than 32 rows, and the kernel writes past the end of its output buffers, corrupting the heap.
import pyarrow as pa
import pyarrow.compute as pc
dates = pa.array(range(40), type=pa.int32()).view(pa.date32())
ymd = pc.year_month_day(dates)
for d, r in list(zip(dates.to_pylist(), ymd.to_pylist()))[30:36]:
print(d, r)
Output with pyarrow 26.0.1 on Linux x86_64:
1970-01-31 {'year': 1970, 'month': 1, 'day': 31}
1970-02-01 {'year': 1970, 'month': 2, 'day': 1}
1970-02-02 {'year': 0, 'month': 0, 'day': 0}
1970-02-03 {'year': 0, 'month': 0, 'day': 0}
1970-02-04 {'year': 0, 'month': 0, 'day': 0}
1970-02-05 {'year': 0, 'month': 0, 'day': 0}
Every row from index 32 on is wrong. The first rows of the month and day children are overwritten too: rows 0-7 come back as (1970, 1970, 2). On 1,000,000 dates, row 0 comes back as {'year': 1970, 'month': 1970, 'day': 1970}. While fuzzing compute kernels, I also saw later, unrelated calls in the same process segfault.
All accepted input types are affected: date32, date64, and timestamp with or without a time zone. I reproduced this in pyarrow 12.0.1, 15.0.2, 18.1.0, 21.0.0, 24.0.0 and 26.0.1. year, month, day and iso_calendar return correct results for the same inputs.
Cause. In YearMonthDay::Exec, each of the three child Int64Builders reserves only one element (scalar_temporal_unary.cc#L384):
RETURN_NOT_OK(field_builders[i]->Reserve(1));
The visitor then appends every value with UnsafeAppend, which does not check capacity. The builder rounds the reservation up to its 32-element minimum, so rows after the 32nd are written past the allocation. Null rows go through StructBuilder::AppendNull, which grows the children, so inputs with nulls show fewer wrong rows but are still affected. ISOCalendar::Exec builds the same kind of struct and reserves in.length for each child (#L1547).
Valgrind, run with ARROW_DEFAULT_MEMORY_POOL=system, reports the out-of-bounds write:
Invalid write of size 8
at ...YearMonthDayVisitValueFunction<..., arrow::Date32Type, ...>::Get(...)::{lambda(int)#1}
by ...YearMonthDay<std::chrono::duration<int, std::ratio<86400l, 1l> >, arrow::Date32Type>::Exec(...)
Address 0x1054a580 is 0 bytes after a block of size 256 alloc'd
256 bytes is 32 int64 values. The existing year_month_day tests use fixtures shorter than 32 rows, so they don't exercise this.
Component(s)
C++
- 主要语言
- C++
- 星标
- 17.3k
- 派生
- 4.5k
- 平均合并
- 3 天 14 小时
- 30 天内合并 PR
- 90
环境准备
- 提供 Dockerfile 或 Docker Compose 文件
- 有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
apache/arrow 的其他 Issue
-
[R] abandon_ship() looks up `call` with an inheriting get(), so its "no call" guard never fires可能已有人在做 关联的 PR 仍在进行中或已合并。 未关闭Component: R
难度 2/5 1-3 小时 新手友好度 78/100
维护者通常 1 天内回复
-
[C++][Python][Docs] Some compute functions are missing from the docs可能已有人在做 @rajbhatt2406-collab 于 1 天前认领。 未关闭Component: C++ Component: Documentation Component: Python
难度 2/5 1-3 小时 新手友好度 78/100
维护者通常 1 天内回复
-
Component: C++ Type: enhancement
难度 2/5 1-3 小时 新手友好度 72/100
维护者通常 1 天内回复
-
Component: Continuous Integration Component: Developer Tools Type: enhancement
难度 2/5 1-3 小时 新手友好度 74/100
维护者通常 1 天内回复
-
Component: Python Type: enhancement
难度 2/5 1-3 小时 新手友好度 67/100
apache/arrow#55659 · 1 条评论 · 1 个 reaction ·
维护者通常 1 天内回复
相似的 Issue
-
难度 2/5 1-3 小时 新手友好度 69/100
维护者通常 2 天内回复
-
Missing repro Platform: Android
难度 2/5 1-3 小时 新手友好度 76/100
software-mansion/react-native-reanimated#11554 · 1 条评论 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 85/100
Niko1221/Strata#7211 · 3 条评论 ·
维护者通常 5 天内回复
-
难度 2/5 1-3 小时 新手友好度 64/100
-
bug
难度 2/5 1-3 小时 新手友好度 82/100
sleepyviper1213/redis#52 ·