Skip to content

Commit 92098af

Browse files
authored
Merge pull request #60 from SixLabors/js/simplify-out-of-range
Check length before using index.
2 parents 1e12a4c + 908b0d2 commit 92098af

2 files changed

Lines changed: 49 additions & 2 deletions

File tree

src/ImageSharp.Drawing/Shapes/InternalPath.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -719,16 +719,18 @@ private static PointData[] Simplify(IEnumerable<PointF> vectors, bool isClosed)
719719
prev--;
720720
if (prev == 0)
721721
{
722-
// all points are common, shouldn't match anything
722+
// All points are common, shouldn't match anything
723+
int next = points.Length == 1 ? 0 : 1;
723724
results.Add(
724725
new PointData
725726
{
726727
Point = points[0],
727728
Orientation = Orientation.Colinear,
728-
Segment = new Segment(points[0], points[1]),
729+
Segment = new Segment(points[0], points[next]),
729730
Length = 0,
730731
TotalLength = 0
731732
});
733+
732734
return results.ToArray();
733735
}
734736
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Copyright (c) Six Labors.
2+
// Licensed under the Apache License, Version 2.0.
3+
4+
using SixLabors.ImageSharp.Drawing.Processing;
5+
using SixLabors.ImageSharp.PixelFormats;
6+
using SixLabors.ImageSharp.Processing;
7+
using Xunit;
8+
9+
namespace SixLabors.ImageSharp.Drawing.Tests.Issues
10+
{
11+
// https://github.com/SixLabors/ImageSharp.Drawing/issues/55
12+
// https://github.com/SixLabors/ImageSharp.Drawing/issues/59
13+
public class Issues_55_59
14+
{
15+
[Fact]
16+
public void SimplifyOutOfRangeExceptionDrawLines()
17+
{
18+
PointF[] line = new[]
19+
{
20+
new PointF(1, 48),
21+
new PointF(5, 77),
22+
new PointF(35, 0),
23+
new PointF(33, 8),
24+
new PointF(11, 23)
25+
};
26+
27+
using var image = new Image<Rgba32>(100, 100);
28+
image.Mutate(imageContext => imageContext.DrawLines(new Rgba32(255, 0, 0), 1, line));
29+
}
30+
31+
[Fact]
32+
public void SimplifyOutOfRangeExceptionDraw()
33+
{
34+
var path = new Path(
35+
new LinearLineSegment(new PointF(592.0153f, 1156.238f), new PointF(592.4992f, 1157.138f)),
36+
new LinearLineSegment(new PointF(592.4992f, 1157.138f), new PointF(593.3998f, 1156.654f)),
37+
new LinearLineSegment(new PointF(593.3998f, 1156.654f), new PointF(592.916f, 1155.754f)),
38+
new LinearLineSegment(new PointF(592.916f, 1155.754f), new PointF(592.0153f, 1156.238f))
39+
);
40+
41+
using var image = new Image<Rgba32>(2000, 2000);
42+
image.Mutate(imageContext => imageContext.Draw(new Rgba32(255, 0, 0), 1, path));
43+
}
44+
}
45+
}

0 commit comments

Comments
 (0)