This is an automated email from the ASF dual-hosted git repository.

fanningpj pushed a commit to branch trunk
in repository https://gitbox.apache.org/repos/asf/poi.git


The following commit(s) were added to refs/heads/trunk by this push:
     new 113059e48e Fix ArrayIndexOutOfBoundsException in XSSFTextParagraph 
when auto number scheme index is out of range (#1033)
113059e48e is described below

commit 113059e48ebbce78bd8631938ce914e695083d40
Author: amos-wnjsoft <[email protected]>
AuthorDate: Fri Mar 20 18:11:24 2026 +0800

    Fix ArrayIndexOutOfBoundsException in XSSFTextParagraph when auto number 
scheme index is out of range (#1033)
    
    * Fix ArrayIndexOutOfBoundsException in XSSFTextParagraph when auto number 
scheme index is out of range
    
    * removed the try-catch block and added throws IOException to the method 
signature
---
 .../poi/xssf/usermodel/XSSFTextParagraph.java      |  9 +++++-
 .../poi/xssf/usermodel/TestXSSFTextParagraph.java  | 37 ++++++++++++++++++++++
 2 files changed, 45 insertions(+), 1 deletion(-)

diff --git 
a/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFTextParagraph.java 
b/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFTextParagraph.java
index c994038aeb..d917f8f720 100644
--- 
a/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFTextParagraph.java
+++ 
b/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFTextParagraph.java
@@ -865,7 +865,14 @@ public class XSSFTextParagraph implements 
Iterable<XSSFTextRun>{
         ParagraphPropertyFetcher<ListAutoNumber> fetcher = new 
ParagraphPropertyFetcher<ListAutoNumber>(getLevel()){
             public boolean fetch(CTTextParagraphProperties props){
                 if(props.isSetBuAutoNum() && props.getBuAutoNum().getType() != 
null) {
-                    
setValue(ListAutoNumber.values()[props.getBuAutoNum().getType().intValue() - 
1]);
+                    int typeIdx = props.getBuAutoNum().getType().intValue() - 
1;
+                    ListAutoNumber[] values = ListAutoNumber.values();
+                    if (typeIdx >= 0 && typeIdx < values.length) {
+                        setValue(values[typeIdx]);
+                    } else {
+                        // Fallback handling: use the default format when the 
value is out of range
+                        setValue(ListAutoNumber.ARABIC_PLAIN);
+                    }
                     return true;
                 }
                 return false;
diff --git 
a/poi-ooxml/src/test/java/org/apache/poi/xssf/usermodel/TestXSSFTextParagraph.java
 
b/poi-ooxml/src/test/java/org/apache/poi/xssf/usermodel/TestXSSFTextParagraph.java
index 17daaf102c..ae8f11bf37 100644
--- 
a/poi-ooxml/src/test/java/org/apache/poi/xssf/usermodel/TestXSSFTextParagraph.java
+++ 
b/poi-ooxml/src/test/java/org/apache/poi/xssf/usermodel/TestXSSFTextParagraph.java
@@ -18,6 +18,12 @@ package org.apache.poi.xssf.usermodel;
 
 import static org.junit.jupiter.api.Assertions.*;
 
+import org.openxmlformats.schemas.drawingml.x2006.main.STTextAutonumberScheme;
+import 
org.openxmlformats.schemas.drawingml.x2006.main.CTTextParagraphProperties;
+import org.openxmlformats.schemas.drawingml.x2006.main.CTTextAutonumberBullet;
+import org.openxmlformats.schemas.drawingml.x2006.main.STTextAutonumberScheme;
+import org.openxmlformats.schemas.drawingml.x2006.main.CTTextParagraph;
+
 import java.awt.Color;
 import java.io.IOException;
 import java.util.List;
@@ -224,4 +230,35 @@ class TestXSSFTextParagraph {
             assertArrayEquals(run.getFontColorAsBytes(), 
run2.getFontColorAsBytes());
         }
     }
+
+    @Test
+    public void testBulletAutoNumberSchemeOutOfBounds() throws IOException{
+        try (XSSFWorkbook wb = new XSSFWorkbook()) {
+            XSSFSheet sheet = wb.createSheet();
+            XSSFDrawing drawing = sheet.createDrawingPatriarch();
+            XSSFTextBox shape = drawing.createTextbox(new XSSFClientAnchor(0, 
0, 0, 0, 0, 0, 1, 1));
+
+            // Ensure the paragraph is created correctly
+            XSSFTextParagraph para = shape.addNewTextParagraph();
+
+            // 1. Obtain or create the underlying XML structure
+            CTTextParagraph ctPara = para.getXmlObject();
+            CTTextParagraphProperties pr = ctPara.isSetPPr() ? ctPara.getPPr() 
: ctPara.addNewPPr();
+
+            // 2. Force a level to avoid ParagraphPropertyFetcher errors
+            pr.setLvl(0);
+
+            // 3. Set an out-of-range autonumber type (e.g. 24)
+            CTTextAutonumberBullet bullet = pr.isSetBuAutoNum() ? 
pr.getBuAutoNum() : pr.addNewBuAutoNum();
+            bullet.setType(STTextAutonumberScheme.Enum.forInt(24));
+
+            // 4. Execute the test
+            // If you haven't applied the fix, this line will throw AIOOBE
+            // If the fix is applied, this should return ARABIC_PLAIN
+            ListAutoNumber result = para.getBulletAutoNumberScheme();
+
+            assertNotNull(result, "Result should not be null");
+            assertEquals(ListAutoNumber.ARABIC_PLAIN, result, "Should fallback 
to ARABIC_PLAIN for unknown index");
+        }
+    }
 }


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to