Skip to content

Commit ecf023b

Browse files
committed
Fix #300: clear out MSB for float too (like double)
1 parent e3676c2 commit ecf023b

File tree

4 files changed

+25
-5
lines changed

4 files changed

+25
-5
lines changed

release-notes/CREDITS-2.x

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,11 @@ Knut Wannheden (@knutwannheden)
363363
* Contributed #518: Should not read past end for CBOR string values
364364
(2.18.1)
365365

366+
Steven Fackler (@sfackler)
367+
* Reported #300: (smile) Floats are encoded with sign extension while
368+
doubles without
369+
(2.19.0)
370+
366371
Idan Sheinberg (@sheinbergon)
367372
* Reported #308: (avro) Incorrect serialization for `LogicalType.Decimal` (Java
368373
`BigDecimal`)

release-notes/VERSION-2.x

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ Active maintainers:
1616

1717
2.19.0 (not yet released)
1818

19+
#300: (smile) Floats are encoded with sign extension while doubles without
20+
(reported by Steven F)
1921
#308: (avro) Incorrect serialization for `LogicalType.Decimal` (Java `BigDecimal`)
2022
(reported by Idan S)
2123
(fix contributed by Michal F)

smile/src/main/java/com/fasterxml/jackson/dataformat/smile/SmileGenerator.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1726,13 +1726,13 @@ public void writeNumber(float f) throws IOException
17261726
int i = Float.floatToRawIntBits(f);
17271727
_outputBuffer[_outputTail++] = TOKEN_BYTE_FLOAT_32;
17281728
_outputBuffer[_outputTail+4] = (byte) (i & 0x7F);
1729-
i >>= 7;
1729+
i >>>= 7;
17301730
_outputBuffer[_outputTail+3] = (byte) (i & 0x7F);
1731-
i >>= 7;
1731+
i >>>= 7;
17321732
_outputBuffer[_outputTail+2] = (byte) (i & 0x7F);
1733-
i >>= 7;
1733+
i >>>= 7;
17341734
_outputBuffer[_outputTail+1] = (byte) (i & 0x7F);
1735-
i >>= 7;
1735+
i >>>= 7;
17361736
_outputBuffer[_outputTail] = (byte) (i & 0x7F);
17371737
_outputTail += 5;
17381738
}

smile/src/test/java/com/fasterxml/jackson/dataformat/smile/gen/SmileGeneratorNumbersTest.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
package com.fasterxml.jackson.dataformat.smile.gen;
22

33
import java.io.ByteArrayOutputStream;
4+
import java.util.Arrays;
45

56
import org.junit.jupiter.api.Test;
67

78
import com.fasterxml.jackson.dataformat.smile.*;
89

10+
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
911
import static org.junit.jupiter.api.Assertions.assertEquals;
1012

1113
public class SmileGeneratorNumbersTest
@@ -146,7 +148,12 @@ public void testFloatUnusedBits() throws Exception
146148
byte[] encoded = out.toByteArray();
147149
assertEquals(6, encoded.length);
148150
assertEquals(0x28, encoded[0]); // type byte, float
149-
}
151+
152+
// From 0x80 0x00 0x00 0x00 (spread over 5 x 7bits)
153+
assertArrayEquals(new byte[] {
154+
0x08, 0x00, 0x00, 0x00, 0x00
155+
}, Arrays.copyOfRange(encoded, 1, encoded.length));
156+
}
150157

151158
// [dataformats-binary#300]
152159
@Test
@@ -159,6 +166,12 @@ public void testDoubleUnusedBits() throws Exception
159166
byte[] encoded = out.toByteArray();
160167
assertEquals(11, encoded.length);
161168
assertEquals(0x29, encoded[0]); // type byte, double
169+
// From 0x80 0x00 0x00 0x00 ... 0x00 (spread over 10 x 7 bits)
170+
171+
assertArrayEquals(new byte[] {
172+
0x01, 0x00, 0x00, 0x00, 0x00,
173+
0x00, 0x00, 0x00, 0x00, 0x00
174+
}, Arrays.copyOfRange(encoded, 1, encoded.length));
162175
}
163176

164177
// #16: Problems with 'Stringified' numbers

0 commit comments

Comments
 (0)