Harden SVG path parser and add tests for robustness

Merged
JI
6mo ago
+191-80

Prerequisites

  • I have written a descriptive pull-request title
  • I have verified that there are no overlapping pull-requests open
  • I have verified that I am following matches the existing coding patterns and practice as demonstrated in the repository. These follow strict Stylecop rules :cop:.
  • I have provided test coverage for my change (where applicable)

Description

Fixes #385

This pull request significantly refactors and improves the robustness of the SVG path parsing logic in Path.cs, addressing edge cases and malformed input handling. The changes introduce stricter validation for SVG arc flags, ensure full operand groups are parsed before appending geometry, and replace several parsing helpers with safer, more explicit methods. Additionally, new unit tests are added to verify correct handling of malformed arc data.

SVG Path Parsing Improvements:

  • Added strict parsing for SVG arc flags by introducing TryFindFlag, ensuring only valid "0" or "1" values are accepted and preventing malformed arc flag/end-point boundaries from being parsed incorrectly. [1] [2]
  • Refactored operand parsing to use TryFindPoint, TryFindScaler, and TryReadScalar, providing better error checking and preventing partially parsed segments from being added to the path. This also ensures that only complete operand groups are processed for each SVG command. [1] [2] [3] [4]
  • Improved handling of relative coordinates and validation of finite values for points and scalars, preventing overflows and invalid geometry from being accepted.

Code Quality and Safety Enhancements:

  • Replaced direct parsing (ParseFloat) with a safer TryParseFloat method that validates numeric input and ensures only finite floats are accepted.
  • Updated command handling logic to ensure that malformed or truncated SVG path data does not result in partial or incorrect geometry being added. [1] [2]

Testing:

  • Added new unit tests in Issue_385.cs to verify that malformed arc data is correctly rejected by TryParseSvgPath, increasing reliability for edge cases.