Skip to content

Commit

Permalink
Merge pull request #660 from Xian55/fix/609
Browse files Browse the repository at this point in the history
Fix: SharedLib: LineSegmentOperation: bounds check before copying into span
  • Loading branch information
Xian55 authored Jan 11, 2025
2 parents 11d6efc + fc0ac6e commit 5a228de
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 8 deletions.
2 changes: 0 additions & 2 deletions CoreTests/NpcNameFinder/Test_NpcNameFinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ internal sealed class Test_NpcNameFinder : IDisposable

private readonly IWowScreen screen;

private readonly Stopwatch stopwatch;
private readonly StringBuilder stringBuilder;

private readonly NpcNameOverlay? npcNameOverlay;
Expand All @@ -49,7 +48,6 @@ public Test_NpcNameFinder(ILogger logger, WowProcess process,
this.logger = logger;
this.screen = screen;

stopwatch = new();
stringBuilder = new();

INpcResetEvent npcResetEvent = new NpcResetEvent();
Expand Down
6 changes: 4 additions & 2 deletions SharedLib/NpcFinder/LineSegmentOperation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,10 @@ public readonly void Invoke(int y, Span<LineSegment> span)
if (i == 0)
return;

Interlocked.Add(ref counter.count, i);
int newCount = Interlocked.Add(ref counter.count, i);
if (counter.count + newCount > segments.Length)
return;

span[..i].CopyTo(segments.AsSpan(counter.count, i));
span[..i].CopyTo(segments.AsSpan(counter.count, newCount));
}
}
7 changes: 3 additions & 4 deletions SharedLib/NpcFinder/NpcNameFinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ public void Update()
resetEvent.Reset();

ReadOnlySpan<LineSegment> lineSegments =
PopulateLines(bitmapProvider, Area, colorMatcher, Area,
PopulateLines(colorMatcher, Area,
ScaleWidth(MinHeight), ScaleWidth(WidthDiff));

Npcs = DetermineNpcs(lineSegments);
Expand Down Expand Up @@ -520,11 +520,10 @@ private int YOffset(Rectangle area, Rectangle npc)

[SkipLocalsInit]
private ReadOnlySpan<LineSegment> PopulateLines(
IScreenImageProvider provider, Rectangle rect,
Func<byte, byte, byte, bool> colorMatcher,
Rectangle area, float minLength, float lengthDiff)
{
const int RESOLUTION = 32;
const int RESOLUTION = 16;
int rowSize = (area.Right - area.Left) / RESOLUTION;
int height = (area.Bottom - area.Top) / RESOLUTION;
int totalSize = rowSize * height;
Expand Down Expand Up @@ -553,7 +552,7 @@ private ReadOnlySpan<LineSegment> PopulateLines(
in operation);

pooler.Return(segments);
return new(segments, 0, counter.count);
return new(segments, 0, Math.Min(segments.Length, counter.count));
}

public Point ToScreenCoordinates()
Expand Down

0 comments on commit 5a228de

Please sign in to comment.