Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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);
}

Expand All @@ -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);
}

Expand Down Expand Up @@ -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);
}

Expand All @@ -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);
}

Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

/**
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -1066,7 +1070,8 @@ private int getCellMargin(Function<CTTblCellMar,CTTblWidth> 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())));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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())));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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())));
}

/**
Expand All @@ -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));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,23 +86,9 @@ void testParseMalformedCountFile() throws Exception {
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)));
}
}

Expand Down Expand Up @@ -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))));
}
}
4 changes: 2 additions & 2 deletions poi/src/main/java/org/apache/poi/hssf/record/FeatRecord.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
7 changes: 6 additions & 1 deletion poi/src/main/java/org/apache/poi/sl/draw/SLGraphics.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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)));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down
Loading