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 d0eb055f99 more safe conversions
d0eb055f99 is described below
commit d0eb055f99afcc01353ccb51d4c99a6a4f5fa2bd
Author: PJ Fanning <[email protected]>
AuthorDate: Wed Jun 3 21:57:33 2026 +0100
more safe conversions
---
.../src/main/java/org/apache/poi/hwmf/draw/HwmfImageRenderer.java | 6 +++++-
.../src/main/java/org/apache/poi/hwmf/draw/HwmfROP3Composite.java | 5 +++--
.../src/main/java/org/apache/poi/hwmf/record/HwmfBitmapDib.java | 6 +++---
.../src/main/java/org/apache/poi/hwmf/record/HwmfFill.java | 8 ++++----
.../src/main/java/org/apache/poi/hwmf/record/HwmfMisc.java | 2 +-
.../src/main/java/org/apache/poi/hwmf/record/HwmfText.java | 2 +-
.../src/main/java/org/apache/poi/hwmf/usermodel/HwmfPicture.java | 4 ++--
.../java/org/apache/poi/hwpf/converter/AbstractWordUtils.java | 2 +-
.../src/main/java/org/apache/poi/hwpf/model/ListData.java | 3 ++-
.../java/org/apache/poi/hwpf/sprm/CharacterSprmUncompressor.java | 3 ++-
.../src/main/java/org/apache/poi/hwpf/usermodel/HWPFList.java | 3 ++-
11 files changed, 26 insertions(+), 18 deletions(-)
diff --git
a/poi-scratchpad/src/main/java/org/apache/poi/hwmf/draw/HwmfImageRenderer.java
b/poi-scratchpad/src/main/java/org/apache/poi/hwmf/draw/HwmfImageRenderer.java
index 9a44028f07..c43ec8b54a 100644
---
a/poi-scratchpad/src/main/java/org/apache/poi/hwmf/draw/HwmfImageRenderer.java
+++
b/poi-scratchpad/src/main/java/org/apache/poi/hwmf/draw/HwmfImageRenderer.java
@@ -40,6 +40,7 @@ import org.apache.poi.sl.draw.EmbeddedExtractor;
import org.apache.poi.sl.draw.ImageRenderer;
import org.apache.poi.sl.usermodel.PictureData.PictureType;
import org.apache.poi.util.Internal;
+import org.apache.poi.util.MathUtil;
import org.apache.poi.util.Units;
/**
@@ -93,7 +94,10 @@ public class HwmfImageRenderer implements ImageRenderer,
EmbeddedExtractor {
return new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB);
}
- BufferedImage bufImg = new BufferedImage((int)dim.getWidth(),
(int)dim.getHeight(), BufferedImage.TYPE_INT_ARGB);
+ BufferedImage bufImg = new BufferedImage(
+ MathUtil.safeDoubleToInt(dim.getWidth()),
+ MathUtil.safeDoubleToInt(dim.getHeight()),
+ BufferedImage.TYPE_INT_ARGB);
Graphics2D g = bufImg.createGraphics();
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g.setRenderingHint(RenderingHints.KEY_RENDERING,
RenderingHints.VALUE_RENDER_QUALITY);
diff --git
a/poi-scratchpad/src/main/java/org/apache/poi/hwmf/draw/HwmfROP3Composite.java
b/poi-scratchpad/src/main/java/org/apache/poi/hwmf/draw/HwmfROP3Composite.java
index b176aedbab..7a542a768a 100644
---
a/poi-scratchpad/src/main/java/org/apache/poi/hwmf/draw/HwmfROP3Composite.java
+++
b/poi-scratchpad/src/main/java/org/apache/poi/hwmf/draw/HwmfROP3Composite.java
@@ -33,6 +33,7 @@ import java.util.ArrayDeque;
import java.util.Deque;
import org.apache.poi.hwmf.record.HwmfTernaryRasterOp;
+import org.apache.poi.util.MathUtil;
/**
* HWMFs Raster Operation for Ternary arguments (Source / Destination /
Pattern)
@@ -81,8 +82,8 @@ public class HwmfROP3Composite implements Composite {
int w = Math.min(src.getWidth(), dstIn.getWidth());
int h = Math.min(src.getHeight(), dstIn.getHeight());
- int startX = (int)startPnt.getX();
- int startY = (int)startPnt.getY();
+ int startX = MathUtil.safeDoubleToInt(startPnt.getX());
+ int startY = MathUtil.safeDoubleToInt(startPnt.getY());
int offsetY = dstIn.getSampleModelTranslateY();
int offsetX = dstIn.getSampleModelTranslateX();
diff --git
a/poi-scratchpad/src/main/java/org/apache/poi/hwmf/record/HwmfBitmapDib.java
b/poi-scratchpad/src/main/java/org/apache/poi/hwmf/record/HwmfBitmapDib.java
index e06aaeaf2d..4669d6ed97 100644
--- a/poi-scratchpad/src/main/java/org/apache/poi/hwmf/record/HwmfBitmapDib.java
+++ b/poi-scratchpad/src/main/java/org/apache/poi/hwmf/record/HwmfBitmapDib.java
@@ -364,13 +364,13 @@ public class HwmfBitmapDib implements GenericRecord {
return 0;
case BI_BITCOUNT_1:
// 2 colors
- return readRGBQuad(leis, (int)(headerColorUsed == 0 ? 2 :
Math.min(headerColorUsed,2)));
+ return readRGBQuad(leis, headerColorUsed == 0 ? 2 :
Math.toIntExact(Math.min(headerColorUsed,2)));
case BI_BITCOUNT_2:
// 16 colors
- return readRGBQuad(leis, (int)(headerColorUsed == 0 ? 16 :
Math.min(headerColorUsed,16)));
+ return readRGBQuad(leis, headerColorUsed == 0 ? 16 :
Math.toIntExact(Math.min(headerColorUsed,16)));
case BI_BITCOUNT_3:
// 256 colors
- return readRGBQuad(leis, (int)(headerColorUsed == 0 ? 256 :
Math.min(headerColorUsed,256)));
+ return readRGBQuad(leis, headerColorUsed == 0 ? 256 :
Math.toIntExact(Math.min(headerColorUsed,256)));
case BI_BITCOUNT_4:
switch (headerCompression) {
case BI_RGB:
diff --git
a/poi-scratchpad/src/main/java/org/apache/poi/hwmf/record/HwmfFill.java
b/poi-scratchpad/src/main/java/org/apache/poi/hwmf/record/HwmfFill.java
index 7dfa823252..5bd7db8028 100644
--- a/poi-scratchpad/src/main/java/org/apache/poi/hwmf/record/HwmfFill.java
+++ b/poi-scratchpad/src/main/java/org/apache/poi/hwmf/record/HwmfFill.java
@@ -605,7 +605,7 @@ public class HwmfFill {
size += readBounds2(leis, srcBounds);
size += readBounds2(leis, dstBounds);
- size += bitmap.init(leis, (int)(recordSize-6-size));
+ size += bitmap.init(leis, Math.toIntExact(recordSize-6-size));
return size;
}
@@ -758,7 +758,7 @@ public class HwmfFill {
size += readBounds2(leis, dstBounds);
dib = new HwmfBitmapDib();
- size += dib.init(leis, (int)(recordSize-6-size));
+ size += dib.init(leis, Math.toIntExact(recordSize-6-size));
srcBounds.setRect(srcPnt.getX(), srcPnt.getY(),
dstBounds.getWidth(), dstBounds.getHeight());
@@ -843,7 +843,7 @@ public class HwmfFill {
size += readBounds2(leis, dstBounds);
if (hasBitmap) {
target = new HwmfBitmapDib();
- size += target.init(leis, (int)(recordSize-6-size));
+ size += target.init(leis, Math.toIntExact(recordSize-6-size));
}
// the destination rectangle, having the same dimension as the
source rectangle
@@ -905,7 +905,7 @@ public class HwmfFill {
size += readBounds2(leis, dstBounds);
if (hasBitmap) {
target = new HwmfBitmapDib();
- size += target.init(leis, (int)(recordSize-6-size));
+ size += target.init(leis, Math.toIntExact(recordSize-6-size));
}
return size;
diff --git
a/poi-scratchpad/src/main/java/org/apache/poi/hwmf/record/HwmfMisc.java
b/poi-scratchpad/src/main/java/org/apache/poi/hwmf/record/HwmfMisc.java
index 8130f7d7e3..56d9af5a80 100644
--- a/poi-scratchpad/src/main/java/org/apache/poi/hwmf/record/HwmfMisc.java
+++ b/poi-scratchpad/src/main/java/org/apache/poi/hwmf/record/HwmfMisc.java
@@ -532,7 +532,7 @@ public class HwmfMisc {
case BS_HATCHED:
case BS_PATTERN:
patternDib = new HwmfBitmapDib();
- size += patternDib.init(leis, (int)(recordSize-6-size));
+ size += patternDib.init(leis,
Math.toIntExact(recordSize-6-size));
break;
case BS_INDEXED:
case BS_DIBPATTERN8X8:
diff --git
a/poi-scratchpad/src/main/java/org/apache/poi/hwmf/record/HwmfText.java
b/poi-scratchpad/src/main/java/org/apache/poi/hwmf/record/HwmfText.java
index 7710afddba..b78950b5cb 100644
--- a/poi-scratchpad/src/main/java/org/apache/poi/hwmf/record/HwmfText.java
+++ b/poi-scratchpad/src/main/java/org/apache/poi/hwmf/record/HwmfText.java
@@ -426,7 +426,7 @@ public class HwmfText {
@Override
public int init(LittleEndianInputStream leis, long recordSize, int
recordFunction) throws IOException {
// -6 bytes of record function and length header
- final int remainingRecordSize = (int)(recordSize-6);
+ final int remainingRecordSize = Math.toIntExact(recordSize-6);
int size = readPointS(leis, reference);
diff --git
a/poi-scratchpad/src/main/java/org/apache/poi/hwmf/usermodel/HwmfPicture.java
b/poi-scratchpad/src/main/java/org/apache/poi/hwmf/usermodel/HwmfPicture.java
index 1ab6965db2..ab9835d0d0 100644
---
a/poi-scratchpad/src/main/java/org/apache/poi/hwmf/usermodel/HwmfPicture.java
+++
b/poi-scratchpad/src/main/java/org/apache/poi/hwmf/usermodel/HwmfPicture.java
@@ -103,7 +103,7 @@ public class HwmfPicture implements Iterable<HwmfRecord>,
GenericRecord {
} else if (recordSizeLong < 0L) {
throw new RecordFormatException("record size can't be
< 0");
}
- recordSize = (int)recordSizeLong;
+ recordSize = Math.toIntExact(recordSizeLong);
recordFunction = leis.readShort();
} catch (Exception e) {
LOG.atError().log("unexpected eof - wmf file was
truncated");
@@ -126,7 +126,7 @@ public class HwmfPicture implements Iterable<HwmfRecord>,
GenericRecord {
records.add(wr);
consumedSize += wr.init(leis, recordSize, recordFunction);
- int remainingSize = (int)(recordSize - consumedSize);
+ int remainingSize = Math.toIntExact(recordSize - consumedSize);
if (remainingSize < 0) {
throw new RecordFormatException("read too many bytes.
record size: "+recordSize + "; comsumed size: "+consumedSize);
} else if(remainingSize > 0) {
diff --git
a/poi-scratchpad/src/main/java/org/apache/poi/hwpf/converter/AbstractWordUtils.java
b/poi-scratchpad/src/main/java/org/apache/poi/hwpf/converter/AbstractWordUtils.java
index b2bd30f808..74eb7ff3d3 100644
---
a/poi-scratchpad/src/main/java/org/apache/poi/hwpf/converter/AbstractWordUtils.java
+++
b/poi-scratchpad/src/main/java/org/apache/poi/hwpf/converter/AbstractWordUtils.java
@@ -311,7 +311,7 @@ public class AbstractWordUtils {
}
public static String getOpacity( int argbValue ) {
- int opacity = (int) ( ( argbValue & 0xFF000000L) >>> 24 );
+ int opacity = Math.toIntExact( ( argbValue & 0xFF000000L) >>> 24 );
if ( opacity == 0 || opacity == 0xFF )
return ".0";
diff --git
a/poi-scratchpad/src/main/java/org/apache/poi/hwpf/model/ListData.java
b/poi-scratchpad/src/main/java/org/apache/poi/hwpf/model/ListData.java
index d01b2dc46c..61b7f1fcac 100644
--- a/poi-scratchpad/src/main/java/org/apache/poi/hwpf/model/ListData.java
+++ b/poi-scratchpad/src/main/java/org/apache/poi/hwpf/model/ListData.java
@@ -21,6 +21,7 @@ import java.util.Arrays;
import java.util.concurrent.ThreadLocalRandom;
import org.apache.poi.util.Internal;
+import org.apache.poi.util.MathUtil;
@Internal
public final class ListData
@@ -118,7 +119,7 @@ public final class ListData
int resetListID()
{
- _lstf.setLsid( (int) ( ThreadLocalRandom.current().nextDouble() *
System.currentTimeMillis() ) );
+ _lstf.setLsid( MathUtil.safeDoubleToInt(
ThreadLocalRandom.current().nextDouble() * System.currentTimeMillis() ) );
return _lstf.getLsid();
}
diff --git
a/poi-scratchpad/src/main/java/org/apache/poi/hwpf/sprm/CharacterSprmUncompressor.java
b/poi-scratchpad/src/main/java/org/apache/poi/hwpf/sprm/CharacterSprmUncompressor.java
index 7565b1aedb..f253e7020c 100644
---
a/poi-scratchpad/src/main/java/org/apache/poi/hwpf/sprm/CharacterSprmUncompressor.java
+++
b/poi-scratchpad/src/main/java/org/apache/poi/hwpf/sprm/CharacterSprmUncompressor.java
@@ -30,6 +30,7 @@ import org.apache.poi.hwpf.usermodel.DateAndTime;
import org.apache.poi.hwpf.usermodel.ShadingDescriptor;
import org.apache.poi.util.Internal;
import org.apache.poi.util.LittleEndian;
+import org.apache.poi.util.MathUtil;
@Internal
public final class CharacterSprmUncompressor extends SprmUncompressor
@@ -522,7 +523,7 @@ public final class CharacterSprmUncompressor extends
SprmUncompressor
case 0x4d:
// sprmCHpsMul -- 0x4A4D
float percentage = sprm.getOperand() / 100.0f;
- int add = (int) ( percentage * newCHP.getHps() );
+ int add = MathUtil.safeDoubleToInt(percentage * newCHP.getHps());
newCHP.setHps( newCHP.getHps() + add );
break;
case 0x4e:
diff --git
a/poi-scratchpad/src/main/java/org/apache/poi/hwpf/usermodel/HWPFList.java
b/poi-scratchpad/src/main/java/org/apache/poi/hwpf/usermodel/HWPFList.java
index 28cbba7717..3a91304214 100644
--- a/poi-scratchpad/src/main/java/org/apache/poi/hwpf/usermodel/HWPFList.java
+++ b/poi-scratchpad/src/main/java/org/apache/poi/hwpf/usermodel/HWPFList.java
@@ -31,6 +31,7 @@ import org.apache.poi.hwpf.model.ListLevel;
import org.apache.poi.hwpf.model.StyleSheet;
import org.apache.poi.hwpf.sprm.CharacterSprmCompressor;
import org.apache.poi.hwpf.sprm.ParagraphSprmCompressor;
+import org.apache.poi.util.MathUtil;
/**
* This class is used to create a list in a Word document. It is used in
@@ -66,7 +67,7 @@ public final class HWPFList
public HWPFList( boolean numbered, StyleSheet styleSheet )
{
_listData = new ListData(
- (int) ( ThreadLocalRandom.current().nextDouble() *
System.currentTimeMillis() ), numbered );
+ MathUtil.safeDoubleToInt(
ThreadLocalRandom.current().nextDouble() * System.currentTimeMillis() ),
numbered );
_lfo = new LFO();
_lfo.setLsid( _listData.getLsid() );
_lfoData = new LFOData();
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]