Skip to content

Commit b082f58

Browse files
committed
Fixed FasterXML#312 (use 0xFF for padding empty/missing bytes as its not valid UTF-8 byte)
1 parent 6e06b38 commit b082f58

File tree

6 files changed

+26
-19
lines changed

6 files changed

+26
-19
lines changed

cbor/src/main/java/com/fasterxml/jackson/dataformat/cbor/CBORParser.java

+6-9
Original file line numberDiff line numberDiff line change
@@ -2886,7 +2886,7 @@ private final String _findDecodedFromSymbols(final int len) throws IOException
28862886
if (len < 5) {
28872887
int inPtr = _inputPtr;
28882888
final byte[] inBuf = _inputBuffer;
2889-
int q = inBuf[inPtr] & 0xFF;
2889+
int q = _padQuadForNulls(inBuf[inPtr]);
28902890
if (len > 1) {
28912891
q = (q << 8) + (inBuf[++inPtr] & 0xFF);
28922892
if (len > 2) {
@@ -2910,7 +2910,7 @@ private final String _findDecodedFromSymbols(final int len) throws IOException
29102910
q1 = (q1 << 8) | (inBuf[inPtr++] & 0xFF);
29112911

29122912
if (len < 9) {
2913-
int q2 = (inBuf[inPtr++] & 0xFF);
2913+
int q2 = _padQuadForNulls(inBuf[inPtr++]);
29142914
int left = len - 5;
29152915
if (left > 0) {
29162916
q2 = (q2 << 8) + (inBuf[inPtr++] & 0xFF);
@@ -2932,7 +2932,7 @@ private final String _findDecodedFromSymbols(final int len) throws IOException
29322932
q2 = (q2 << 8) | (inBuf[inPtr++] & 0xFF);
29332933

29342934
if (len < 13) {
2935-
int q3 = (inBuf[inPtr++] & 0xFF);
2935+
int q3 = _padQuadForNulls(inBuf[inPtr++]);
29362936
int left = len - 9;
29372937
if (left > 0) {
29382938
q3 = (q3 << 8) + (inBuf[inPtr++] & 0xFF);
@@ -2981,7 +2981,7 @@ private final String _findDecodedLong(int len, int q1, int q2) throws IOExceptio
29812981
} while ((len -= 4) > 3);
29822982
// and then leftovers
29832983
if (len > 0) {
2984-
int q = inBuf[inPtr] & 0xFF;
2984+
int q = _padQuadForNulls(inBuf[inPtr]);
29852985
if (len > 1) {
29862986
q = (q << 8) + (inBuf[++inPtr] & 0xFF);
29872987
if (len > 2) {
@@ -3012,12 +3012,9 @@ private static int[] _growArrayTo(int[] arr, int minSize) {
30123012
}
30133013

30143014
// Helper method needed to fix [dataformats-binary#312], masking of 0x00 character
3015-
// 26-Feb-2022, tatu: not yet used
3016-
/*
3017-
private final static int _padLastQuad(int q, int bytes) {
3018-
return (bytes == 4) ? q : (q | (-1 << (bytes << 3)));
3015+
private final static int _padQuadForNulls(int firstByte) {
3016+
return (firstByte & 0xFF) | 0xFFFFFF00;
30193017
}
3020-
*/
30213018

30223019
/*
30233020
/**********************************************************

cbor/src/test/java/com/fasterxml/jackson/dataformat/cbor/failing/SymbolTable312Test.java renamed to cbor/src/test/java/com/fasterxml/jackson/dataformat/cbor/parse/SymbolTable312Test.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.fasterxml.jackson.dataformat.cbor.failing;
1+
package com.fasterxml.jackson.dataformat.cbor.parse;
22

33
import java.nio.charset.StandardCharsets;
44

release-notes/CREDITS-2.x

+5
Original file line numberDiff line numberDiff line change
@@ -225,3 +225,8 @@ Martin Giannechini (MartinGian@github)
225225
* Contributed fix for #295: (ion) `jackson-dataformat-ion` does not handle null.struct
226226
deserialization correctly
227227
(2.13.0)
228+
229+
David Turner (DaveCTurner@github)
230+
231+
* Reported #312: (cbor, smile) Short NUL-only keys incorrectly detected as duplicates
232+
(2.14.0)

release-notes/VERSION-2.x

+2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ Modules:
1414

1515
#301: (cbor, smile) Missing configuration methods for format-specific
1616
parser/generator features
17+
#312: (cbor, smile) Short NUL-only keys incorrectly detected as duplicates
18+
(reported by David T)
1719

1820
2.13.1 (19-Dec-2021)
1921

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

+11-8
Original file line numberDiff line numberDiff line change
@@ -1793,6 +1793,7 @@ private final String _handleLongFieldName() throws IOException
17931793
if (quads >= _quadBuffer.length) {
17941794
_quadBuffer = _growArrayTo(_quadBuffer, _quadBuffer.length + 256);
17951795
}
1796+
q = _padLastQuad(q, bytes);
17961797
_quadBuffer[quads++] = q;
17971798
byteLen += bytes;
17981799
}
@@ -1825,7 +1826,7 @@ private final String _findDecodedFromSymbols(final int len) throws IOException
18251826
if (len < 5) {
18261827
int inPtr = _inputPtr;
18271828
final byte[] inBuf = _inputBuffer;
1828-
int q = inBuf[inPtr] & 0xFF;
1829+
int q = _padQuadForNulls(inBuf[inPtr]);
18291830
if (len > 1) {
18301831
q = (q << 8) + (inBuf[++inPtr] & 0xFF);
18311832
if (len > 2) {
@@ -1849,7 +1850,7 @@ private final String _findDecodedFromSymbols(final int len) throws IOException
18491850
q1 = (q1 << 8) | (inBuf[inPtr++] & 0xFF);
18501851

18511852
if (len < 9) {
1852-
int q2 = (inBuf[inPtr++] & 0xFF);
1853+
int q2 = _padQuadForNulls(inBuf[inPtr++]);
18531854
int left = len - 5;
18541855
if (left > 0) {
18551856
q2 = (q2 << 8) + (inBuf[inPtr++] & 0xFF);
@@ -1871,7 +1872,7 @@ private final String _findDecodedFromSymbols(final int len) throws IOException
18711872
q2 = (q2 << 8) | (inBuf[inPtr++] & 0xFF);
18721873

18731874
if (len < 13) {
1874-
int q3 = (inBuf[inPtr++] & 0xFF);
1875+
int q3 = _padQuadForNulls(inBuf[inPtr++]);
18751876
int left = len - 9;
18761877
if (left > 0) {
18771878
q3 = (q3 << 8) + (inBuf[inPtr++] & 0xFF);
@@ -1920,7 +1921,7 @@ private final String _findDecodedFixed12(int len, int q1, int q2) throws IOExcep
19201921
} while ((len -= 4) > 3);
19211922
// and then leftovers
19221923
if (len > 0) {
1923-
int q = inBuf[inPtr] & 0xFF;
1924+
int q = _padQuadForNulls(inBuf[inPtr]);
19241925
if (len > 1) {
19251926
q = (q << 8) + (inBuf[++inPtr] & 0xFF);
19261927
if (len > 2) {
@@ -1940,13 +1941,15 @@ private static int[] _growArrayTo(int[] arr, int minSize) {
19401941
return Arrays.copyOf(arr, size);
19411942
}
19421943

1943-
// Helper method needed to fix [dataformats-binary#312], masking of 0x00 character
1944-
// 26-Feb-2022, tatu: not yet used
1945-
/*
1944+
// Helper methods needed to fix [dataformats-binary#312], masking of 0x00 character
1945+
19461946
private final static int _padLastQuad(int q, int bytes) {
19471947
return (bytes == 4) ? q : (q | (-1 << (bytes << 3)));
19481948
}
1949-
*/
1949+
1950+
private final static int _padQuadForNulls(int firstByte) {
1951+
return (firstByte & 0xFF) | 0xFFFFFF00;
1952+
}
19501953

19511954
/*
19521955
/**********************************************************

smile/src/test/java/com/fasterxml/jackson/dataformat/smile/failing/SymbolTable312Test.java renamed to smile/src/test/java/com/fasterxml/jackson/dataformat/smile/parse/SymbolTable312Test.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.fasterxml.jackson.dataformat.smile.failing;
1+
package com.fasterxml.jackson.dataformat.smile.parse;
22

33
import java.nio.charset.StandardCharsets;
44

0 commit comments

Comments
 (0)