Skip to content

fix: Read unaligned buffers safely in ArrowArrayView getters - #1011

Open
JonSnow1807 wants to merge 1 commit into
apache:mainfrom
JonSnow1807:fix-unaligned-getters-950
Open

JonSnow1807 wants to merge 1 commit into
apache:mainfrom
JonSnow1807:fix-unaligned-getters-950

Conversation

@JonSnow1807

@JonSnow1807 JonSnow1807 commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

Part of #950. The ArrowArrayView getters in inline_array.h read values, offsets and binary views through typed pointers, which is a misaligned load when a buffer from the C Data interface isn’t aligned. This adds ArrowBufferViewGet*Unsafe() helpers for the remaining fixed-width types next to the two from #946, uses them in the integer, double, string, bytes, list child offset and union child offset getters, and copies binary views before reading their fields.

New tests read from buffers misaligned by 1 to 7 bytes in sliced arrays. On main UBSan reports 56 misaligned loads on 43 lines; with this change it reports none, and ctest passes under ASan and UBSan with clang and gcc.

The helpers index the buffer as an array of byte arrays because with i * sizeof(T) clang stopped inlining ArrowArrayViewGetIntUnsafe() and ArrowArrayViewGetDoubleUnsafe() in files with more than one call site. With this form clang generates the same code as on main, and the array benchmarks are unchanged. The remaining sites (array.c, the IPC decoder, the C++ helpers) can follow separately.

The ArrowArrayView getters read values, offsets and binary views through
typed pointers (e.g., data.as_int32[i]). Buffers that arrive through the
C Data interface do not have to be aligned, and for those each of these
reads is a misaligned load that UBSan reports.

This adds memcpy-based ArrowBufferViewGet*Unsafe() helpers for the
remaining fixed-width types next to the 32- and 64-bit ones from apache#946
and uses them in ArrowArrayViewGetIntUnsafe(),
ArrowArrayViewGetUIntUnsafe(), ArrowArrayViewGetDoubleUnsafe(),
ArrowArrayViewGetStringUnsafe(), ArrowArrayViewGetBytesUnsafe(),
ArrowArrayViewListChildOffset() and ArrowArrayViewUnionChildOffset().
Binary views are copied before their fields are read.

The helpers index the buffer as an array of byte arrays instead of
adding i * sizeof(T) to a byte pointer. With the multiplication, clang
stopped inlining ArrowArrayViewGetIntUnsafe() and
ArrowArrayViewGetDoubleUnsafe() in a file with more than one call site.

Part of apache#950.
@codecov-commenter

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 78.72%. Comparing base (f7eb0c1) to head (be3b42f).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1011      +/-   ##
==========================================
+ Coverage   78.48%   78.72%   +0.24%     
==========================================
  Files         106      106              
  Lines       17245    17273      +28     
  Branches     2065     2065              
==========================================
+ Hits        13535    13599      +64     
+ Misses       2446     2410      -36     
  Partials     1264     1264              

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants