Skip to content

Commit 7b5172d

Browse files
committed
Some more bounds checking added
1 parent c40bb51 commit 7b5172d

File tree

2 files changed

+33
-10
lines changed

2 files changed

+33
-10
lines changed

src/main/java/com/fasterxml/jackson/core/json/UTF8JsonGenerator.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -670,19 +670,19 @@ public void writeRaw(String text) throws IOException {
670670
@Override
671671
public void writeRaw(String text, int offset, int len) throws IOException
672672
{
673-
final char[] buf = _charBuffer;
674-
673+
final int end = offset+len;
675674
// 03-Aug-2022, tatu: Maybe need to do bounds checks first (found by Fuzzer)
676-
if ((offset < 0) || (len < 0) || (offset+len) > text.length()) {
675+
if ((offset < 0) || (len < 0) || (end > text.length())) {
677676
_reportError(String.format(
678677
"Invalid 'offset' (%d) and/or 'len' (%d) arguments for String of length %d",
679678
offset, len, text.length()));
680679
}
681680

681+
final char[] buf = _charBuffer;
682682
final int cbufLen = buf.length;
683683
// minor optimization: see if we can just get and copy
684684
if (len <= cbufLen) {
685-
text.getChars(offset, offset+len, buf, 0);
685+
text.getChars(offset, end, buf, 0);
686686
writeRaw(buf, 0, len);
687687
return;
688688
}
@@ -745,6 +745,13 @@ public void writeRawValue(SerializableString text) throws IOException {
745745
@Override
746746
public final void writeRaw(char[] cbuf, int offset, int len) throws IOException
747747
{
748+
// 03-Aug-2022, tatu: Maybe need to do bounds checks first (found by Fuzzer)
749+
if ((offset < 0) || (len < 0) || (offset+len) > cbuf.length) {
750+
_reportError(String.format(
751+
"Invalid 'offset' (%d) and/or 'len' (%d) arguments for `char[]` of length %d",
752+
offset, len, cbuf.length));
753+
}
754+
748755
// First: if we have 3 x charCount spaces, we know it'll fit just fine
749756
{
750757
int len3 = len+len+len;

src/main/java/com/fasterxml/jackson/core/json/WriterBasedJsonGenerator.java

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -563,8 +563,17 @@ public void writeRaw(String text) throws IOException
563563
}
564564

565565
@Override
566-
public void writeRaw(String text, int start, int len) throws IOException
566+
public void writeRaw(String text, int offset, int len) throws IOException
567567
{
568+
final int end = offset + len;
569+
570+
// 03-Aug-2022, tatu: Maybe need to do bounds checks first (found by Fuzzer)
571+
if ((offset < 0) || (len < 0) || end > text.length()) {
572+
_reportError(String.format(
573+
"Invalid 'offset' (%d) and/or 'len' (%d) arguments for String of length %d",
574+
offset, len, text.length()));
575+
}
576+
568577
// Nothing to check, can just output as is
569578
int room = _outputEnd - _outputTail;
570579

@@ -574,10 +583,10 @@ public void writeRaw(String text, int start, int len) throws IOException
574583
}
575584
// But would it nicely fit in? If yes, it's easy
576585
if (room >= len) {
577-
text.getChars(start, start+len, _outputBuffer, _outputTail);
586+
text.getChars(offset, end, _outputBuffer, _outputTail);
578587
_outputTail += len;
579588
} else {
580-
writeRawLong(text.substring(start, start+len));
589+
writeRawLong(text.substring(offset, end));
581590
}
582591
}
583592

@@ -593,21 +602,28 @@ public void writeRaw(SerializableString text) throws IOException {
593602
}
594603

595604
@Override
596-
public void writeRaw(char[] text, int offset, int len) throws IOException
605+
public void writeRaw(char[] cbuf, int offset, int len) throws IOException
597606
{
607+
// 03-Aug-2022, tatu: Maybe need to do bounds checks first (found by Fuzzer)
608+
if ((offset < 0) || (len < 0) || (offset+len) > cbuf.length) {
609+
_reportError(String.format(
610+
"Invalid 'offset' (%d) and/or 'len' (%d) arguments for `char[]` of length %d",
611+
offset, len, cbuf.length));
612+
}
613+
598614
// Only worth buffering if it's a short write?
599615
if (len < SHORT_WRITE) {
600616
int room = _outputEnd - _outputTail;
601617
if (len > room) {
602618
_flushBuffer();
603619
}
604-
System.arraycopy(text, offset, _outputBuffer, _outputTail, len);
620+
System.arraycopy(cbuf, offset, _outputBuffer, _outputTail, len);
605621
_outputTail += len;
606622
return;
607623
}
608624
// Otherwise, better just pass through:
609625
_flushBuffer();
610-
_writer.write(text, offset, len);
626+
_writer.write(cbuf, offset, len);
611627
}
612628

613629
@Override

0 commit comments

Comments
 (0)