Skip to content

Commit da95c1e

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 765865d6bc 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 Autoported commit. Original commit hash: [b50863a0d]
1 parent 3ab56e4 commit da95c1e

File tree

2 files changed

+13
-7
lines changed

2 files changed

+13
-7
lines changed

itext/itext.io/itext/io/source/ByteUtils.cs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -131,11 +131,6 @@ internal static byte[] GetIsoBytes(double d, ByteBuffer buffer) {
131131
}
132132

133133
internal static byte[] GetIsoBytes(double d, ByteBuffer buffer, bool highPrecision) {
134-
if (double.IsNaN(d)) {
135-
ILog logger = LogManager.GetLogger(typeof(ByteUtils));
136-
logger.Error(iText.IO.LogMessageConstant.ATTEMPT_PROCESS_NAN);
137-
d = 0;
138-
}
139134
if (highPrecision) {
140135
if (Math.Abs(d) < 0.000001) {
141136
if (buffer != null) {
@@ -146,6 +141,11 @@ internal static byte[] GetIsoBytes(double d, ByteBuffer buffer, bool highPrecisi
146141
return zero;
147142
}
148143
}
144+
if (double.IsNaN(d)) {
145+
ILog logger = LogManager.GetLogger(typeof(ByteUtils));
146+
logger.Error(iText.IO.LogMessageConstant.ATTEMPT_PROCESS_NAN);
147+
d = 0;
148+
}
149149
byte[] result = DecimalFormatUtil.FormatNumber(d, "0.######").GetBytes(iText.IO.Util.EncodingUtil.ISO_8859_1
150150
);
151151
if (buffer != null) {
@@ -268,10 +268,16 @@ internal static byte[] GetIsoBytes(double d, ByteBuffer buffer, bool highPrecisi
268268
d += 0.5;
269269
long v;
270270
if (d > long.MaxValue) {
271-
//by default cast logic do the same, but not in .NET
271+
// by default cast logic do the same, but not in .NET
272272
v = long.MaxValue;
273273
}
274274
else {
275+
if (double.IsNaN(d)) {
276+
ILog logger = LogManager.GetLogger(typeof(ByteUtils));
277+
logger.Error(iText.IO.LogMessageConstant.ATTEMPT_PROCESS_NAN);
278+
// in java NaN casted to long results in 0, but in .NET it results in long.MIN_VALUE
279+
d = 0;
280+
}
275281
v = (long)d;
276282
}
277283
int intLen = LongSize(v);

port-hash

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
aad6d465c3a248545818a0ab6d45a84661d2e137
1+
b50863a0d49b6660e15aa942d766bf701d21913c

0 commit comments

Comments
 (0)