From 994272e359554a60adc14d00dbc4bf112a1cb2a2 Mon Sep 17 00:00:00 2001 From: PJ Fanning Date: Wed, 27 May 2026 22:02:43 +0100 Subject: [PATCH 1/3] more number conversion changes --- .../ReadOnlySharedStringsTable.java | 4 ++-- .../apache/poi/xssf/model/StylesTable.java | 2 +- .../poi/xssf/usermodel/XSSFCellStyle.java | 4 ++-- .../usermodel/XWPFDefaultParagraphStyle.java | 5 ++++- .../poi/xwpf/usermodel/XWPFParagraph.java | 20 +++++++++---------- .../apache/poi/xwpf/usermodel/XWPFRun.java | 2 +- .../apache/poi/xwpf/usermodel/XWPFTable.java | 11 +++++++--- .../poi/xwpf/usermodel/XWPFTableCell.java | 4 +++- .../poi/xwpf/usermodel/XWPFTableRow.java | 7 +++++-- .../apache/poi/hssf/record/FeatRecord.java | 4 ++-- .../org/apache/poi/sl/draw/SLGraphics.java | 7 ++++++- .../poi/ss/format/CellDateFormatter.java | 3 ++- .../apache/poi/ss/format/SimpleFraction.java | 4 ++-- .../apache/poi/ss/formula/PlainCellCache.java | 6 +++--- .../poi/ss/formula/atp/WorkdayCalculator.java | 8 +++++--- .../poi/ss/formula/atp/WorkdayFunction.java | 4 +++- .../ss/formula/atp/WorkdayIntlFunction.java | 7 +++++-- .../formula/functions/AggregateFunction.java | 3 ++- .../poi/ss/formula/functions/WeekNum.java | 3 ++- 19 files changed, 68 insertions(+), 40 deletions(-) diff --git a/poi-ooxml/src/main/java/org/apache/poi/xssf/eventusermodel/ReadOnlySharedStringsTable.java b/poi-ooxml/src/main/java/org/apache/poi/xssf/eventusermodel/ReadOnlySharedStringsTable.java index f76ba8c8b50..df69936c7c0 100644 --- a/poi-ooxml/src/main/java/org/apache/poi/xssf/eventusermodel/ReadOnlySharedStringsTable.java +++ b/poi-ooxml/src/main/java/org/apache/poi/xssf/eventusermodel/ReadOnlySharedStringsTable.java @@ -244,9 +244,9 @@ public void startElement(String uri, String localName, String name, if ("sst".equals(localName)) { String count = attributes.getValue("count"); - if(count != null) this.count = (int) Long.parseLong(count); + if(count != null) this.count = Math.toIntExact(Long.parseLong(count)); String uniqueCount = attributes.getValue("uniqueCount"); - if(uniqueCount != null) this.uniqueCount = (int) Long.parseLong(uniqueCount); + if(uniqueCount != null) this.uniqueCount = Math.toIntExact(Long.parseLong(uniqueCount)); this.strings = new ArrayList<>( // corrupted files may have a very large number here, so only use it diff --git a/poi-ooxml/src/main/java/org/apache/poi/xssf/model/StylesTable.java b/poi-ooxml/src/main/java/org/apache/poi/xssf/model/StylesTable.java index a4cd687fad3..bb731f8b4a2 100644 --- a/poi-ooxml/src/main/java/org/apache/poi/xssf/model/StylesTable.java +++ b/poi-ooxml/src/main/java/org/apache/poi/xssf/model/StylesTable.java @@ -457,7 +457,7 @@ public XSSFCellStyle getStyleAt(int idx) { } // 0 is the empty default if(xfs.get(idx).getXfId() > 0) { - styleXfId = (int) xfs.get(idx).getXfId(); + styleXfId = Math.toIntExact(xfs.get(idx).getXfId()); } return new XSSFCellStyle(idx, styleXfId, this, theme); diff --git a/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFCellStyle.java b/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFCellStyle.java index d4493192a19..92615891524 100644 --- a/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFCellStyle.java +++ b/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFCellStyle.java @@ -1295,9 +1295,9 @@ public void invalidateCachedProperties() { private int getFontId() { if (_cellXf.isSetFontId()) { - return (int) _cellXf.getFontId(); + return Math.toIntExact(_cellXf.getFontId()); } - return (int) _cellStyleXf.getFontId(); + return Math.toIntExact(_cellStyleXf.getFontId()); } /** diff --git a/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFDefaultParagraphStyle.java b/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFDefaultParagraphStyle.java index adc7150dc1f..9c46328b4f4 100644 --- a/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFDefaultParagraphStyle.java +++ b/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFDefaultParagraphStyle.java @@ -18,6 +18,7 @@ Licensed to the Apache Software Foundation (ASF) under one or more package org.apache.poi.xwpf.usermodel; import org.apache.poi.ooxml.util.POIXMLUnits; +import org.apache.poi.util.MathUtil; import org.apache.poi.util.Units; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTPPrGeneral; @@ -42,6 +43,8 @@ public CTPPrGeneral getPPr() { } public int getSpacingAfter() { - return ppr.isSetSpacing() ? (int) Units.toDXA(POIXMLUnits.parseLength(ppr.getSpacing().xgetAfter())) : -1; + return ppr.isSetSpacing() ? + MathUtil.safeDoubleToInt( + Units.toDXA(POIXMLUnits.parseLength(ppr.getSpacing().xgetAfter()))) : -1; } } diff --git a/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFParagraph.java b/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFParagraph.java index 7ba7e404bf6..680fe2433de 100644 --- a/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFParagraph.java +++ b/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFParagraph.java @@ -949,7 +949,7 @@ public int getSpacingAfter() { public void setSpacingAfter(int spaces) { CTSpacing spacing = getCTSpacing(true); if (spacing != null) { - BigInteger bi = new BigInteger(Integer.toString(spaces)); + BigInteger bi = BigInteger.valueOf(spaces); spacing.setAfter(bi); } @@ -985,7 +985,7 @@ public int getSpacingAfterLines() { */ public void setSpacingAfterLines(int spaces) { CTSpacing spacing = getCTSpacing(true); - BigInteger bi = new BigInteger(Integer.toString(spaces)); + BigInteger bi = BigInteger.valueOf(spaces); spacing.setAfterLines(bi); } @@ -1014,7 +1014,7 @@ public int getSpacingBefore() { */ public void setSpacingBefore(int spaces) { CTSpacing spacing = getCTSpacing(true); - BigInteger bi = new BigInteger(Integer.toString(spaces)); + BigInteger bi = BigInteger.valueOf(spaces); spacing.setBefore(bi); } @@ -1046,7 +1046,7 @@ public int getSpacingBeforeLines() { */ public void setSpacingBeforeLines(int spaces) { CTSpacing spacing = getCTSpacing(true); - BigInteger bi = new BigInteger(Integer.toString(spaces)); + BigInteger bi = BigInteger.valueOf(spaces); spacing.setBeforeLines(bi); } @@ -1165,7 +1165,7 @@ public int getIndentationLeft() { */ public void setIndentationLeft(int indentation) { CTInd indent = getCTInd(true); - BigInteger bi = new BigInteger(Integer.toString(indentation)); + BigInteger bi = BigInteger.valueOf(indentation); indent.setLeft(bi); } @@ -1191,7 +1191,7 @@ public int getIndentationLeftChars() { */ public void setIndentationLeftChars(int indentation) { CTInd indent = getCTInd(true); - BigInteger bi = new BigInteger(Integer.toString(indentation)); + BigInteger bi = BigInteger.valueOf(indentation); indent.setLeftChars(bi); } @@ -1231,7 +1231,7 @@ public int getIndentationRight() { */ public void setIndentationRight(int indentation) { CTInd indent = getCTInd(true); - BigInteger bi = new BigInteger(Integer.toString(indentation)); + BigInteger bi = BigInteger.valueOf(indentation); indent.setRight(bi); } @@ -1257,7 +1257,7 @@ public int getIndentationRightChars() { */ public void setIndentationRightChars(int indentation) { CTInd indent = getCTInd(true); - BigInteger bi = new BigInteger(Integer.toString(indentation)); + BigInteger bi = BigInteger.valueOf(indentation); indent.setRightChars(bi); } @@ -1296,7 +1296,7 @@ public int getIndentationHanging() { public void setIndentationHanging(int indentation) { CTInd indent = getCTInd(true); - BigInteger bi = new BigInteger(Integer.toString(indentation)); + BigInteger bi = BigInteger.valueOf(indentation); indent.setHanging(bi); } @@ -1338,7 +1338,7 @@ public int getIndentationFirstLine() { */ public void setIndentationFirstLine(int indentation) { CTInd indent = getCTInd(true); - BigInteger bi = new BigInteger(Integer.toString(indentation)); + BigInteger bi = BigInteger.valueOf(indentation); indent.setFirstLine(bi); } diff --git a/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFRun.java b/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFRun.java index d28d628eab4..687c5181626 100644 --- a/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFRun.java +++ b/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFRun.java @@ -1129,7 +1129,7 @@ public int getTextPosition() { * values will lower it. */ public void setTextPosition(int val) { - BigInteger bint = new BigInteger(Integer.toString(val)); + BigInteger bint = BigInteger.valueOf(val); CTRPr pr = getRunProperties(true); CTSignedHpsMeasure position = pr.sizeOfPositionArray() > 0 ? pr.getPositionArray(0) : pr.addNewPosition(); position.setVal(bint); diff --git a/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFTable.java b/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFTable.java index 33fc2a8d635..2fdb5e339d8 100644 --- a/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFTable.java +++ b/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFTable.java @@ -28,6 +28,7 @@ Licensed to the Apache Software Foundation (ASF) under one or more import org.apache.poi.ooxml.POIXMLDocumentPart; import org.apache.poi.ooxml.util.POIXMLUnits; import org.apache.poi.util.Internal; +import org.apache.poi.util.MathUtil; import org.apache.poi.util.Units; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTBorder; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTDecimalNumber; @@ -285,7 +286,9 @@ public XWPFTableRow getRow(int pos) { */ public int getWidth() { CTTblPr tblPr = getTblPr(false); - return tblPr != null && tblPr.isSetTblW() ? (int)Units.toDXA(POIXMLUnits.parseLength(tblPr.getTblW().xgetW())) : -1; + return tblPr != null && tblPr.isSetTblW() ? + MathUtil.safeDoubleToInt( + Units.toDXA(POIXMLUnits.parseLength(tblPr.getTblW().xgetW()))) : -1; } /** @@ -327,7 +330,8 @@ public int getIndent() { } switch (typeValue.intValue()) { case STTblWidth.INT_DXA: - return (int) Units.toDXA(POIXMLUnits.parseLength(tblPr.getTblInd().xgetW())); + return MathUtil.safeDoubleToInt( + Units.toDXA(POIXMLUnits.parseLength(tblPr.getTblInd().xgetW()))); case STTblWidth.INT_NIL: // "ยง17.18.90: [nil] Specifies that the current width is zero, regardless of // any width value specified on the parent element" @@ -1066,7 +1070,8 @@ private int getCellMargin(Function margin) { if (tcm != null) { CTTblWidth tw = margin.apply(tcm); if (tw != null) { - return (int) Units.toDXA(POIXMLUnits.parseLength(tw.xgetW())); + return MathUtil.safeDoubleToInt( + Units.toDXA(POIXMLUnits.parseLength(tw.xgetW()))); } } } diff --git a/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFTableCell.java b/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFTableCell.java index cbf6b5fe56f..cf5846a7720 100644 --- a/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFTableCell.java +++ b/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFTableCell.java @@ -25,6 +25,7 @@ Licensed to the Apache Software Foundation (ASF) under one or more import org.apache.poi.ooxml.POIXMLDocumentPart; import org.apache.poi.ooxml.util.POIXMLUnits; import org.apache.poi.util.Internal; +import org.apache.poi.util.MathUtil; import org.apache.poi.util.Units; import org.apache.xmlbeans.XmlCursor; import org.apache.xmlbeans.XmlObject; @@ -624,6 +625,7 @@ public void setWidthType(TableWidthType widthType) { } public int getWidth() { - return (int) Units.toDXA(POIXMLUnits.parseLength(getTcWidth().xgetW())); + return MathUtil.safeDoubleToInt( + Units.toDXA(POIXMLUnits.parseLength(getTcWidth().xgetW()))); } } diff --git a/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFTableRow.java b/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFTableRow.java index 4b43c0817cb..2717fe56611 100644 --- a/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFTableRow.java +++ b/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFTableRow.java @@ -23,6 +23,7 @@ Licensed to the Apache Software Foundation (ASF) under one or more import org.apache.poi.ooxml.util.POIXMLUnits; import org.apache.poi.util.Internal; +import org.apache.poi.util.MathUtil; import org.apache.poi.util.Units; import org.apache.xmlbeans.XmlCursor; import org.apache.xmlbeans.XmlObject; @@ -114,7 +115,9 @@ private void ensureBlockLevelElement(XWPFTableCell tableCell) { */ public int getHeight() { CTTrPr properties = getTrPr(); - return properties.sizeOfTrHeightArray() == 0 ? 0 : (int) Units.toDXA(POIXMLUnits.parseLength(properties.getTrHeightArray(0).xgetVal())); + return properties.sizeOfTrHeightArray() == 0 ? 0 : + MathUtil.safeDoubleToInt(Units.toDXA( + POIXMLUnits.parseLength(properties.getTrHeightArray(0).xgetVal()))); } /** @@ -128,7 +131,7 @@ public int getHeight() { public void setHeight(int height) { CTTrPr properties = getTrPr(); CTHeight h = properties.sizeOfTrHeightArray() == 0 ? properties.addNewTrHeight() : properties.getTrHeightArray(0); - h.setVal(new BigInteger(Integer.toString(height))); + h.setVal(BigInteger.valueOf(height)); } /** diff --git a/poi/src/main/java/org/apache/poi/hssf/record/FeatRecord.java b/poi/src/main/java/org/apache/poi/hssf/record/FeatRecord.java index 065bf91cb2c..d7367409f2b 100644 --- a/poi/src/main/java/org/apache/poi/hssf/record/FeatRecord.java +++ b/poi/src/main/java/org/apache/poi/hssf/record/FeatRecord.java @@ -123,9 +123,9 @@ public void serialize(LittleEndianOutput out) { out.writeShort(isf_sharedFeatureType); out.writeByte(reserved1); - out.writeInt((int)reserved2); + out.writeInt(Math.toIntExact(reserved2)); out.writeShort(cellRefs.length); - out.writeInt((int)cbFeatData); + out.writeInt(Math.toIntExact(cbFeatData)); out.writeShort(reserved3); for (CellRangeAddress cellRef : cellRefs) { diff --git a/poi/src/main/java/org/apache/poi/sl/draw/SLGraphics.java b/poi/src/main/java/org/apache/poi/sl/draw/SLGraphics.java index 47838a6ed55..2d5c2945657 100644 --- a/poi/src/main/java/org/apache/poi/sl/draw/SLGraphics.java +++ b/poi/src/main/java/org/apache/poi/sl/draw/SLGraphics.java @@ -47,6 +47,7 @@ Licensed to the Apache Software Foundation (ASF) under one or more import org.apache.poi.sl.usermodel.TextBox; import org.apache.poi.sl.usermodel.TextRun; import org.apache.poi.sl.usermodel.VerticalAlignment; +import org.apache.poi.util.MathUtil; import org.apache.poi.util.NotImplemented; import org.apache.poi.util.SuppressForbidden; @@ -307,7 +308,11 @@ public void drawString(String s, float x, float y) { Java graphics sets string coordinates by the baseline of the first character so we need to shift down by the height of the textbox */ - txt.setAnchor(new Rectangle((int)x, (int)y, (int)width, (int)height)); + txt.setAnchor(new Rectangle( + MathUtil.safeFloatToInt(x), + MathUtil.safeFloatToInt(y), + MathUtil.safeFloatToInt(width), + MathUtil.safeFloatToInt(height))); } /** diff --git a/poi/src/main/java/org/apache/poi/ss/format/CellDateFormatter.java b/poi/src/main/java/org/apache/poi/ss/format/CellDateFormatter.java index 24a95b023f0..8deb4f24229 100644 --- a/poi/src/main/java/org/apache/poi/ss/format/CellDateFormatter.java +++ b/poi/src/main/java/org/apache/poi/ss/format/CellDateFormatter.java @@ -27,6 +27,7 @@ Licensed to the Apache Software Foundation (ASF) under one or more import java.util.regex.Matcher; import org.apache.poi.util.LocaleUtil; +import org.apache.poi.util.MathUtil; import org.apache.poi.util.StringUtil; /** @@ -186,7 +187,7 @@ public synchronized void formatValue(StringBuffer toAppendTo, Object value) { } else { Calendar c = (Calendar)EXCEL_EPOCH_CAL.clone(); // If milliseconds were not requested in the format string, round the seconds. - int seconds = (int) (sFmt == null ? Math.round(v / 1000) : v / 1000); + int seconds = MathUtil.safeDoubleToInt(sFmt == null ? Math.round(v / 1000) : v / 1000); c.add(Calendar.SECOND, seconds); c.add(Calendar.MILLISECOND, (int)(v % 1000)); value = c.getTime(); diff --git a/poi/src/main/java/org/apache/poi/ss/format/SimpleFraction.java b/poi/src/main/java/org/apache/poi/ss/format/SimpleFraction.java index f097820ab2e..cea6c2bbb78 100644 --- a/poi/src/main/java/org/apache/poi/ss/format/SimpleFraction.java +++ b/poi/src/main/java/org/apache/poi/ss/format/SimpleFraction.java @@ -133,9 +133,9 @@ private static SimpleFraction buildFractionMaxDenominator(double value, double e } if (q2 < maxDenominator) { - return new SimpleFraction((int) p2, (int)q2); + return new SimpleFraction(Math.toIntExact(p2), Math.toIntExact(q2)); } else { - return new SimpleFraction((int)p1, (int)q1); + return new SimpleFraction(Math.toIntExact(p1), Math.toIntExact(q1)); } } diff --git a/poi/src/main/java/org/apache/poi/ss/formula/PlainCellCache.java b/poi/src/main/java/org/apache/poi/ss/formula/PlainCellCache.java index 75230cf93ec..19e00a7da55 100644 --- a/poi/src/main/java/org/apache/poi/ss/formula/PlainCellCache.java +++ b/poi/src/main/java/org/apache/poi/ss/formula/PlainCellCache.java @@ -63,15 +63,15 @@ public int getRowIndex() { } public int getColumnIndex() { - return (int)(_bookSheetColumn & 0x000FFFF); + return Math.toIntExact(_bookSheetColumn & 0x000FFFF); } public int getSheetIndex() { - return (int)((_bookSheetColumn >> 32) & 0xFFFF); + return Math.toIntExact((_bookSheetColumn >> 32) & 0xFFFF); } public int getBookIndex() { - return (int)((_bookSheetColumn >> 48) & 0xFFFF); + return Math.toIntExact((_bookSheetColumn >> 48) & 0xFFFF); } } diff --git a/poi/src/main/java/org/apache/poi/ss/formula/atp/WorkdayCalculator.java b/poi/src/main/java/org/apache/poi/ss/formula/atp/WorkdayCalculator.java index 78739843772..03afebdd1ad 100644 --- a/poi/src/main/java/org/apache/poi/ss/formula/atp/WorkdayCalculator.java +++ b/poi/src/main/java/org/apache/poi/ss/formula/atp/WorkdayCalculator.java @@ -28,6 +28,7 @@ Licensed to the Apache Software Foundation (ASF) under one or more import org.apache.poi.ss.usermodel.DateUtil; import org.apache.poi.util.LocaleUtil; +import org.apache.poi.util.MathUtil; /** * A calculator for workdays, considering dates as excel representations. @@ -100,7 +101,8 @@ public int calculateWorkdays(double start, double end, double[] holidays) { int weekendDay1Past = weekendDays.length == 0 ? 0 : this.pastDaysOfWeek(start, end, weekendDays[0]); int weekendDay2Past = weekendDays.length <= 1 ? 0 : this.pastDaysOfWeek(start, end, weekendDays[1]); int nonWeekendHolidays = this.calculateNonWeekendHolidays(start, end, holidays); - return (int) (end - start + 1) - weekendDay1Past - weekendDay2Past - nonWeekendHolidays; + return MathUtil.safeDoubleToInt( + (end - start + 1) - weekendDay1Past - weekendDay2Past - nonWeekendHolidays); } /** @@ -152,8 +154,8 @@ public Date calculateWorkdays(double start, int workdays, int weekendType, doubl */ protected int pastDaysOfWeek(double start, double end, int dayOfWeek) { int pastDaysOfWeek = 0; - int startDay = (int) Math.floor(Math.min(start, end)); - int endDay = (int) Math.floor(Math.max(end, start)); + int startDay = MathUtil.safeDoubleToInt(Math.floor(Math.min(start, end))); + int endDay = MathUtil.safeDoubleToInt(Math.floor(Math.max(end, start))); for (; startDay <= endDay; startDay++) { Calendar today = LocaleUtil.getLocaleCalendar(); today.setTime(DateUtil.getJavaDate(startDay)); diff --git a/poi/src/main/java/org/apache/poi/ss/formula/atp/WorkdayFunction.java b/poi/src/main/java/org/apache/poi/ss/formula/atp/WorkdayFunction.java index 3793bd7d365..cdb465fdbc2 100644 --- a/poi/src/main/java/org/apache/poi/ss/formula/atp/WorkdayFunction.java +++ b/poi/src/main/java/org/apache/poi/ss/formula/atp/WorkdayFunction.java @@ -24,6 +24,7 @@ Licensed to the Apache Software Foundation (ASF) under one or more import org.apache.poi.ss.formula.eval.ValueEval; import org.apache.poi.ss.formula.functions.FreeRefFunction; import org.apache.poi.ss.usermodel.DateUtil; +import org.apache.poi.util.MathUtil; /** * Implementation of Excel 'Analysis ToolPak' function WORKDAY()
@@ -64,7 +65,8 @@ public ValueEval evaluate(ValueEval[] args, OperationEvaluationContext ec) { double[] holidays; try { start = this.evaluator.evaluateDateArg(args[0], srcCellRow, srcCellCol); - days = (int) Math.floor(this.evaluator.evaluateNumberArg(args[1], srcCellRow, srcCellCol)); + days = MathUtil.safeDoubleToInt( + Math.floor(this.evaluator.evaluateNumberArg(args[1], srcCellRow, srcCellCol))); ValueEval holidaysCell = args.length == 3 ? args[2] : null; holidays = this.evaluator.evaluateDatesArg(holidaysCell, srcCellRow, srcCellCol); return new NumberEval(DateUtil.getExcelDate(WorkdayCalculator.instance.calculateWorkdays(start, days, holidays))); diff --git a/poi/src/main/java/org/apache/poi/ss/formula/atp/WorkdayIntlFunction.java b/poi/src/main/java/org/apache/poi/ss/formula/atp/WorkdayIntlFunction.java index 158071b1410..a3be12b43a7 100644 --- a/poi/src/main/java/org/apache/poi/ss/formula/atp/WorkdayIntlFunction.java +++ b/poi/src/main/java/org/apache/poi/ss/formula/atp/WorkdayIntlFunction.java @@ -25,6 +25,7 @@ Licensed to the Apache Software Foundation (ASF) under one or more import org.apache.poi.ss.formula.eval.ValueEval; import org.apache.poi.ss.formula.functions.FreeRefFunction; import org.apache.poi.ss.usermodel.DateUtil; +import org.apache.poi.util.MathUtil; /** * Implementation of Excel 'Analysis ToolPak' function WORKDAY.INTL()
@@ -68,10 +69,12 @@ public ValueEval evaluate(ValueEval[] args, OperationEvaluationContext ec) { double[] holidays; try { start = this.evaluator.evaluateDateArg(args[0], srcCellRow, srcCellCol); - days = (int) Math.floor(this.evaluator.evaluateNumberArg(args[1], srcCellRow, srcCellCol)); + days = MathUtil.safeDoubleToInt( + Math.floor(this.evaluator.evaluateNumberArg(args[1], srcCellRow, srcCellCol))); if (args.length >= 3) { if (args[2] != BlankEval.instance) { - weekendType = (int) this.evaluator.evaluateNumberArg(args[2], srcCellRow, srcCellCol); + weekendType = MathUtil.safeDoubleToInt( + this.evaluator.evaluateNumberArg(args[2], srcCellRow, srcCellCol)); } if (!WorkdayCalculator.instance.getValidWeekendTypes().contains(weekendType)) { return ErrorEval.NUM_ERROR; diff --git a/poi/src/main/java/org/apache/poi/ss/formula/functions/AggregateFunction.java b/poi/src/main/java/org/apache/poi/ss/formula/functions/AggregateFunction.java index 8b76ac161ac..6404358ca5d 100644 --- a/poi/src/main/java/org/apache/poi/ss/formula/functions/AggregateFunction.java +++ b/poi/src/main/java/org/apache/poi/ss/formula/functions/AggregateFunction.java @@ -23,6 +23,7 @@ Licensed to the Apache Software Foundation (ASF) under one or more import org.apache.poi.ss.formula.eval.NumberEval; import org.apache.poi.ss.formula.eval.OperandResolver; import org.apache.poi.ss.formula.eval.ValueEval; +import org.apache.poi.util.MathUtil; public abstract class AggregateFunction extends MultiOperandNumericFunction { @@ -116,7 +117,7 @@ public ValueEval evaluate(int srcRowIndex, int srcColumnIndex, ValueEval arg0, } else if (Double.compare(n, N) == 0) { result = StatsLib.kthLargest(ds, 1); } else { - int k = (int) n; + int k = MathUtil.safeDoubleToInt(n); double d = n - k; result = StatsLib.kthSmallest(ds, k) + d * (StatsLib.kthSmallest(ds, k + 1) - StatsLib.kthSmallest(ds, k)); diff --git a/poi/src/main/java/org/apache/poi/ss/formula/functions/WeekNum.java b/poi/src/main/java/org/apache/poi/ss/formula/functions/WeekNum.java index c9101dc71aa..938eae0ef1c 100644 --- a/poi/src/main/java/org/apache/poi/ss/formula/functions/WeekNum.java +++ b/poi/src/main/java/org/apache/poi/ss/formula/functions/WeekNum.java @@ -33,6 +33,7 @@ Licensed to the Apache Software Foundation (ASF) under one or more import org.apache.poi.ss.formula.eval.OperandResolver; import org.apache.poi.ss.formula.eval.ValueEval; import org.apache.poi.ss.usermodel.DateUtil; +import org.apache.poi.util.MathUtil; /** * Implementation for Excel WeekNum() function. @@ -75,7 +76,7 @@ public ValueEval evaluate(int srcRowIndex, int srcColumnIndex, ValueEval serialN try { ValueEval ve = OperandResolver.getSingleValue(returnTypeVE, srcRowIndex, srcColumnIndex); if (ve instanceof MissingArgEval) { - returnType = (int)DEFAULT_RETURN_TYPE.getNumberValue(); + returnType = MathUtil.safeDoubleToInt(DEFAULT_RETURN_TYPE.getNumberValue()); } else { returnType = OperandResolver.coerceValueToInt(ve); } From cbec32768f8f350c09c932fef87c6c44869a255f Mon Sep 17 00:00:00 2001 From: PJ Fanning Date: Wed, 27 May 2026 22:43:02 +0100 Subject: [PATCH 2/3] try to fix tests --- .../poi/xssf/model/SharedStringsTable.java | 4 +-- .../TestReadOnlySharedStringsTable.java | 35 +++++-------------- test-data/poi-integration-exceptions.csv | 2 ++ 3 files changed, 12 insertions(+), 29 deletions(-) diff --git a/poi-ooxml/src/main/java/org/apache/poi/xssf/model/SharedStringsTable.java b/poi-ooxml/src/main/java/org/apache/poi/xssf/model/SharedStringsTable.java index d5e0bed8149..57af5bbf091 100644 --- a/poi-ooxml/src/main/java/org/apache/poi/xssf/model/SharedStringsTable.java +++ b/poi-ooxml/src/main/java/org/apache/poi/xssf/model/SharedStringsTable.java @@ -124,8 +124,8 @@ public void readFrom(InputStream is) throws IOException { int cnt = 0; _sstDoc = SstDocument.Factory.parse(is, DEFAULT_XML_OPTIONS); CTSst sst = _sstDoc.getSst(); - count = (int)sst.getCount(); - uniqueCount = (int)sst.getUniqueCount(); + count = Math.toIntExact(sst.getCount()); + uniqueCount = Math.toIntExact(sst.getUniqueCount()); //noinspection deprecation for (CTRst st : sst.getSiArray()) { stmap.put(xmlText(st), cnt); diff --git a/poi-ooxml/src/test/java/org/apache/poi/xssf/eventusermodel/TestReadOnlySharedStringsTable.java b/poi-ooxml/src/test/java/org/apache/poi/xssf/eventusermodel/TestReadOnlySharedStringsTable.java index 4c7797695f6..bf7de93509a 100644 --- a/poi-ooxml/src/test/java/org/apache/poi/xssf/eventusermodel/TestReadOnlySharedStringsTable.java +++ b/poi-ooxml/src/test/java/org/apache/poi/xssf/eventusermodel/TestReadOnlySharedStringsTable.java @@ -86,23 +86,9 @@ void testParseMalformedCountFile() throws Exception { try (OPCPackage pkg = OPCPackage.open(_ssTests.openResourceAsStream("MalformedSSTCount.xlsx"))) { List parts = pkg.getPartsByName(Pattern.compile("/xl/sharedStrings.xml")); assertEquals(1, parts.size()); - - try (SharedStringsTable stbl = new SharedStringsTable(parts.get(0))) { - ReadOnlySharedStringsTable rtbl = new ReadOnlySharedStringsTable(parts.get(0)); - ReadOnlySharedStringsTable rtbl2; - try (InputStream stream = parts.get(0).getInputStream()) { - rtbl2 = new ReadOnlySharedStringsTable(stream); - } - - assertEquals(stbl.getCount(), rtbl.getCount()); - assertEquals(stbl.getUniqueCount(), rtbl.getUniqueCount()); - assertEquals(stbl.getUniqueCount(), rtbl2.getUniqueCount()); - for (int i = 0; i < stbl.getUniqueCount(); i++) { - RichTextString i1 = stbl.getItemAt(i); - assertEquals(i1.getString(), rtbl.getItemAt(i).getString()); - assertEquals(i1.getString(), rtbl2.getItemAt(i).getString()); - } - } + // the sharedStrings.xml contains a count value that is too large (8876876876876) + // we expect int values + assertThrows(ArithmeticException.class, () -> new ReadOnlySharedStringsTable(parts.get(0))); } } @@ -187,16 +173,11 @@ void testMinimalTable() throws IOException, SAXException { } @Test - void testHugeUniqueCount() throws IOException, SAXException { - ReadOnlySharedStringsTable tbl = new ReadOnlySharedStringsTable( + void testHugeUniqueCount() { + // the 99999999999999999 below is too large, we expect an int value + assertThrows(ArithmeticException.class, + () -> new ReadOnlySharedStringsTable( new ByteArrayInputStream(MINIMAL_XML.replace("49", "99999999999999999"). - getBytes(StandardCharsets.UTF_8))); - assertNotNull(tbl); - assertEquals(1569325055, tbl.getUniqueCount()); - assertEquals(55, tbl.getCount()); - assertTrue(tbl.includePhoneticRuns); - assertEquals("bla", tbl.getItemAt(0).getString()); - assertThrows(IllegalStateException.class, - () -> tbl.getItemAt(1).getString()); + getBytes(StandardCharsets.UTF_8)))); } } diff --git a/test-data/poi-integration-exceptions.csv b/test-data/poi-integration-exceptions.csv index e0849f0a0b2..16587c643b7 100644 --- a/test-data/poi-integration-exceptions.csv +++ b/test-data/poi-integration-exceptions.csv @@ -99,6 +99,8 @@ spreadsheet/sample.strict.xlsx,"handle,extract",XSSF,,org.apache.poi.ooxml.POIXM spreadsheet/sample.strict.xlsx,extract,OPC,,org.apache.poi.ooxml.POIXMLException,"Strict OOXML isn't currently supported, please see bug #57699", spreadsheet/57914.xlsx,"handle,extract",XSSF,,org.apache.poi.ooxml.POIXMLException,"Strict OOXML isn't currently supported, please see bug #57699", spreadsheet/57914.xlsx,extract,OPC,,org.apache.poi.ooxml.POIXMLException,"Strict OOXML isn't currently supported, please see bug #57699", +spreadsheet/MalformedSSTCount.xlsx,OPC,,java.lang.ArithmeticException,integer overflow, +spreadsheet/MalformedSSTCount.xlsx,XSSF,,java.lang.ArithmeticException,integer overflow, spreadsheet/poi-fuzz.xls,additional,HSSF,,org.apache.poi.util.RecordFormatException,Not enough data (0) to read requested (4) bytes, spreadsheet/poi-fuzz.xls,additional,HPSF,,java.lang.ArithmeticException,integer overflow, spreadsheet/poi-fuzz.xls,handle,HPSF,,org.opentest4j.AssertionFailedError,expected: but was: , From 30fef97b90864ae35ac224f943774c13498e91ca Mon Sep 17 00:00:00 2001 From: PJ Fanning Date: Wed, 27 May 2026 23:15:01 +0100 Subject: [PATCH 3/3] Update poi-integration-exceptions.csv --- test-data/poi-integration-exceptions.csv | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test-data/poi-integration-exceptions.csv b/test-data/poi-integration-exceptions.csv index 16587c643b7..9244a0337b9 100644 --- a/test-data/poi-integration-exceptions.csv +++ b/test-data/poi-integration-exceptions.csv @@ -99,8 +99,8 @@ spreadsheet/sample.strict.xlsx,"handle,extract",XSSF,,org.apache.poi.ooxml.POIXM spreadsheet/sample.strict.xlsx,extract,OPC,,org.apache.poi.ooxml.POIXMLException,"Strict OOXML isn't currently supported, please see bug #57699", spreadsheet/57914.xlsx,"handle,extract",XSSF,,org.apache.poi.ooxml.POIXMLException,"Strict OOXML isn't currently supported, please see bug #57699", spreadsheet/57914.xlsx,extract,OPC,,org.apache.poi.ooxml.POIXMLException,"Strict OOXML isn't currently supported, please see bug #57699", -spreadsheet/MalformedSSTCount.xlsx,OPC,,java.lang.ArithmeticException,integer overflow, -spreadsheet/MalformedSSTCount.xlsx,XSSF,,java.lang.ArithmeticException,integer overflow, +spreadsheet/MalformedSSTCount.xlsx,extract,OPC,,java.lang.ArithmeticException,integer overflow, +spreadsheet/MalformedSSTCount.xlsx,"handle,extract,additional",XSSF,,java.lang.ArithmeticException,integer overflow, spreadsheet/poi-fuzz.xls,additional,HSSF,,org.apache.poi.util.RecordFormatException,Not enough data (0) to read requested (4) bytes, spreadsheet/poi-fuzz.xls,additional,HPSF,,java.lang.ArithmeticException,integer overflow, spreadsheet/poi-fuzz.xls,handle,HPSF,,org.opentest4j.AssertionFailedError,expected: but was: ,