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

quinnj pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/arrow-julia.git


The following commit(s) were added to refs/heads/main by this push:
     new 46932f1  Fix Utf8View/BinaryView variadicBufferCounts Int64 
deserialization (#615)
46932f1 is described below

commit 46932f18006e375f984f595442ef02898162db3a
Author: Brian Klahn <[email protected]>
AuthorDate: Tue Nov 10 23:22:09 2026 -0500

    Fix Utf8View/BinaryView variadicBufferCounts Int64 deserialization (#615)
    
    RecordBatch.variadicBufferCounts is defined as [long] (Int64) in the
    Apache Arrow Message.fbs spec, but was deserialized as
    FlatBuffers.Array{Int32}. When reading tables with multiple
    Utf8View/BinaryView columns (e.g. from Polars), reading 32-bit elements
    split each 64-bit buffer count into two 32-bit words, shifting buffer
    offsets for subsequent columns and causing out-of-bounds reads or
    segmentation faults in Arrow.View getindex:
    
      signal 11 (2): Segmentation fault: 11
      getindex at ./essentials.jl:975 [inlined]
      getindex at Arrow/src/arraytypes/views.jl:61 [inlined]
    
    Also handle 4-byte-aligned Int64 vectors in FlatBuffers.Array{T} when
    the vector payload following the 4-byte UOffsetT length prefix is not
    8-byte aligned.
    
    ---------
    
    Co-authored-by: Brian D. Klahn <[email protected]>
---
 src/FlatBuffers/table.jl | 13 +++++++++----
 src/metadata/Message.jl  |  2 +-
 test/runtests.jl         | 16 ++++++++++++++++
 3 files changed, 26 insertions(+), 5 deletions(-)

diff --git a/src/FlatBuffers/table.jl b/src/FlatBuffers/table.jl
index a17d1cd..a430ea5 100644
--- a/src/FlatBuffers/table.jl
+++ b/src/FlatBuffers/table.jl
@@ -100,18 +100,23 @@ function vector(t::Table, off)
     return x + sizeof(UOffsetT)
 end
 
-struct Array{T,S,TT} <: AbstractVector{T}
+struct Array{T,S,TT,D<:AbstractVector{S}} <: AbstractVector{T}
     _tab::TT
     pos::Int64
-    data::Vector{S}
+    data::D
 end
 
 function Array{T}(t::Table, off) where {T}
     a = vector(t, off)
     S = T <: Table ? UOffsetT : T <: Struct ? NTuple{structsizeof(T),UInt8} : T
     ptr = convert(Ptr{S}, pointer(bytes(t), a + 1))
-    data = unsafe_wrap(Base.Array, ptr, vectorlen(t, off))
-    return Array{T,S,typeof(t)}(t, a, data)
+    len = vectorlen(t, off)
+    data = if UInt(ptr) % Base.datatype_alignment(S) == 0
+        unsafe_wrap(Base.Array, ptr, len)
+    else
+        reinterpret(S, @view bytes(t)[(a + 1):(a + len * sizeof(S))])
+    end
+    return Array{T,S,typeof(t),typeof(data)}(t, a, data)
 end
 
 function structsizeof end
diff --git a/src/metadata/Message.jl b/src/metadata/Message.jl
index 0e49439..126297f 100644
--- a/src/metadata/Message.jl
+++ b/src/metadata/Message.jl
@@ -101,7 +101,7 @@ function Base.getproperty(x::RecordBatch, field::Symbol)
     elseif field === :variadicBufferCounts
         o = FlatBuffers.offset(x, 12)
         if o != 0
-            return FlatBuffers.Array{Int32}(x, o)
+            return FlatBuffers.Array{Int64}(x, o)
         end
     end
     return nothing
diff --git a/test/runtests.jl b/test/runtests.jl
index 4f7d28b..a19b4a3 100644
--- a/test/runtests.jl
+++ b/test/runtests.jl
@@ -1107,6 +1107,22 @@ end
                 joinpath(dirname(pathof(Arrow)), 
"../test/reject_reason_trimmed.arrow"),
             )
             @test t.reject_reason[end] == "POST_ONLY"
+
+            # RecordBatch.variadicBufferCounts uses 64-bit integers in the 
Arrow spec
+            FB = Arrow.FlatBuffers
+            b = FB.Builder(64)
+            FB.startvector!(b, sizeof(Int64), 3, sizeof(Int64))
+            for cnt in Iterators.reverse(Int64[0, 7, 0])
+                FB.prepend!(b, cnt)
+            end
+            varcounts = FB.endvector!(b, 3)
+            FB.startobject!(b, 5)
+            FB.prependoffsetslot!(b, 4, varcounts, 0) # slot 4 (offset 12) = 
variadicBufferCounts
+            FB.finish!(b, Arrow.Meta.recordBatchEnd(b))
+
+            rb = FB.getrootas(Arrow.Meta.RecordBatch, 
Vector(FB.finishedbytes(b)), 0)
+            @test eltype(rb.variadicBufferCounts) === Int64
+            @test collect(rb.variadicBufferCounts) == Int64[0, 7, 0]
         end
     end # @testset "misc"
 

Reply via email to