From 99e0bf99d46e08293de4e5e178d3e390ef6f98d7 Mon Sep 17 00:00:00 2001 From: Trivalik_cp <42497cfff885d3ca0e6fda54fb6262dd42101bd5sx56jUzf> Date: Sun, 12 Jun 2011 13:47:25 +0000 Subject: [PATCH] count up position and add aCount correction if to great on Read() in FatStream --- .../Cosmos.System/Filesystem/FAT/FatStream.cs | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/source2/Kernel/System/Cosmos.System/Filesystem/FAT/FatStream.cs b/source2/Kernel/System/Cosmos.System/Filesystem/FAT/FatStream.cs index 7ffa1d12a..eee29d4f3 100644 --- a/source2/Kernel/System/Cosmos.System/Filesystem/FAT/FatStream.cs +++ b/source2/Kernel/System/Cosmos.System/Filesystem/FAT/FatStream.cs @@ -58,16 +58,23 @@ namespace Cosmos.System.Filesystem.FAT { public override int Read(byte[] aBuffer, int aOffset, int aCount) { if (aOffset < 0 || aCount < 0) { throw new ArgumentOutOfRangeException(); - } else if (aOffset >= mFile.Size) { - throw new ArgumentException(); + } + else if(aBuffer == null || aBuffer.Length - aOffset < aCount) { + throw new ArgumentException("Invalid offset length!"); } else if (mFile.FirstClusterNum == 0) { // FirstSector can be 0 for 0 length files return 0; - } else if (aOffset == mFile.Size) { + } else if (mPosition == mFile.Size) { // EOF return 0; } + // reduce count, so that no out of bound exception occurs if not existing + // entry is used in line mFS.ReadCluster(mFatTable[(int)xClusterIdx], xCluster); + uint xMaxReadableBytes = mFile.Size - mPosition; + if (aCount > xMaxReadableBytes) + aCount = (int)xMaxReadableBytes; + var xCluster = mFS.NewClusterArray(); UInt32 xClusterSize = mFS.BytesPerCluster; @@ -88,8 +95,9 @@ namespace Cosmos.System.Filesystem.FAT { aCount = aCount - xReadSize; } + mPosition += (uint)aOffset; return (int)aOffset; - } + } public override void Flush() { throw new NotImplementedException(); @@ -106,6 +114,5 @@ namespace Cosmos.System.Filesystem.FAT { public override void Write(byte[] buffer, int offset, int count) { throw new NotImplementedException(); } - } -} +} \ No newline at end of file