From bd3afa0a5a5d48251ebf01d8dbfdcf0c4a55a8a4 Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Wed, 8 Jan 2025 21:59:29 +1000 Subject: [PATCH 1/2] Limit access to target dimensions --- ImageSharp.Drawing.sln | 2 +- src/ImageSharp.Drawing/Processing/RecolorBrush.cs | 12 ++++++++++-- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/ImageSharp.Drawing.sln b/ImageSharp.Drawing.sln index ffc24ef8..575d4a5e 100644 --- a/ImageSharp.Drawing.sln +++ b/ImageSharp.Drawing.sln @@ -28,7 +28,7 @@ EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "ISSUE_TEMPLATE", "ISSUE_TEMPLATE", "{FBE8C1AD-5AEC-4514-9B64-091D8E145865}" ProjectSection(SolutionItems) = preProject .github\ISSUE_TEMPLATE\config.yml = .github\ISSUE_TEMPLATE\config.yml - .github\ISSUE_TEMPLATE\oss-bug-report.md = .github\ISSUE_TEMPLATE\oss-bug-report.md + .github\ISSUE_TEMPLATE\oss-bug-report.yml = .github\ISSUE_TEMPLATE\oss-bug-report.yml EndProjectSection EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{815C0625-CD3D-440F-9F80-2D83856AB7AE}" diff --git a/src/ImageSharp.Drawing/Processing/RecolorBrush.cs b/src/ImageSharp.Drawing/Processing/RecolorBrush.cs index 797e88d6..998a0f2d 100644 --- a/src/ImageSharp.Drawing/Processing/RecolorBrush.cs +++ b/src/ImageSharp.Drawing/Processing/RecolorBrush.cs @@ -136,9 +136,17 @@ public RecolorBrushApplicator( /// public override void Apply(Span scanline, int x, int y) { - Span amounts = this.blenderBuffers.AmountSpan.Slice(0, scanline.Length); - Span overlays = this.blenderBuffers.OverlaySpan.Slice(0, scanline.Length); + if (x < 0 || y < 0 || x >= this.Target.Width || y >= this.Target.Height) + { + return; + } + + // Limit the scanline to the bounds of the image relative to x. + scanline = scanline[..Math.Min(this.Target.Width - x, scanline.Length)]; + Span amounts = this.blenderBuffers.AmountSpan[..scanline.Length]; + Span overlays = this.blenderBuffers.OverlaySpan[..scanline.Length]; + int width = this.Target.Width; for (int i = 0; i < scanline.Length; i++) { amounts[i] = scanline[i] * this.Options.BlendPercentage; From 51173a8036a2f01951ea73e938fea78d37965e01 Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Wed, 8 Jan 2025 22:04:27 +1000 Subject: [PATCH 2/2] Remove unused variable --- src/ImageSharp.Drawing/Processing/RecolorBrush.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/ImageSharp.Drawing/Processing/RecolorBrush.cs b/src/ImageSharp.Drawing/Processing/RecolorBrush.cs index 998a0f2d..95738114 100644 --- a/src/ImageSharp.Drawing/Processing/RecolorBrush.cs +++ b/src/ImageSharp.Drawing/Processing/RecolorBrush.cs @@ -146,7 +146,6 @@ public override void Apply(Span scanline, int x, int y) Span amounts = this.blenderBuffers.AmountSpan[..scanline.Length]; Span overlays = this.blenderBuffers.OverlaySpan[..scanline.Length]; - int width = this.Target.Width; for (int i = 0; i < scanline.Length; i++) { amounts[i] = scanline[i] * this.Options.BlendPercentage;