diff --git a/avro/src/main/java/com/fasterxml/jackson/dataformat/avro/deser/JacksonAvroParserImpl.java b/avro/src/main/java/com/fasterxml/jackson/dataformat/avro/deser/JacksonAvroParserImpl.java index 89392ff9e..688704814 100644 --- a/avro/src/main/java/com/fasterxml/jackson/dataformat/avro/deser/JacksonAvroParserImpl.java +++ b/avro/src/main/java/com/fasterxml/jackson/dataformat/avro/deser/JacksonAvroParserImpl.java @@ -614,9 +614,8 @@ private final String _finishShortText(int len) throws IOException _inputPtr += len; final byte[] inputBuf = _inputBuffer; - // Let's actually do a tight loop for ASCII first: final int end = inPtr + len; - + // Let's actually do a tight loop for ASCII first: int i; while ((i = inputBuf[inPtr]) >= 0) { outBuf[outPtr++] = (char) i; @@ -626,16 +625,20 @@ private final String _finishShortText(int len) throws IOException } final int[] codes = sUtf8UnitLengths; - do { i = inputBuf[inPtr++] & 0xFF; - if (inPtr >= end) { - throw _constructError("Malformed UTF-8 character at end of short (non-chunked) text segment"); - } - switch (codes[i]) { - case 0: + final int code = codes[i]; + if (code == 0) { // still optimized for ASCII outBuf[outPtr++] = (char) i; - break; + continue; + } + if ((inPtr + code) > end) { + if (code < 4) { + throw _constructError(String.format( + "Malformed %d-byte UTF-8 character at the end of Unicode text block", code)); + } + } + switch (code) { case 1: i = ((i & 0x1F) << 6) | (inputBuf[inPtr++] & 0x3F); break; @@ -655,7 +658,7 @@ private final String _finishShortText(int len) throws IOException i = 0xDC00 | (i & 0x3FF); break; default: // invalid - _reportError("Invalid byte "+Integer.toHexString(i)+" in Unicode text block"); + _reportError(String.format("Invalid byte 0x2X in Unicode text block", i)); } outBuf[outPtr++] = (char) i; } while (inPtr < end); diff --git a/avro/src/test/java/com/fasterxml/jackson/dataformat/avro/fuzz/AvroFuzz449_65618_IOOBETest.java b/avro/src/test/java/com/fasterxml/jackson/dataformat/avro/fuzz/AvroFuzz449_65618_IOOBETest.java index 158129dec..fbcc2df6c 100644 --- a/avro/src/test/java/com/fasterxml/jackson/dataformat/avro/fuzz/AvroFuzz449_65618_IOOBETest.java +++ b/avro/src/test/java/com/fasterxml/jackson/dataformat/avro/fuzz/AvroFuzz449_65618_IOOBETest.java @@ -39,7 +39,7 @@ public void testFuzz65618IOOBE() throws Exception { p.nextToken(); fail("Should not pass (invalid content)"); } catch (StreamReadException e) { - assertTrue(e.getMessage().contains("Malformed UTF-8 character")); + verifyException(e, "Malformed 2-byte UTF-8 character at the end of"); } } }