Skip to content

Commit 08af8cb

Browse files
committed
Fixes a bug that caused the binary reader not to fail cleanly when parsing incomplete containers in certain cases.
1 parent 8dbd001 commit 08af8cb

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

src/main/java/com/amazon/ion/impl/IonCursorBinary.java

+3
Original file line numberDiff line numberDiff line change
@@ -1574,6 +1574,9 @@ private boolean uncheckedNextToken() {
15741574
if (uncheckedNextContainedToken()) {
15751575
return false;
15761576
}
1577+
if (peekIndex >= limit) {
1578+
throw new IonException("Malformed data: declared length exceeds the number of bytes remaining in the stream.");
1579+
}
15771580
b = buffer[(int)(peekIndex++)] & SINGLE_BYTE_MASK;
15781581
}
15791582
if (uncheckedReadHeader(b, false, valueMarker)) {

src/test/java/com/amazon/ion/impl/IonReaderContinuableCoreBinaryTest.java

+53
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
package com.amazon.ion.impl;
55

6+
import com.amazon.ion.IonCursor;
67
import com.amazon.ion.IonException;
78
import com.amazon.ion.IonType;
89
import org.junit.jupiter.api.Test;
@@ -496,4 +497,56 @@ public void expectLobWithOverflowingEndIndexToFailCleanly(boolean constructFromB
496497
assertThrows(IonException.class, reader::nextValue);
497498
reader.close();
498499
}
500+
501+
@Test
502+
public void expectIncompleteContainerToFailCleanlyAfterFieldSid() {
503+
IonReaderContinuableCoreBinary reader = initializeReader(
504+
true,
505+
0xE0, 0x01, 0x00, 0xEA, // IVM
506+
0xDC, // Struct, length 12
507+
0x9A // Field SID 26
508+
// The struct ends unexpectedly
509+
);
510+
assertEquals(IonCursor.Event.START_CONTAINER, reader.nextValue());
511+
assertEquals(IonType.STRUCT, reader.getType());
512+
reader.stepIntoContainer();
513+
// This is an unexpected EOF, so the reader should fail cleanly.
514+
assertThrows(IonException.class, reader::nextValue);
515+
reader.close();
516+
}
517+
518+
@Test
519+
public void expectIncompleteContainerToFailCleanlyAfterAnnotationHeader() {
520+
IonReaderContinuableCoreBinary reader = initializeReader(
521+
true,
522+
0xE0, 0x01, 0x00, 0xEA, // IVM
523+
0xDC, // Struct, length 12
524+
0x9A, // Field SID 26
525+
0xE4, // Annotation wrapper length 4
526+
0x00, 0x81, // VarUInt length 1 (overpadded by 1 byte)
527+
0x00, 0x84 // VarUInt SID 4 (overpadded by 1 byte)
528+
// The value ends unexpectedly
529+
);
530+
assertEquals(IonCursor.Event.START_CONTAINER, reader.nextValue());
531+
assertEquals(IonType.STRUCT, reader.getType());
532+
reader.stepIntoContainer();
533+
// This is an unexpected EOF, so the reader should fail cleanly.
534+
assertThrows(IonException.class, reader::nextValue);
535+
reader.close();
536+
}
537+
538+
@Test
539+
public void expectIncompleteAnnotationHeaderToFailCleanly() {
540+
IonReaderContinuableCoreBinary reader = initializeReader(
541+
true,
542+
0xE0, 0x01, 0x00, 0xEA, // IVM
543+
0xE4, // Annotation wrapper length 5
544+
0x81, // VarUInt length 1
545+
0x84 // VarUInt SID 4
546+
// The value ends unexpectedly
547+
);
548+
// This is an unexpected EOF, so the reader should fail cleanly.
549+
assertThrows(IonException.class, reader::nextValue);
550+
reader.close();
551+
}
499552
}

0 commit comments

Comments
 (0)