Skip to content

Commit 113059e

Browse files
authored
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
1 parent ddb4900 commit 113059e

2 files changed

Lines changed: 45 additions & 1 deletion

File tree

poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFTextParagraph.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -865,7 +865,14 @@ public ListAutoNumber getBulletAutoNumberScheme() {
865865
ParagraphPropertyFetcher<ListAutoNumber> fetcher = new ParagraphPropertyFetcher<ListAutoNumber>(getLevel()){
866866
public boolean fetch(CTTextParagraphProperties props){
867867
if(props.isSetBuAutoNum() && props.getBuAutoNum().getType() != null) {
868-
setValue(ListAutoNumber.values()[props.getBuAutoNum().getType().intValue() - 1]);
868+
int typeIdx = props.getBuAutoNum().getType().intValue() - 1;
869+
ListAutoNumber[] values = ListAutoNumber.values();
870+
if (typeIdx >= 0 && typeIdx < values.length) {
871+
setValue(values[typeIdx]);
872+
} else {
873+
// Fallback handling: use the default format when the value is out of range
874+
setValue(ListAutoNumber.ARABIC_PLAIN);
875+
}
869876
return true;
870877
}
871878
return false;

poi-ooxml/src/test/java/org/apache/poi/xssf/usermodel/TestXSSFTextParagraph.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@ Licensed to the Apache Software Foundation (ASF) under one or more
1818

1919
import static org.junit.jupiter.api.Assertions.*;
2020

21+
import org.openxmlformats.schemas.drawingml.x2006.main.STTextAutonumberScheme;
22+
import org.openxmlformats.schemas.drawingml.x2006.main.CTTextParagraphProperties;
23+
import org.openxmlformats.schemas.drawingml.x2006.main.CTTextAutonumberBullet;
24+
import org.openxmlformats.schemas.drawingml.x2006.main.STTextAutonumberScheme;
25+
import org.openxmlformats.schemas.drawingml.x2006.main.CTTextParagraph;
26+
2127
import java.awt.Color;
2228
import java.io.IOException;
2329
import java.util.List;
@@ -224,4 +230,35 @@ void testXSSFTextParagraph2() throws IOException {
224230
assertArrayEquals(run.getFontColorAsBytes(), run2.getFontColorAsBytes());
225231
}
226232
}
233+
234+
@Test
235+
public void testBulletAutoNumberSchemeOutOfBounds() throws IOException{
236+
try (XSSFWorkbook wb = new XSSFWorkbook()) {
237+
XSSFSheet sheet = wb.createSheet();
238+
XSSFDrawing drawing = sheet.createDrawingPatriarch();
239+
XSSFTextBox shape = drawing.createTextbox(new XSSFClientAnchor(0, 0, 0, 0, 0, 0, 1, 1));
240+
241+
// Ensure the paragraph is created correctly
242+
XSSFTextParagraph para = shape.addNewTextParagraph();
243+
244+
// 1. Obtain or create the underlying XML structure
245+
CTTextParagraph ctPara = para.getXmlObject();
246+
CTTextParagraphProperties pr = ctPara.isSetPPr() ? ctPara.getPPr() : ctPara.addNewPPr();
247+
248+
// 2. Force a level to avoid ParagraphPropertyFetcher errors
249+
pr.setLvl(0);
250+
251+
// 3. Set an out-of-range autonumber type (e.g. 24)
252+
CTTextAutonumberBullet bullet = pr.isSetBuAutoNum() ? pr.getBuAutoNum() : pr.addNewBuAutoNum();
253+
bullet.setType(STTextAutonumberScheme.Enum.forInt(24));
254+
255+
// 4. Execute the test
256+
// If you haven't applied the fix, this line will throw AIOOBE
257+
// If the fix is applied, this should return ARABIC_PLAIN
258+
ListAutoNumber result = para.getBulletAutoNumberScheme();
259+
260+
assertNotNull(result, "Result should not be null");
261+
assertEquals(ListAutoNumber.ARABIC_PLAIN, result, "Should fallback to ARABIC_PLAIN for unknown index");
262+
}
263+
}
227264
}

0 commit comments

Comments
 (0)