This is an automated email from the ASF dual-hosted git repository.
pjfanning 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 dfa955921c more number conversion changes (#1096)
dfa955921c is described below
commit dfa955921c773ac56bc09534214ca7e61aa74d47
Author: PJ Fanning <[email protected]>
AuthorDate: Wed May 27 23:38:01 2026 +0100
more number conversion changes (#1096)
* more number conversion changes
* try to fix tests
* Update poi-integration-exceptions.csv
---
.../eventusermodel/ReadOnlySharedStringsTable.java | 4 +--
.../apache/poi/xssf/model/SharedStringsTable.java | 4 +--
.../org/apache/poi/xssf/model/StylesTable.java | 2 +-
.../apache/poi/xssf/usermodel/XSSFCellStyle.java | 4 +--
.../xwpf/usermodel/XWPFDefaultParagraphStyle.java | 5 +++-
.../apache/poi/xwpf/usermodel/XWPFParagraph.java | 20 ++++++-------
.../org/apache/poi/xwpf/usermodel/XWPFRun.java | 2 +-
.../org/apache/poi/xwpf/usermodel/XWPFTable.java | 11 +++++--
.../apache/poi/xwpf/usermodel/XWPFTableCell.java | 4 ++-
.../apache/poi/xwpf/usermodel/XWPFTableRow.java | 7 +++--
.../TestReadOnlySharedStringsTable.java | 35 +++++-----------------
.../org/apache/poi/hssf/record/FeatRecord.java | 4 +--
.../java/org/apache/poi/sl/draw/SLGraphics.java | 7 ++++-
.../apache/poi/ss/format/CellDateFormatter.java | 3 +-
.../org/apache/poi/ss/format/SimpleFraction.java | 4 +--
.../org/apache/poi/ss/formula/PlainCellCache.java | 6 ++--
.../poi/ss/formula/atp/WorkdayCalculator.java | 8 +++--
.../apache/poi/ss/formula/atp/WorkdayFunction.java | 4 ++-
.../poi/ss/formula/atp/WorkdayIntlFunction.java | 7 +++--
.../ss/formula/functions/AggregateFunction.java | 3 +-
.../apache/poi/ss/formula/functions/WeekNum.java | 3 +-
test-data/poi-integration-exceptions.csv | 2 ++
22 files changed, 80 insertions(+), 69 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 f76ba8c8b5..df69936c7c 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 class ReadOnlySharedStringsTable extends
DefaultHandler implements Shared
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/SharedStringsTable.java
b/poi-ooxml/src/main/java/org/apache/poi/xssf/model/SharedStringsTable.java
index d5e0bed814..57af5bbf09 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 class SharedStringsTable extends POIXMLDocumentPart
implements SharedStri
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/main/java/org/apache/poi/xssf/model/StylesTable.java
b/poi-ooxml/src/main/java/org/apache/poi/xssf/model/StylesTable.java
index a4cd687fad..bb731f8b4a 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 class StylesTable extends POIXMLDocumentPart
implements Styles {
}
// 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 d4493192a1..9261589152 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 class XSSFCellStyle implements CellStyle,
Duplicatable {
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 adc7150dc1..9c46328b4f 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 @@
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 class XWPFDefaultParagraphStyle {
}
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 7ba7e404bf..680fe2433d 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 class XWPFParagraph implements IBodyElement,
IRunBody, ISDTContents, Para
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 class XWPFParagraph implements IBodyElement,
IRunBody, ISDTContents, Para
*/
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 class XWPFParagraph implements IBodyElement,
IRunBody, ISDTContents, Para
*/
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 class XWPFParagraph implements IBodyElement,
IRunBody, ISDTContents, Para
*/
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 class XWPFParagraph implements IBodyElement,
IRunBody, ISDTContents, Para
*/
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 class XWPFParagraph implements IBodyElement,
IRunBody, ISDTContents, Para
*/
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 class XWPFParagraph implements IBodyElement,
IRunBody, ISDTContents, Para
*/
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 class XWPFParagraph implements IBodyElement,
IRunBody, ISDTContents, Para
*/
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 class XWPFParagraph implements IBodyElement,
IRunBody, ISDTContents, Para
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 class XWPFParagraph implements IBodyElement,
IRunBody, ISDTContents, Para
*/
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 d28d628eab..687c518162 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 class XWPFRun implements ISDTContents,
IRunElement, CharacterRun {
* 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 33fc2a8d63..2fdb5e339d 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 @@ import java.util.function.Function;
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 class XWPFTable implements IBodyElement,
ISDTContents {
*/
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 class XWPFTable implements IBodyElement,
ISDTContents {
}
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 @@ public class XWPFTable implements IBodyElement,
ISDTContents {
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 cbf6b5fe56..cf5846a772 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 @@ import java.util.List;
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 class XWPFTableCell implements IBody, ICell {
}
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 4b43c0817c..2717fe5661 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 @@ import java.util.List;
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 @@ public class XWPFTableRow {
*/
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 class XWPFTableRow {
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-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 4c7797695f..bf7de93509 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 @@ public final class TestReadOnlySharedStringsTable {
try (OPCPackage pkg =
OPCPackage.open(_ssTests.openResourceAsStream("MalformedSSTCount.xlsx"))) {
List<PackagePart> 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 @@ public final class TestReadOnlySharedStringsTable {
}
@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/poi/src/main/java/org/apache/poi/hssf/record/FeatRecord.java
b/poi/src/main/java/org/apache/poi/hssf/record/FeatRecord.java
index 065bf91cb2..d7367409f2 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 final class FeatRecord extends StandardRecord {
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 47838a6ed5..2d5c294565 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 @@ import org.apache.poi.sl.usermodel.StrokeStyle;
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 class SLGraphics extends Graphics2D implements
Cloneable {
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 24a95b023f..8deb4f2422 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 @@ import java.util.Locale;
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 class CellDateFormatter extends CellFormatter {
} 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 f097820ab2..cea6c2bbb7 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 @@ public class SimpleFraction {
}
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 75230cf93e..19e00a7da5 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 @@ final class PlainCellCache {
}
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 7873984377..03afebdd1a 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 @@ import java.util.Set;
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 class WorkdayCalculator {
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 class WorkdayCalculator {
*/
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 3793bd7d36..cdb465fdbc 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 @@ import org.apache.poi.ss.formula.eval.NumberEval;
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()<br>
@@ -64,7 +65,8 @@ final class WorkdayFunction implements FreeRefFunction {
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 158071b141..a3be12b43a 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 @@ import org.apache.poi.ss.formula.eval.NumberEval;
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()<br>
@@ -68,10 +69,12 @@ final class WorkdayIntlFunction implements FreeRefFunction {
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 8b76ac161a..6404358ca5 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 @@ import org.apache.poi.ss.formula.eval.EvaluationException;
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 abstract class AggregateFunction extends
MultiOperandNumericFunction {
} 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 c9101dc71a..938eae0ef1 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 @@ 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.ss.usermodel.DateUtil;
+import org.apache.poi.util.MathUtil;
/**
* Implementation for Excel WeekNum() function.
@@ -75,7 +76,7 @@ public class WeekNum extends Fixed2ArgFunction implements
FreeRefFunction {
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);
}
diff --git a/test-data/poi-integration-exceptions.csv
b/test-data/poi-integration-exceptions.csv
index e0849f0a0b..9244a0337b 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,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:
<true> but was: <false>,
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]