Skip to content

RandomAccess.Write throws System.IO.IOException: The parameter is incorrect. #108322

@miroslavp

Description

@miroslavp

Description

RandomAccess.Write(SafeFileHandle handle, IReadOnlyList<ReadOnlyMemory<byte>> buffers, long fileOffset) throws IOException on Windows platform when the bytes in the buffers list exceed Int32.MaxValue

Here's the relevant stack trace

System.IO.IOException: The parameter is incorrect.
 at System.IO.RandomAccess.WriteAtOffset(SafeFileHandle handle, ReadOnlySpan`1 buffer, Int64 fileOffset)
 at System.IO.RandomAccess.WriteGatherAtOffset(SafeFileHandle handle, IReadOnlyList`1 buffers, Int64 fileOffset)

I suspect the problem is in the bytesWritten variable, because it is int and it overflows

internal static void WriteGatherAtOffset(SafeFileHandle handle, IReadOnlyList<ReadOnlyMemory<byte>> buffers, long fileOffset)
{
// WriteFileGather does not support sync handles, so we just call WriteFile in a loop
int bytesWritten = 0;
int buffersCount = buffers.Count;
for (int i = 0; i < buffersCount; i++)
{
ReadOnlySpan<byte> span = buffers[i].Span;
WriteAtOffset(handle, span, fileOffset + bytesWritten);
bytesWritten += span.Length;
}
}

Reproduction Steps

Just call RandomAccess.Write(handle, buffers, 0) on Windows machine where buffers parameter contains multiple ReadOnlyMemory<byte> buffers that collectively are larger than 2GB

Expected behavior

It should save the buffers to the file and not throw IOException

Actual behavior

throws System.IO.IOException: The parameter is incorrect.

Regression?

Haven't tried it on .NET 6

Known Workarounds

I guess you can split the buffers in multiple smaller lists and call the method multiple times

Configuration

Which version of .NET is the code running on?
.NET 7

What OS and version, and what distro if applicable?
Windows 10

What is the architecture (x64, x86, ARM, ARM64)?
x64

Do you know whether it is specific to that configuration?
I believe the problem is in the Windows implementation only

Other information

No response

Metadata

Metadata

Assignees

Labels

area-System.IObugin-prThere is an active PR which will close this issue when it is merged

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