Skip to content

Commit

Permalink
Ensures that encountering an incomplete binary IVM causes clean failure.
Browse files Browse the repository at this point in the history
  • Loading branch information
tgregg committed May 2, 2024
1 parent 89d61a7 commit 178c010
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/main/java/com/amazon/ion/impl/IonCursorBinary.java
Original file line number Diff line number Diff line change
Expand Up @@ -1113,6 +1113,9 @@ private void reset() {
* point to the first byte after the IVM.
*/
private void readIvm() {
if (limit < peekIndex + IVM_REMAINING_LENGTH) {
throw new IonException("Incomplete Ion version marker.");
}
majorVersion = buffer[(int) (peekIndex++)];
minorVersion = buffer[(int) (peekIndex++)];
if ((buffer[(int) (peekIndex++)] & SINGLE_BYTE_MASK) != IVM_FINAL_BYTE) {
Expand Down
22 changes: 22 additions & 0 deletions src/test/java/com/amazon/ion/impl/IonCursorBinaryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import java.util.function.Consumer;

import static com.amazon.ion.BitUtils.bytes;
import static com.amazon.ion.IonCursor.Event.NEEDS_DATA;
import static com.amazon.ion.IonCursor.Event.VALUE_READY;
import static com.amazon.ion.IonCursor.Event.START_CONTAINER;
import static com.amazon.ion.IonCursor.Event.START_SCALAR;
Expand Down Expand Up @@ -445,4 +446,25 @@ public void expectUseAfterCloseToHaveNoEffect(boolean constructFromBytes) {
assertNull(cursor.getValueMarker().typeId);
cursor.close();
}

@ParameterizedTest(name = "constructFromBytes={0}")
@ValueSource(booleans = {true, false})
public void expectIncompleteIvmToFailCleanly(boolean constructFromBytes) {
IonCursorBinary cursor = initializeCursor(
constructFromBytes,
0xE0, 0x01, 0x00, 0xEA, // Complete IVM
0x20, // Int 0
0xE0, 0x01 // Incomplete IVM
);
assertEquals(START_SCALAR, cursor.nextValue());
if (constructFromBytes) {
// This is a fixed stream, so no more bytes will become available. An error must be raised when the
// incomplete IVM is encountered.
assertThrows(IonException.class, cursor::nextValue);
} else {
// This is a growing stream, so the cursor waits for more bytes.
assertEquals(NEEDS_DATA, cursor.nextValue());
}
cursor.close();
}
}

0 comments on commit 178c010

Please sign in to comment.