Bug 4118 - MemoryMappedFiles viewaccesor ReadArray reads more than requested
Summary: MemoryMappedFiles viewaccesor ReadArray reads more than requested
Status: RESOLVED FIXED
Alias: None
Product: Class Libraries
Classification: Mono
Component: System.Core ()
Version: unspecified
Hardware: PC Linux
: --- normal
Target Milestone: Untriaged
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2012-03-27 23:57 UTC by Darren
Modified: 2012-06-19 15:43 UTC (History)
4 users (show)

Tags:
Is this bug a regression?: ---
Last known good build:


Attachments
F# source code to reproduce the problem (449 bytes, application/octet-stream)
2012-03-27 23:57 UTC, Darren
Details


Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on GitHub or Developer Community with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED FIXED

Description Darren 2012-03-27 23:57:17 UTC
Created attachment 1585 [details]
F# source code to reproduce the problem

Description of Problem:

The MMF, ViewAccessor class ReadArray method, when asked to read 2 uint2 reads 15429 ints.

Steps to reproduce the problem:
1. Create a 131072 byte file called parts.pash
2. Compile and run the FSharp code.


Actual Results:

Reads 15429 ints into the array, when attempting to read 2 ints at position 69356.  If the receiving array is only 2 ints long, it complains of an array out of bounds error (offset + length > array.length).

Expected Results:

Expected just 2 ints to be read.


How often does this happen? 

Every time under mono.  Works correctly under windows.

Additional Information:

I haven't tried different offsets but it looks like it's trying to read a whole page into the array rather than the requested length,
Comment 1 Darren 2012-03-28 01:45:31 UTC
I think this implementation is wrong.  It is reading the number of remaining slots rather than the number of requested slots.  Should be perhaps min(slots,count) or just count.


public int ReadArray<T> (long position, T [] array, int offset, int count) where T : struct
                {
                        if (position < 0)
                                throw new ArgumentOutOfRangeException ();

                        long left = capacity - position;
                        var slots = (int)(left / Marshal.SizeOf (typeof (T)));

                        buffer.ReadArray ((ulong) position, array, offset, slots);
                        return slots;
                }
Comment 2 Darren 2012-03-28 14:44:29 UTC
Patch that fixes bug.

--- mcs/class/corlib/System.IO/UnmanagedMemoryAccessor.cs       2012-03-28 11:30:30.384600495 -0700
+++ mcs/class/corlib/System.IO/UnmanagedMemoryAccessor.cs.patched       2012-03-28 11:31:05.889485384 -0700
@@ -275,8 +275,11 @@
                        long left = capacity - position;
                        var slots = (int)(left / Marshal.SizeOf (typeof (T)));

-                       buffer.ReadArray ((ulong) position, array, offset,slots);
-                       return slots;
+                       if (count>slots)
+                               throw new  ArgumentOutOfRangeException ();
+
+                       buffer.ReadArray ((ulong) position, array, offset,count);
+                       return count;
                }



======================================================
Corrected function:

public int ReadArray<T> (long position, T [] array, int offset, int count) where T : struct
                {
                        if (position < 0)
                                throw new ArgumentOutOfRangeException ();

                        long left = capacity - position;
                        var slots = (int)(left / Marshal.SizeOf (typeof (T)));

                        if (count>slots)
                                throw new  ArgumentOutOfRangeException ();

                        buffer.ReadArray ((ulong) position, array, offset,count);
                        return count;
                }
Comment 3 Darren 2012-04-02 09:53:29 UTC
It appears that the 2.11 code fixes the reported bug, so I think you can close this,
Comment 4 Miguel de Icaza [MSFT] 2012-06-19 15:43:25 UTC
Closing per the request.