Skip to content

Disk files larger than 4GB may not be read properly #60430

Closed

Description

@rougemeilland

Description

  1. Open the disk file for writing using System.IO.FileStream.
  2. Seek at a position 0x100000000 ( == 1L << 32) or more from the beginning of the file.
  3. Write the data.
  4. Close the disk file.
  5. Open the disk file for reading using System.IO.FileStream.
  6. Seek in the same position as 2.
  7. Read data of the same size as the data written in 3.
  • Expected result: The data written in 3 matches the data read in 7.
  • Actual result: The data written in 3 and the data read in 7 do not match

The sample code is attached at the end of this document.

Configuration

  • .NET version: 6.0RC2
  • OS: Windows10
  • Architecture: x64

Regression?

When I built the same sample code in .NET 5.0 and checked it, the data matched as expected.
I think it's a problem with NET 6.0 RC2.

Other information

When the file of the result of executing the above reproduction procedure was displayed with a binary editor, the writing seemed to be successful.

When reading, the value of the Position property after Seek and Read seems to be okay, but only the data that is actually being read is strange.

I'm guessing that some parts of System.IO.FileStream treat the file read position as a 32-bit integer instead of a 64-bit integer.
As a result, the upper 32 bits of the Position property or the value specified in Seek may be ignored and unexpected data may be read.

Sample Code

using System;
using System.IO;

namespace Experiment
{
    public class Program
    {
        public static void Main(string[] args)
        {
            var filePath = Path.Combine(Path.GetDirectoryName(typeof(Program).Assembly.Location) ?? ".", "test.dat");
            try
            {
                var data1 = new byte[] { 1, 2, 3, 4, 5 };
                var data2 = new byte[] { 6, 7, 8, 9, 10 };
                var positon1 = 10;
                var positon2 = (1L << 32) + 10;
                using (var stream = new FileStream(filePath, FileMode.Create, FileAccess.Write))
                {
                    stream.Seek(positon1, SeekOrigin.Begin);
                    var before1 = stream.Position;
                    stream.Write(data1, 0, data1.Length);
                    var after1 = stream.Position;
                    Console.WriteLine($"write: range={before1}...{after1 - 1}, data=[{data1[0]}, {data1[1]}, {data1[2]}, {data1[3]}, {data1[4]}]");

                    stream.Seek(positon2, SeekOrigin.Begin);
                    var before2 = stream.Position;
                    stream.Write(data2, 0, data2.Length);
                    var after2 = stream.Position;
                    Console.WriteLine($"write: range={before2}...{after2 - 1}, data=[{data2[0]}, {data2[1]}, {data2[2]}, {data2[3]}, {data2[4]}]");
                }
                using (var stream = new FileStream(filePath, FileMode.Open, FileAccess.Read))
                {
                    var buffer = new byte[5];

                    stream.Seek(positon1, SeekOrigin.Begin);
                    var before1 = stream.Position;
                    ReadBytes(stream, buffer, 0, buffer.Length);
                    var after1 = stream.Position;
                    Console.WriteLine($"read: range={before1}...{after1 - 1}, data=[{buffer[0]}, {buffer[1]}, {buffer[2]}, {buffer[3]}, {buffer[4]}]");

                    stream.Seek(positon2, SeekOrigin.Begin);
                    var before2 = stream.Position;
                    ReadBytes(stream, buffer, 0, buffer.Length);
                    var after2 = stream.Position;
                    Console.WriteLine($"read: range={before2}...{after2 - 1}, data=[{buffer[0]}, {buffer[1]}, {buffer[2]}, {buffer[3]}, {buffer[4]}]");
                }

                //
                // Execution result for .NET 5.0:
                // 
                // write: range=10...14, data=[1, 2, 3, 4, 5]
                // write: range=4294967306...4294967310, data=[6, 7, 8, 9, 10]
                // read: range=10...14, data=[1, 2, 3, 4, 5]
                // read: range=4294967306...4294967310, data=[6, 7, 8, 9, 10]
                //

                //
                // Execution result for .NET 6.0 preview:
                // 
                // write: range=10...14, data=[1, 2, 3, 4, 5]
                // write: range=4294967306...4294967310, data=[6, 7, 8, 9, 10]
                // read: range=10...14, data=[1, 2, 3, 4, 5]
                // read: range=4294967306...4294967310, data=[1, 2, 3, 4, 5]
                //
            }
            finally
            {
                //File.Delete(filePath);
            }

            Console.WriteLine("Complete");
            Console.Beep();
            Console.ReadLine();
        }

        private static void ReadBytes(Stream stream, byte[] buffer, int offset, int count)
        {
            while (count > 0)
            {
                var length = stream.Read(buffer, offset, count);
                if (length <= 0)
                    throw new Exception("Unexpected end of file");
                offset += length;
                count -= length;
            }
        }
    }
}

Activity

  1. transferred this issue fromdotnet/coreon Oct 14, 2021
  2. ghost added
    untriaged
    on Oct 14, 2021
  3. filipnavara commented on Oct 14, 2021

    @filipnavara
    Member
  4. self-assigned this
    on Oct 15, 2021
  5. removed
    untriaged
    on Oct 15, 2021
  6. added this to the 6.0.0 milestone on Oct 15, 2021
  7. adamsitnik commented on Oct 15, 2021

    @adamsitnik
    Member

    @rougemeilland big thanks for an excellent bug report! I was able to repro it. It's a bug in the buffering logic.

  8. ghost added
    in-pr
    on Oct 15, 2021
  9. filipnavara commented on Oct 15, 2021

    @filipnavara
    Member

    Thanks for fixing it this quickly!

  10. ghost removed
    in-pr
    on Oct 15, 2021
  11. ghost locked as resolved and limited conversation to collaborators on Nov 15, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions