Skip to content

Commit

Permalink
[IO] fixed occasional exception in ReadArray observed on net 8.0 runtime
Browse files Browse the repository at this point in the history
  • Loading branch information
luithefirst committed May 8, 2024
1 parent 1ee5e5f commit c3e7825
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Aardvark.Base.IO/Aardvark.Base.IO.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<TargetFrameworks>net6.0;netstandard2.0</TargetFrameworks>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<NoWarn>612;1591</NoWarn>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
Expand Down
20 changes: 20 additions & 0 deletions src/Aardvark.Base.IO/StreamCodeReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,25 @@ public long ReadArray<T>(T[] array, long index, long count)
where T : struct
{
if (count < 1) return 0;

#if NET6_0_OR_GREATER
var span = MemoryMarshal.AsBytes(array.AsSpan((int)index, (int)count));

var sizeOfT = span.Length / array.Length;
var bytesToRead = span.Length;
var offset = 0;

do
{
int finished = base.Read(span);
if (finished == 0) break;
offset += finished; bytesToRead -= finished;
span = span.Slice(offset, bytesToRead);
}
while (bytesToRead > 0);

return offset / sizeOfT;
#else
unsafe
{
var sizeOfT = Marshal.SizeOf(typeof(T));
Expand Down Expand Up @@ -152,6 +171,7 @@ public long ReadArray<T>(T[] array, long index, long count)
}
return ((long)(offset / sizeOfT) - index);
}
#endif
}

public long ReadArray<T>(T[,] array, long count)
Expand Down

0 comments on commit c3e7825

Please sign in to comment.