Skip to content

Commit bc59dcc

Browse files
committed
Further improvements for #811
1 parent feee05c commit bc59dcc

File tree

4 files changed

+48
-20
lines changed

4 files changed

+48
-20
lines changed

src/main/java/com/fasterxml/jackson/core/base/GeneratorBase.java

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -505,9 +505,13 @@ protected final int _decodeSurrogate(int surr1, int surr2) throws IOException
505505
*/
506506

507507
// @since 2.14
508-
protected void _checkRangeBoundsForByteArray(int offset, int len, int dataLen)
508+
protected void _checkRangeBoundsForByteArray(byte[] data, int offset, int len)
509509
throws IOException
510510
{
511+
if (data == null) {
512+
_reportError("Invalid `byte[]` argument: `null`");
513+
}
514+
final int dataLen = data.length;
511515
final int end = offset+len;
512516

513517
// Note: we are checking that:
@@ -527,9 +531,13 @@ protected void _checkRangeBoundsForByteArray(int offset, int len, int dataLen)
527531
}
528532

529533
// @since 2.14
530-
protected void _checkRangeBoundsForCharArray(int offset, int len, int dataLen)
534+
protected void _checkRangeBoundsForCharArray(char[] data, int offset, int len)
531535
throws IOException
532536
{
537+
if (data == null) {
538+
_reportError("Invalid `char[]` argument: `null`");
539+
}
540+
final int dataLen = data.length;
533541
final int end = offset+len;
534542
// Note: we are checking same things as with other bounds-checks
535543
int anyNegs = offset | len | end | (dataLen - end);
@@ -541,9 +549,13 @@ protected void _checkRangeBoundsForCharArray(int offset, int len, int dataLen)
541549
}
542550

543551
// @since 2.14
544-
protected void _checkRangeBoundsForString(int offset, int len, int dataLen)
552+
protected void _checkRangeBoundsForString(String data, int offset, int len)
545553
throws IOException
546554
{
555+
if (data == null) {
556+
_reportError("Invalid `String` argument: `null`");
557+
}
558+
final int dataLen = data.length();
547559
final int end = offset+len;
548560
// Note: we are checking same things as with other bounds-checks
549561
int anyNegs = offset | len | end | (dataLen - end);

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -617,7 +617,7 @@ public final void writeString(SerializableString text) throws IOException
617617
@Override
618618
public void writeRawUTF8String(byte[] text, int offset, int len) throws IOException
619619
{
620-
_checkRangeBoundsForByteArray(offset, len, text.length);
620+
_checkRangeBoundsForByteArray(text, offset, len);
621621
_verifyValueWrite(WRITE_STRING);
622622
if (_outputTail >= _outputEnd) {
623623
_flushBuffer();
@@ -633,7 +633,7 @@ public void writeRawUTF8String(byte[] text, int offset, int len) throws IOExcept
633633
@Override
634634
public void writeUTF8String(byte[] text, int offset, int len) throws IOException
635635
{
636-
_checkRangeBoundsForByteArray(offset, len, text.length);
636+
_checkRangeBoundsForByteArray(text, offset, len);
637637
_verifyValueWrite(WRITE_STRING);
638638
if (_outputTail >= _outputEnd) {
639639
_flushBuffer();
@@ -672,7 +672,7 @@ public void writeRaw(String text) throws IOException {
672672
@Override
673673
public void writeRaw(String text, int offset, int len) throws IOException
674674
{
675-
_checkRangeBoundsForString(offset, len, text.length());
675+
_checkRangeBoundsForString(text, offset, len);
676676

677677
final char[] buf = _charBuffer;
678678
final int cbufLen = buf.length;
@@ -741,7 +741,7 @@ public void writeRawValue(SerializableString text) throws IOException {
741741
@Override
742742
public final void writeRaw(char[] cbuf, int offset, int len) throws IOException
743743
{
744-
_checkRangeBoundsForCharArray(offset, len, cbuf.length);
744+
_checkRangeBoundsForCharArray(cbuf, offset, len);
745745

746746
// First: if we have 3 x charCount spaces, we know it'll fit just fine
747747
{
@@ -884,7 +884,7 @@ public void writeBinary(Base64Variant b64variant,
884884
byte[] data, int offset, int len)
885885
throws IOException, JsonGenerationException
886886
{
887-
_checkRangeBoundsForByteArray(offset, len, data.length);
887+
_checkRangeBoundsForByteArray(data, offset, len);
888888

889889
_verifyValueWrite(WRITE_BINARY);
890890
// Starting quotes

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -565,7 +565,7 @@ public void writeRaw(String text) throws IOException
565565
@Override
566566
public void writeRaw(String text, int offset, int len) throws IOException
567567
{
568-
_checkRangeBoundsForString(offset, len, text.length());
568+
_checkRangeBoundsForString(text, offset, len);
569569

570570
// Nothing to check, can just output as is
571571
int room = _outputEnd - _outputTail;
@@ -597,7 +597,7 @@ public void writeRaw(SerializableString text) throws IOException {
597597
@Override
598598
public void writeRaw(char[] cbuf, int offset, int len) throws IOException
599599
{
600-
_checkRangeBoundsForCharArray(offset, len, cbuf.length);
600+
_checkRangeBoundsForCharArray(cbuf, offset, len);
601601

602602
// Only worth buffering if it's a short write?
603603
if (len < SHORT_WRITE) {
@@ -658,7 +658,7 @@ private void writeRawLong(String text) throws IOException
658658
public void writeBinary(Base64Variant b64variant, byte[] data, int offset, int len)
659659
throws IOException, JsonGenerationException
660660
{
661-
_checkRangeBoundsForByteArray(offset, len, data.length);
661+
_checkRangeBoundsForByteArray(data, offset, len);
662662

663663
_verifyValueWrite(WRITE_BINARY);
664664
// Starting quotes

src/test/java/com/fasterxml/jackson/core/write/GeneratorBoundsChecksTest.java

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,8 @@ private void _testBoundsWithByteArrayInput(GeneratorCreator genc,
9898
// and the integer overflow, too
9999
_testBoundsWithByteArrayInput(genc, oper, BYTES10, Integer.MAX_VALUE, 4);
100100
_testBoundsWithByteArrayInput(genc, oper, BYTES10, Integer.MAX_VALUE, Integer.MAX_VALUE);
101+
// and null checks too
102+
_testBoundsWithByteArrayInput(genc, oper, null, 0, 3);
101103
}
102104

103105
private void _testBoundsWithByteArrayInput(GeneratorCreator genc,
@@ -108,9 +110,13 @@ private void _testBoundsWithByteArrayInput(GeneratorCreator genc,
108110
try {
109111
oper.call(gen, data, offset, len);
110112
} catch (StreamWriteException e) {
111-
verifyException(e, "Invalid 'offset'");
112-
verifyException(e, "'len'");
113-
verifyException(e, "arguments for `byte[]` of length "+data.length);
113+
if (data == null) {
114+
verifyException(e, "Invalid `byte[]` argument: `null`");
115+
} else {
116+
verifyException(e, "Invalid 'offset'");
117+
verifyException(e, "'len'");
118+
verifyException(e, "arguments for `byte[]` of length "+data.length);
119+
}
114120
}
115121
}
116122
}
@@ -167,6 +173,7 @@ private void _testBoundsWithCharArrayInput(GeneratorCreator genc,
167173
_testBoundsWithCharArrayInput(genc, oper, CHARS10, 9, 5);
168174
_testBoundsWithCharArrayInput(genc, oper, CHARS10, Integer.MAX_VALUE, 4);
169175
_testBoundsWithCharArrayInput(genc, oper, CHARS10, Integer.MAX_VALUE, Integer.MAX_VALUE);
176+
_testBoundsWithCharArrayInput(genc, oper, null, 0, 3);
170177
}
171178

172179
private void _testBoundsWithCharArrayInput(GeneratorCreator genc,
@@ -177,9 +184,13 @@ private void _testBoundsWithCharArrayInput(GeneratorCreator genc,
177184
try {
178185
oper.call(gen, data, offset, len);
179186
} catch (StreamWriteException e) {
180-
verifyException(e, "Invalid 'offset'");
181-
verifyException(e, "'len'");
182-
verifyException(e, "arguments for `char[]` of length "+data.length);
187+
if (data == null) {
188+
verifyException(e, "Invalid `char[]` argument: `null`");
189+
} else {
190+
verifyException(e, "Invalid 'offset'");
191+
verifyException(e, "'len'");
192+
verifyException(e, "arguments for `char[]` of length "+data.length);
193+
}
183194
}
184195
}
185196
}
@@ -230,6 +241,7 @@ private void _testBoundsWithStringInput(GeneratorCreator genc,
230241
_testBoundsWithStringInput(genc, oper, STRING10, 9, 5);
231242
_testBoundsWithStringInput(genc, oper, STRING10, Integer.MAX_VALUE, 4);
232243
_testBoundsWithStringInput(genc, oper, STRING10, Integer.MAX_VALUE, Integer.MAX_VALUE);
244+
_testBoundsWithStringInput(genc, oper, null, 0, 3);
233245
}
234246

235247
private void _testBoundsWithStringInput(GeneratorCreator genc,
@@ -240,9 +252,13 @@ private void _testBoundsWithStringInput(GeneratorCreator genc,
240252
try {
241253
oper.call(gen, data, offset, len);
242254
} catch (StreamWriteException e) {
243-
verifyException(e, "Invalid 'offset'");
244-
verifyException(e, "'len'");
245-
verifyException(e, "arguments for `String` of length "+data.length());
255+
if (data == null) {
256+
verifyException(e, "Invalid `String` argument: `null`");
257+
} else {
258+
verifyException(e, "Invalid 'offset'");
259+
verifyException(e, "'len'");
260+
verifyException(e, "arguments for `String` of length "+data.length());
261+
}
246262
}
247263
}
248264
}

0 commit comments

Comments
 (0)