Skip to content

[C#] C Data Interface import computes incorrect buffer sizes when offset is non-zero #43267

@adamreeve

Description

@adamreeve

Describe the bug, including details regarding any error messages, version, and platform.

Code to reproduce the problem as an XUnit test:

[Fact]
public unsafe void RoundTripArrayWithOffset()
{
    Int32Array array = new Int32Array.Builder()
        .AppendRange(new[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 })
        .Build();
    IArrowArray sliced = array.Slice(2, 6);
    CArrowArray* cArray = CArrowArray.Create();
    CArrowArrayExporter.ExportArray(sliced, cArray);
    using (var importedSlice = (Int32Array)CArrowArrayImporter.ImportArray(cArray, array.Data.DataType))
    {
        Assert.Equal(6, importedSlice.Length);  // OK
        Assert.Equal(2, importedSlice.Offset);  // OK
        Assert.Equal(2, importedSlice.GetValue(0));  // Throws
    }
    CArrowArray.Free(cArray);
}

This throws:

System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values.

System.ArgumentOutOfRangeException
Specified argument was out of the range of valid values.
   at Apache.Arrow.PrimitiveArray`1.get_Values() in /home/adam/dev/arrow/csharp/src/Apache.Arrow/Arrays/PrimitiveArray.cs:line 34
   at Apache.Arrow.Tests.CDataInterfaceDataTests.RoundTripArrayWithOffset() in /home/adam/dev/arrow/csharp/test/Apache.Arrow.Tests/CDataInterfaceDataTests.cs:line 83
   at System.RuntimeMethodHandle.InvokeMethod(Object target, Void** arguments, Signature sig, Boolean isConstructor)
   at System.Reflection.MethodBaseInvoker.InvokeWithNoArgs(Object obj, BindingFlags invokeAttr)

The problem is that when importing the buffers, we assume the buffer length is at least array.length * typeByteWidth (see ImportFixedWidthBuffers), but really it should be (array.length + array.offset) * typeByteWidth. This example is for a fixed width array type, but other more complex array types appear to have the same problem.

I don't have any immediate plans to fix this myself, but noticed this problem as part of looking at #43266.

Component(s)

C#

Metadata

Metadata

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions