Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: SharedLib: LineSegmentOperation: bounds check before copying into span #660

Merged
merged 1 commit into from
Jan 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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