Skip to content

Commit 2ddb1e6

Browse files
committed
Minor refactoring of error reporting of invalid surrogates with CBOR generation
1 parent dcd26e4 commit 2ddb1e6

File tree

3 files changed

+32
-18
lines changed

3 files changed

+32
-18
lines changed

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

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1450,7 +1450,8 @@ protected final void _writeChunkedString(char[] text, int offset, int len)
14501450
* Helper method called when the whole character sequence is known to fit in
14511451
* the output buffer regardless of UTF-8 expansion.
14521452
*/
1453-
private final int _encode(int outputPtr, char[] str, int i, int end) {
1453+
private final int _encode(int outputPtr, char[] str, int i, int end) throws IOException
1454+
{
14541455
// First: let's see if it's all ASCII: that's rather fast
14551456
final byte[] outBuf = _outputBuffer;
14561457
final int outputStart = outputPtr;
@@ -1470,7 +1471,8 @@ private final int _encode(int outputPtr, char[] str, int i, int end) {
14701471
* characters.
14711472
*/
14721473
private final int _shortUTF8Encode2(char[] str, int i, int end,
1473-
int outputPtr, int outputStart) {
1474+
int outputPtr, int outputStart) throws IOException
1475+
{
14741476
final byte[] outBuf = _outputBuffer;
14751477
while (i < end) {
14761478
int c = str[i++];
@@ -1508,7 +1510,7 @@ private final int _shortUTF8Encode2(char[] str, int i, int end,
15081510
return (outputPtr - outputStart);
15091511
}
15101512

1511-
private final int _encode(int outputPtr, String str, int len) {
1513+
private final int _encode(int outputPtr, String str, int len) throws IOException {
15121514
final byte[] outBuf = _outputBuffer;
15131515
final int outputStart = outputPtr;
15141516

@@ -1523,7 +1525,8 @@ private final int _encode(int outputPtr, String str, int len) {
15231525
}
15241526

15251527
private final int _encode2(int i, int outputPtr, String str, int len,
1526-
int outputStart) {
1528+
int outputStart) throws IOException
1529+
{
15271530
final byte[] outBuf = _outputBuffer;
15281531
// no; non-ASCII stuff, slower loop
15291532
while (i < len) {
@@ -1562,7 +1565,9 @@ private final int _encode2(int i, int outputPtr, String str, int len,
15621565
return (outputPtr - outputStart);
15631566
}
15641567

1565-
private int _invalidSurrogateStart(int code, byte[] outBuf, int outputPtr) {
1568+
private int _invalidSurrogateStart(int code, byte[] outBuf, int outputPtr)
1569+
throws IOException
1570+
{
15661571
if (isEnabled(Feature.LENIENT_UTF_ENCODING)) {
15671572
return _appendReplacementChar(outBuf, outputPtr);
15681573
}
@@ -1571,25 +1576,28 @@ private int _invalidSurrogateStart(int code, byte[] outBuf, int outputPtr) {
15711576
// but there is no second part to encode
15721577
if (code <= SURR1_LAST) {
15731578
// Unmatched first part (closing without second part?)
1574-
throw new IllegalArgumentException(String.format(
1579+
_reportError(String.format(
15751580
"Unmatched surrogate pair, starts with valid high surrogate (0x%04X) but ends without low surrogate",
15761581
code));
15771582
}
1578-
throw new IllegalArgumentException(String.format(
1583+
_reportError(String.format(
15791584
"Invalid surrogate pair, starts with invalid high surrogate (0x%04X), not in valid range [0xD800, 0xDBFF]",
15801585
code));
1586+
return 0; // never gets here
15811587
}
15821588

15831589
private int _invalidSurrogateEnd(int surr1, int surr2,
15841590
byte[] outBuf, int outputPtr)
1591+
throws IOException
15851592
{
15861593
if (isEnabled(Feature.LENIENT_UTF_ENCODING)) {
15871594
return _appendReplacementChar(outBuf, outputPtr);
15881595
}
1589-
throw new IllegalArgumentException(String.format(
1596+
_reportError(String.format(
15901597
"Invalid surrogate pair, starts with valid high surrogate (0x%04X)"
15911598
+" but ends with invalid low surrogate (0x%04X), not in valid range [0xDC00, 0xDFFF]",
15921599
surr1, surr2));
1600+
return 0; // never gets here
15931601
}
15941602

15951603
private int _appendReplacementChar(byte[] outBuf, int outputPtr) {

cbor/src/test/java/com/fasterxml/jackson/dataformat/cbor/gen/LenientUnicodeGenerationTest.java renamed to cbor/src/test/java/com/fasterxml/jackson/dataformat/cbor/gen/LenientUnicodeCBORGenerationTest.java

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22

33
import java.io.ByteArrayOutputStream;
44

5+
import com.fasterxml.jackson.core.exc.StreamWriteException;
6+
57
import com.fasterxml.jackson.dataformat.cbor.*;
68

7-
public class LenientUnicodeGenerationTest extends CBORTestBase
9+
public class LenientUnicodeCBORGenerationTest extends CBORTestBase
810
{
911
/**
1012
* Test that encoding a String containing invalid surrogates fail with an exception
@@ -17,29 +19,32 @@ public void testFailForInvalidSurrogate() throws Exception
1719
assertEquals(0, gen.getOutputBuffered());
1820

1921
// Invalid first surrogate character
20-
try {
22+
try {
2123
gen.writeString("x\ud83d");
22-
} catch (IllegalArgumentException e) {
24+
fail("Should not pass");
25+
} catch (StreamWriteException e) {
2326
verifyException(e, "Unmatched surrogate pair");
2427
verifyException(e, "0xD83D");
2528
verifyException(e, "without low surrogate");
2629
}
2730
assertEquals(0, gen.getOutputBuffered());
2831

2932
// Missing second surrogate character
30-
try {
33+
try {
3134
gen.writeString("x\ude01");
32-
} catch (IllegalArgumentException e) {
35+
fail("Should not pass");
36+
} catch (StreamWriteException e) {
3337
verifyException(e, "Invalid surrogate pair");
3438
verifyException(e, "0xDE01");
3539
verifyException(e, "invalid high surrogate");
3640
}
3741
assertEquals(0, gen.getOutputBuffered());
3842

3943
// Invalid second surrogate character (1)
40-
try {
44+
try {
4145
gen.writeString("x\ud801\ud802");
42-
} catch (IllegalArgumentException e) {
46+
fail("Should not pass");
47+
} catch (StreamWriteException e) {
4348
verifyException(e, "Invalid surrogate pair");
4449
verifyException(e, "0xD801");
4550
verifyException(e, "0xD802");
@@ -49,9 +54,10 @@ public void testFailForInvalidSurrogate() throws Exception
4954
assertEquals(0, gen.getOutputBuffered());
5055

5156
// Invalid second surrogate character (2)
52-
try {
57+
try {
5358
gen.writeString("x\ud83dx");
54-
} catch (IllegalArgumentException e) {
59+
fail("Should not pass");
60+
} catch (StreamWriteException e) {
5561
verifyException(e, "Invalid surrogate pair");
5662
verifyException(e, "0xD83D");
5763
verifyException(e, "0x0078");

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
public class GeneratorInvalidCallsTest extends BaseTestForSmile
1010
{
11-
final SmileFactory SMILE_F = new SmileFactory();
11+
private final SmileFactory SMILE_F = new SmileFactory();
1212

1313
public void testInvalidFieldNameInRoot() throws Exception
1414
{

0 commit comments

Comments
 (0)