Skip to content

[C#] Slicing a previously sliced array computes an incorrect offset #40792

@adamreeve

Description

@adamreeve

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

Test to reproduce:

[Fact]
public void RecursiveArraySlice()
{
    var initialValues = Enumerable.Range(0, 100).ToArray();
    var array = new Int32Array.Builder().AppendRange(initialValues).Build();

    var sliced = (Int32Array) array.Slice(20, 30);
    var slicedAgain = (Int32Array) sliced.Slice(5, 10);

    Assert.Equal(25, slicedAgain.Offset);  // Fails with "Actual:   45"
    Assert.Equal(10, slicedAgain.Length);
    Assert.Equal(
        initialValues.Skip(25).Take(10).Select(val => (int?) val).ToArray(),
        (IReadOnlyList<int?>) slicedAgain);
}

This is because the current offset is accounted for twice, first at

offset += array.Data.Offset;

Then again at

This only works if the offset is zero.

Component(s)

C#

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions