Skip to content

Commit

Permalink
Fix StreamView.Read overflow
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremy-visionaid committed Oct 8, 2024
1 parent 560ca59 commit b7cf828
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
12 changes: 12 additions & 0 deletions sources/OpenMcdf/MathEx.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;

namespace OpenMcdf
{
internal static class MathEx
{
public static int Clamp(int value, int min, int max)
{
return Math.Max(min, Math.Min(max, value));
}
}
}
7 changes: 5 additions & 2 deletions sources/OpenMcdf/StreamView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,11 @@ public override int Read(byte[] buffer, int offset, int count)
int nRead = 0;

// Don't try to read more bytes than this stream contains.
long intMax = Math.Min(int.MaxValue, length);
count = Math.Min((int)intMax, count);
long maxReadable = Math.Min(int.MaxValue, length - position);
count = MathEx.Clamp(count, 0, (int)maxReadable);

if (count == 0)
return 0;

if (BaseSectorChain != null && BaseSectorChain.Count > 0)
{
Expand Down

0 comments on commit b7cf828

Please sign in to comment.