AngusJohnson / Clipper2

Polygon Clipping and Offsetting - C++, C# and Delphi
Boost Software License 1.0
1.52k stars 273 forks source link

Range error in TClipperOffset.BuildNormals (Delphi) #867

Closed rinntech-ak closed 4 months ago

rinntech-ak commented 4 months ago

During using shrinking of paths I got range error in procedure BuildNormals. If pathlength is zero (len=0) fNorms[len-1] accesses element -1

procedure TClipperOffset.BuildNormals; var i, len: integer; begin len := Length(fInPath); SetLength(fNorms, len); for i := 0 to len-2 do fNorms[i] := GetUnitNormal(fInPath[i], fInPath[i+1]); fNorms[len -1] := GetUnitNormal(fInPath[len -1], fInPath[0]); end;

My suggest for fix (works fine in my project): if len>0 then fNorms[len -1] := GetUnitNormal(fInPath[len -1], fInPath[0]);

Best regards Andreas

rakkie commented 4 months ago

I am seeing the same exception when offsetting in C#.

Here is some minimal code that reproduces the issue.

using Clipper2Lib;

var path = new Path64 { new Point64(100,  100) };
var paths = new Paths64() { path };

var clipperOffset = new ClipperOffset();
clipperOffset.AddPaths(paths, JoinType.Miter, EndType.Polygon);

var solution = new Paths64();
clipperOffset.Execute(-10, solution);

Expected Result: Empty solution (as in previous Clipper versions) Actual Result: Out of range exception.

regards, Richard