Skip to content

Commit

Permalink
SVG: fix NPE in TransforUtil in case of invalid rotate/skewX/skewY
Browse files Browse the repository at this point in the history
DEVSIX-2888

Autoported commit.
Original commit hash: [8d4fbb630]
  • Loading branch information
introfog authored and iText-CI committed Jan 31, 2025
1 parent bdf894e commit d01c2ec
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 4 deletions.
25 changes: 25 additions & 0 deletions itext.tests/itext.svg.tests/itext/svg/utils/TransformUtilsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
using System;
using iText.Commons.Utils;
using iText.Kernel.Geom;
using iText.Svg.Exceptions;
using iText.Test;
Expand Down Expand Up @@ -173,5 +174,29 @@ public virtual void MixedWhiteSpace() {
AffineTransform expected = AffineTransform.GetTranslateInstance(2.25, 0);
NUnit.Framework.Assert.AreEqual(expected, actual);
}

[NUnit.Framework.Test]
public virtual void ParseInvalidRotateTest() {
Exception e = NUnit.Framework.Assert.Catch(typeof(SvgProcessingException), () => TransformUtils.ParseTransform
("rotate(text)"));
NUnit.Framework.Assert.AreEqual(MessageFormatUtil.Format(SvgExceptionMessageConstant.INVALID_TRANSFORM_VALUE
, "text"), e.Message);
}

[NUnit.Framework.Test]
public virtual void ParseInvalidSkewXTest() {
Exception e = NUnit.Framework.Assert.Catch(typeof(SvgProcessingException), () => TransformUtils.ParseTransform
("skewX(text)"));
NUnit.Framework.Assert.AreEqual(MessageFormatUtil.Format(SvgExceptionMessageConstant.INVALID_TRANSFORM_VALUE
, "text"), e.Message);
}

[NUnit.Framework.Test]
public virtual void ParseInvalidSkewYTest() {
Exception e = NUnit.Framework.Assert.Catch(typeof(SvgProcessingException), () => TransformUtils.ParseTransform
("skewY(text)"));
NUnit.Framework.Assert.AreEqual(MessageFormatUtil.Format(SvgExceptionMessageConstant.INVALID_TRANSFORM_VALUE
, "text"), e.Message);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ public sealed class SvgExceptionMessageConstant {

public const String INVALID_TRANSFORM_DECLARATION = "Transformation declaration is not formed correctly.";

public const String INVALID_TRANSFORM_VALUE = "Invalid transformation value: {0}";

public const String LINE_TO_EXPECTS_FOLLOWING_PARAMETERS_GOT_0 = "(x y)+ parameters are expected for lineTo operator. Got: {0}";

public const String MOVE_TO_EXPECTS_FOLLOWING_PARAMETERS_GOT_0 = "(x y)+ parameters are expected for moveTo operator. Got: {0}";
Expand Down
17 changes: 14 additions & 3 deletions itext/itext.svg/itext/svg/utils/TransformUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ private static AffineTransform CreateSkewYTransformation(IList<String> values) {
if (values.Count != 1) {
throw new SvgProcessingException(SvgExceptionMessageConstant.TRANSFORM_INCORRECT_NUMBER_OF_VALUES);
}
double tan = Math.Tan(MathUtil.ToRadians((float)CssDimensionParsingUtils.ParseFloat(values[0])));
double tan = Math.Tan(MathUtil.ToRadians(ParseTransformationValue(values[0])));
//Differs from the notation in the PDF-spec for skews
return new AffineTransform(1, tan, 0, 1, 0, 0);
}
Expand All @@ -215,7 +215,7 @@ private static AffineTransform CreateSkewXTransformation(IList<String> values) {
if (values.Count != 1) {
throw new SvgProcessingException(SvgExceptionMessageConstant.TRANSFORM_INCORRECT_NUMBER_OF_VALUES);
}
double tan = Math.Tan(MathUtil.ToRadians((float)CssDimensionParsingUtils.ParseFloat(values[0])));
double tan = Math.Tan(MathUtil.ToRadians(ParseTransformationValue(values[0])));
//Differs from the notation in the PDF-spec for skews
return new AffineTransform(1, 0, tan, 1, 0, 0);
}
Expand All @@ -227,7 +227,7 @@ private static AffineTransform CreateRotationTransformation(IList<String> values
if (values.Count != 1 && values.Count != 3) {
throw new SvgProcessingException(SvgExceptionMessageConstant.TRANSFORM_INCORRECT_NUMBER_OF_VALUES);
}
double angle = MathUtil.ToRadians((float)CssDimensionParsingUtils.ParseFloat(values[0]));
double angle = MathUtil.ToRadians(ParseTransformationValue(values[0]));
if (values.Count == 3) {
float centerX = CssDimensionParsingUtils.ParseAbsoluteLength(values[1]);
float centerY = CssDimensionParsingUtils.ParseAbsoluteLength(values[2]);
Expand Down Expand Up @@ -294,5 +294,16 @@ private static IList<String> GetValuesFromTransformationString(String transforma
String numbers = transformation.JSubstring(transformation.IndexOf('(') + 1, transformation.IndexOf(')'));
return SvgCssUtils.SplitValueList(numbers);
}

private static float ParseTransformationValue(String valueStr) {
float? valueParsed = CssDimensionParsingUtils.ParseFloat(valueStr);
if (valueParsed == null) {
throw new SvgProcessingException(MessageFormatUtil.Format(SvgExceptionMessageConstant.INVALID_TRANSFORM_VALUE
, valueStr));
}
else {
return (float)valueParsed;
}
}
}
}
2 changes: 1 addition & 1 deletion port-hash
Original file line number Diff line number Diff line change
@@ -1 +1 @@
13be9701c7d28b13ec07d9b6244825b95f6e62a3
8d4fbb6308e2126af33891bf5ad981ec9f2fd81b

0 comments on commit d01c2ec

Please sign in to comment.