Skip to content

Commit 8d4fbb6

Browse files
committed
SVG: fix NPE in TransforUtil in case of invalid rotate/skewX/skewY
DEVSIX-2888
1 parent 13be970 commit 8d4fbb6

File tree

3 files changed

+41
-5
lines changed

3 files changed

+41
-5
lines changed

svg/src/main/java/com/itextpdf/svg/exceptions/SvgExceptionMessageConstant.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ public final class SvgExceptionMessageConstant {
5353
public static final String INVALID_SMOOTH_CURVE_USE =
5454
"The smooth curve operations (S, s, T, t) may not be used as a first operator in path.";
5555
public static final String INVALID_TRANSFORM_DECLARATION = "Transformation declaration is not formed correctly.";
56+
public static final String INVALID_TRANSFORM_VALUE = "Invalid transformation value: {0}";
5657
public static final String LINE_TO_EXPECTS_FOLLOWING_PARAMETERS_GOT_0 =
5758
"(x y)+ parameters are expected for lineTo operator. Got: {0}";
5859
public static final String MOVE_TO_EXPECTS_FOLLOWING_PARAMETERS_GOT_0 =

svg/src/main/java/com/itextpdf/svg/utils/TransformUtils.java

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ This file is part of the iText (R) project.
2222
*/
2323
package com.itextpdf.svg.utils;
2424

25+
import com.itextpdf.commons.utils.MessageFormatUtil;
2526
import com.itextpdf.kernel.geom.AffineTransform;
2627
import com.itextpdf.styledxmlparser.css.util.CssDimensionParsingUtils;
27-
import com.itextpdf.styledxmlparser.css.util.CssUtils;
2828
import com.itextpdf.svg.exceptions.SvgExceptionMessageConstant;
2929
import com.itextpdf.svg.exceptions.SvgProcessingException;
3030

@@ -200,7 +200,7 @@ private static AffineTransform createSkewYTransformation(List<String> values) {
200200
throw new SvgProcessingException(SvgExceptionMessageConstant.TRANSFORM_INCORRECT_NUMBER_OF_VALUES);
201201
}
202202

203-
double tan = Math.tan(Math.toRadians((float) CssDimensionParsingUtils.parseFloat(values.get(0))));
203+
double tan = Math.tan(Math.toRadians(parseTransformationValue(values.get(0))));
204204

205205
//Differs from the notation in the PDF-spec for skews
206206
return new AffineTransform(1, tan, 0, 1, 0, 0);
@@ -217,7 +217,7 @@ private static AffineTransform createSkewXTransformation(List<String> values) {
217217
throw new SvgProcessingException(SvgExceptionMessageConstant.TRANSFORM_INCORRECT_NUMBER_OF_VALUES);
218218
}
219219

220-
double tan = Math.tan(Math.toRadians((float) CssDimensionParsingUtils.parseFloat(values.get(0))));
220+
double tan = Math.tan(Math.toRadians(parseTransformationValue(values.get(0))));
221221

222222
//Differs from the notation in the PDF-spec for skews
223223
return new AffineTransform(1, 0, tan, 1, 0, 0);
@@ -234,7 +234,7 @@ private static AffineTransform createRotationTransformation(List<String> values)
234234
throw new SvgProcessingException(SvgExceptionMessageConstant.TRANSFORM_INCORRECT_NUMBER_OF_VALUES);
235235
}
236236

237-
double angle = Math.toRadians((float) CssDimensionParsingUtils.parseFloat(values.get(0)));
237+
double angle = Math.toRadians(parseTransformationValue(values.get(0)));
238238

239239
if (values.size() == 3) {
240240
float centerX = CssDimensionParsingUtils.parseAbsoluteLength(values.get(1));
@@ -327,4 +327,14 @@ private static List<String> getValuesFromTransformationString(String transformat
327327

328328
return SvgCssUtils.splitValueList(numbers);
329329
}
330+
331+
private static float parseTransformationValue(String valueStr) {
332+
Float valueParsed = CssDimensionParsingUtils.parseFloat(valueStr);
333+
if (valueParsed == null) {
334+
throw new SvgProcessingException(MessageFormatUtil.format(
335+
SvgExceptionMessageConstant.INVALID_TRANSFORM_VALUE, valueStr));
336+
} else {
337+
return (float) valueParsed;
338+
}
339+
}
330340
}

svg/src/test/java/com/itextpdf/svg/utils/TransformUtilsTest.java

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,15 @@ This file is part of the iText (R) project.
2222
*/
2323
package com.itextpdf.svg.utils;
2424

25+
import com.itextpdf.commons.utils.MessageFormatUtil;
2526
import com.itextpdf.kernel.geom.AffineTransform;
2627
import com.itextpdf.svg.exceptions.SvgExceptionMessageConstant;
2728
import com.itextpdf.svg.exceptions.SvgProcessingException;
2829
import com.itextpdf.test.ExtendedITextTest;
2930

3031
import org.junit.jupiter.api.Assertions;
31-
import org.junit.jupiter.api.Test;
3232
import org.junit.jupiter.api.Tag;
33+
import org.junit.jupiter.api.Test;
3334

3435
@Tag("UnitTest")
3536
public class TransformUtilsTest extends ExtendedITextTest {
@@ -198,4 +199,28 @@ public void mixedWhiteSpace() {
198199

199200
Assertions.assertEquals(expected, actual);
200201
}
202+
203+
@Test
204+
public void parseInvalidRotateTest() {
205+
Exception e = Assertions.assertThrows(SvgProcessingException.class,
206+
() -> TransformUtils.parseTransform("rotate(text)"));
207+
Assertions.assertEquals(MessageFormatUtil.format(SvgExceptionMessageConstant.INVALID_TRANSFORM_VALUE, "text"),
208+
e.getMessage());
209+
}
210+
211+
@Test
212+
public void parseInvalidSkewXTest() {
213+
Exception e = Assertions.assertThrows(SvgProcessingException.class,
214+
() -> TransformUtils.parseTransform("skewX(text)"));
215+
Assertions.assertEquals(MessageFormatUtil.format(SvgExceptionMessageConstant.INVALID_TRANSFORM_VALUE, "text"),
216+
e.getMessage());
217+
}
218+
219+
@Test
220+
public void parseInvalidSkewYTest() {
221+
Exception e = Assertions.assertThrows(SvgProcessingException.class,
222+
() -> TransformUtils.parseTransform("skewY(text)"));
223+
Assertions.assertEquals(MessageFormatUtil.format(SvgExceptionMessageConstant.INVALID_TRANSFORM_VALUE, "text"),
224+
e.getMessage());
225+
}
201226
}

0 commit comments

Comments
 (0)