Skip to content

Commit

Permalink
Improve check to cover all multi-byte encoded characters
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Jan 9, 2024
1 parent 7711289 commit a1f62b1
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
}
}

0 comments on commit a1f62b1

Please sign in to comment.