Skip to content

Commit b50863a

Browse files
yulian-gaponenkoiText-CI
authored andcommitted
Make explicit fallback to zero more focused when NaN is attempted to be written
This is a fixup for a 765865d a few commits back. Original commit comments: This was already how Java behaved in non-high-precision cases. However NaN processing would produce corrupted PDF in Java for highPrecision case and also caused exceptions in .NET. DEVSIX-2883
1 parent aad6d46 commit b50863a

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

io/src/main/java/com/itextpdf/io/source/ByteUtils.java

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -128,11 +128,6 @@ static byte[] getIsoBytes(double d, ByteBuffer buffer) {
128128
}
129129

130130
static byte[] getIsoBytes(double d, ByteBuffer buffer, boolean highPrecision) {
131-
if (Double.isNaN(d)) {
132-
Logger logger = LoggerFactory.getLogger(ByteUtils.class);
133-
logger.error(LogMessageConstant.ATTEMPT_PROCESS_NAN);
134-
d = 0;
135-
}
136131
if (highPrecision) {
137132
if (Math.abs(d) < 0.000001) {
138133
if (buffer != null) {
@@ -142,6 +137,11 @@ static byte[] getIsoBytes(double d, ByteBuffer buffer, boolean highPrecision) {
142137
return zero;
143138
}
144139
}
140+
if (Double.isNaN(d)) {
141+
Logger logger = LoggerFactory.getLogger(ByteUtils.class);
142+
logger.error(LogMessageConstant.ATTEMPT_PROCESS_NAN);
143+
d = 0;
144+
}
145145
byte[] result = DecimalFormatUtil.formatNumber(d, "0.######").getBytes(StandardCharsets.ISO_8859_1);
146146
if (buffer != null) {
147147
buffer.prepend(result);
@@ -240,9 +240,15 @@ static byte[] getIsoBytes(double d, ByteBuffer buffer, boolean highPrecision) {
240240
d += 0.5;
241241
long v;
242242
if (d > Long.MAX_VALUE) {
243-
//by default cast logic do the same, but not in .NET
243+
// by default cast logic do the same, but not in .NET
244244
v = Long.MAX_VALUE;
245245
} else {
246+
if (Double.isNaN(d)) {
247+
Logger logger = LoggerFactory.getLogger(ByteUtils.class);
248+
logger.error(LogMessageConstant.ATTEMPT_PROCESS_NAN);
249+
// in java NaN casted to long results in 0, but in .NET it results in long.MIN_VALUE
250+
d = 0;
251+
}
246252
v = (long) d;
247253
}
248254
int intLen = longSize(v);

0 commit comments

Comments
 (0)