]> source.dussan.org Git - poi.git/commitdiff
Fix ArrayIndexOutOfBoundsException if XWPFRun does not set style
authorSayi <sayi@apache.org>
Wed, 28 Apr 2021 09:26:16 +0000 (09:26 +0000)
committerSayi <sayi@apache.org>
Wed, 28 Apr 2021 09:26:16 +0000 (09:26 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1889259 13f79535-47bb-0310-9956-ffa450edef68

poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFRun.java
poi-ooxml/src/test/java/org/apache/poi/xwpf/usermodel/TestXWPFRun.java

index c9176437a77f53cbdeb82f522e6dd7e17ebc53ba..0a77b32d0fd3b83a20928e143f45ffebc2e91288 100644 (file)
@@ -1248,16 +1248,11 @@ public class XWPFRun implements ISDTContents, IRunElement, CharacterRun {
      */
     public String getStyle() {
         CTRPr pr = getCTR().getRPr();
-        if (pr == null) {
+        if (pr == null || pr.sizeOfRStyleArray() <= 0) {
             return "";
         }
-
         CTString style = pr.getRStyleArray(0);
-        if (style == null) {
-            return "";
-        }
-
-        return style.getVal();
+        return null == style ? "" : style.getVal();
     }
 
 
index 1de994a804926e96cf22750cb19b69b4042e742d..856fcd9be3ae70a94b89f4e037a40c1058d2eda6 100644 (file)
@@ -788,6 +788,17 @@ class TestXWPFRun {
         document.close();
     }
 
+    @Test
+    void testGetEmptyStyle() throws IOException {
+        XWPFDocument document = new XWPFDocument();
+        final XWPFRun run = document.createParagraph().createRun();
+        assertEquals("", run.getStyle());
+
+        run.getCTR().addNewRPr();
+        assertEquals("", run.getStyle());
+        document.close();
+    }
+
     @Test
     void testGetDepthWidth() throws IOException, InvalidFormatException {
         try (XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("TestDocument.docx")) {