Skip to content

Conversation

NicoAvanzDev
Copy link
Contributor

@NicoAvanzDev NicoAvanzDev commented Sep 30, 2024

Fixes #108322

Description

Fixes RandomAccess.Write that throws System.IO.IOException on windows machine where the buffer is larger than 2GB

Customer Impact

Possible file corruption when writing

Regression

#108322 (comment)

Testing

Without the fix:
image

With the fix:
image

Test method

[Fact]
void RandomAccessWrite_ShouldNot_Throw_IOException_When_Buffer_Larger_Than_MaxInt32()
{
    // Arrange
    const long gigabyte = 1L * 1024 * 1024 * 1024;

    var rolist = new List<ReadOnlyMemory<byte>>();

    for (int i = 0; i < 3; i++)
    {
        rolist.Add(new ReadOnlyMemory<byte>(new byte[gigabyte]));
    }

    // Act
    try
    {
        UnmanagedFileLoader loader = new UnmanagedFileLoader("C:\\tmp\\testfile.txt");
        RandomAccess.Write(loader.Handle, rolist, 0);
    }
    catch (IOException ex)
    {
        Assert.Fail("Unable to write file" + ex.Message);
    }            
}

The actual UnmanagedFileLoader implementation can be found here from official microsoft documentation

Risk

None

Package authoring signed off?

IMPORTANT: If this change touches code that ships in a NuGet package, please make certain that you have added any necessary package authoring and gotten it explicitly reviewed.

@ghost ghost added the area-System.IO label Sep 30, 2024
@dotnet-policy-service dotnet-policy-service bot added the community-contribution Indicates that the PR has been added by a community member label Sep 30, 2024
@NicoAvanzDev NicoAvanzDev changed the title RandomAccess.Write throws System.IO.IOException Fix RandomAccess.Write throws System.IO.IOException when buffers total size greater than 2GB Sep 30, 2024
Copy link
Member

@adamsitnik adamsitnik left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thank you for your contribution @NicoAvanzDev !

@adamsitnik
Copy link
Member

/ba-g crypto failure is unrelated

@adamsitnik adamsitnik merged commit 50e8435 into dotnet:main Oct 2, 2024
146 of 148 checks passed
@adamsitnik adamsitnik added this to the 10.0.0 milestone Oct 2, 2024
@adamsitnik
Copy link
Member

@jeffhandley @stephentoub once I get back from the vacations I am going to add test for it and backport it to supported versions

sirntar pushed a commit to sirntar/runtime that referenced this pull request Oct 3, 2024
lambdageek pushed a commit to lambdageek/runtime that referenced this pull request Oct 3, 2024
@github-actions github-actions bot locked and limited conversation to collaborators Nov 2, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-System.IO community-contribution Indicates that the PR has been added by a community member
Projects
None yet
Development

Successfully merging this pull request may close these issues.

RandomAccess.Write throws System.IO.IOException: The parameter is incorrect.
2 participants