mchoe / SwiftSVG

A simple, performant, and lightweight SVG parser
Other
1.92k stars 229 forks source link

Library should throw error when path contains insufficient coordinates #119

Open marcprux opened 5 years ago

marcprux commented 5 years ago

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 commented 5 years ago

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.