dotnet / dotnet/machinelearning

DataFrame IndexOufRange exception on attemp to call Apply method

Open
#7,122 0 comments 0 reactions 0 assignees View on GitHub
area-DataFrame untriaged
Dominant language
C#
Stars
9.4k
Forks
2k
Avg merge
2d 20h
Merged PRs (30d)
11

Description

Test to reproduce:

```cs
public void TestApplyMethod()
{
PrimitiveDataFrameColumn column = new PrimitiveDataFrameColumn("Byte1", int.MaxValue / 2 - 1);
PrimitiveDataFrameColumn newColumn = column.Apply(x => (double?)x);
}
```
Root cause is in Apply method:

```cs
public void Apply(Func func, PrimitiveColumnContainer resultContainer)
where TResult : unmanaged
{
for (int b = 0; b < Buffers.Count; b++)
{
var sourceBuffer = Buffers[b];
var sourceNullBitMap = NullBitMapBuffers[b].ReadOnlySpan;

Span mutableResultBuffer = resultContainer.Buffers.GetOrCreateMutable(b).Span;
Span mutableResultNullBitMapBuffers = resultContainer.NullBitMapBuffers.GetOrCreateMutable(b).Span;

for (int i = 0; i < sourceBuffer.Length; i++)
{
bool isValid = BitUtility.IsValid(sourceNullBitMap, i);
TResult? value = func(isValid ? sourceBuffer[i] : null);
mutableResultBuffer[i] = value.GetValueOrDefault();
resultContainer.SetValidityBit(mutableResultNullBitMapBuffers, i, value != null);
}
}
}
```

`mutableResultBuffer` has TResult type of underlying data, so it's max length is 2Gb / sizeof(TResult)
`sourceBuffer` has T type of underlying data, so it's max length is 2 Gb/ sizeof(T)

when sizeof(TResult) > sizeof(T) and source buffer is large enough - it resulted in IndexOutOfRange exception

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.