SwiftSVG icon indicating copy to clipboard operation
SwiftSVG copied to clipboard

Library should throw error when path contains insufficient coordinates

Open marcprux opened this issue 5 years ago • 1 comments

Currently when a path component doesn't contain a sufficient number of coordinates, the library will crash when the array bounds are exceeded. E.g., LineTo currently just does:

internal func execute(on path: UIBezierPath, previousCommand: PreviousCommand? = nil) {
    let point = self.pointForPathType(CGPoint(x: self.coordinateBuffer[0], y: self.coordinateBuffer[1]), relativeTo: path.currentPoint)
    path.addLine(to: point)
}

Since SwiftSVG may sometimes accept user input for the SVG path, the library should instead check for the sufficient number of coordinates and throw an error when there are not enough. E.g.:

internal func execute(on path: UIBezierPath, previousCommand: PreviousCommand? = nil) throws {
    if self.coordinateBuffer.count < 2 { 
        throw SVGError.insufficientNumberOfCorrdinates(expected: 2, found: self.coordinateBuffer.count)
    }

    let point = self.pointForPathType(CGPoint(x: self.coordinateBuffer[0], y: self.coordinateBuffer[1]), relativeTo: path.currentPoint)
    path.addLine(to: point)
}

marcprux avatar Mar 19 '19 20:03 marcprux

Actually the spec at https://www.w3.org/TR/SVG/paths.html#PathDataErrorHandling says that "The general rule for error handling in path data is that the SVG user agent shall render a ‘path’ element up to (but not including) the path command containing the first error in the path data specification".

So perhaps it doesn't need to throw an error, but instead just stop the parsing.

marcprux avatar Mar 20 '19 02:03 marcprux