[Mono-dev] FileStream write bug

Andrii Nakryiko andrii.nakryiko at gmail.com
Wed Apr 10 09:50:28 UTC 2013


We've had a problems with FileStream under Mono (different versions, both
2.10.x and 3.0.x). It seems like some internal positioning problem, because
instead of overwriting some parts of file, it just appends written data. It
is very critical that the amount of bytes written is larger than internal
buffer size. I've managed to create small isolated test case that shows
both what is necessary for bug to occur and easy workaround for that. But
hopefully this will be fixed as this is core functionality.

I've filed a bug report here:

    using System;
    using System.IO;
    using NUnit.Framework;

    public class mono_filestream_bug
        public void show_time()
            const int pos = 1;
            const int bufferSize = 128;

            var filename = Path.GetTempFileName();
            File.WriteAllBytes(filename, new byte[pos + 1]); // init file
with zeros

            var bytes = new byte[bufferSize + 1 /* THIS IS WHAT MAKES A BIG
            new Random().NextBytes(bytes);

            using (var file = new FileStream(filename, FileMode.Open,
FileAccess.ReadWrite, FileShare.Read,
                file.Read(new byte[pos], 0, pos); // THIS READ IS CRITICAL,
                Assert.AreEqual(pos, file.Position); // !!! here it says
position is correct, but writes at different position !!!
                // file.Position = pos; // !!! this fixes test !!!
                file.Write(bytes, 0, bytes.Length);

                //Assert.AreEqual(pos + bytes.Length, file.Length); -- fails

            using (var filestream = File.Open(filename, FileMode.Open,
                var bb = new byte[bytes.Length];
                filestream.Position = pos;
                filestream.Read(bb, 0, bb.Length);
                Assert.AreEqual(bytes, bb);

Best regards,
Andrii Nakryiko
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.ximian.com/pipermail/mono-devel-list/attachments/20130410/5874585b/attachment-0001.html>

More information about the Mono-devel-list mailing list