From a4bf7167b00aed4834c8b0b0a60e51dd00ac9319 Mon Sep 17 00:00:00 2001 From: Joshua Barr Date: Mon, 29 Jan 2024 16:01:50 -0800 Subject: [PATCH] Ensure that a modeled exception is thrown --- .../com/amazon/ion/impl/IonCursorBinary.java | 1 + .../amazon/ion/impl/IonCursorBinaryTest.java | 30 +++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/src/main/java/com/amazon/ion/impl/IonCursorBinary.java b/src/main/java/com/amazon/ion/impl/IonCursorBinary.java index 43e64748e3..9cb9c91225 100644 --- a/src/main/java/com/amazon/ion/impl/IonCursorBinary.java +++ b/src/main/java/com/amazon/ion/impl/IonCursorBinary.java @@ -458,6 +458,7 @@ private static IonBufferConfiguration getFixedSizeConfigurationFor( configuration.getInitialBufferSize(), configuration.getMaximumBufferSize() ); + registerOversizedValueHandler(configuration.getOversizedValueHandler()); } /* diff --git a/src/test/java/com/amazon/ion/impl/IonCursorBinaryTest.java b/src/test/java/com/amazon/ion/impl/IonCursorBinaryTest.java index b28e9bd5f9..734d152b57 100644 --- a/src/test/java/com/amazon/ion/impl/IonCursorBinaryTest.java +++ b/src/test/java/com/amazon/ion/impl/IonCursorBinaryTest.java @@ -3,6 +3,7 @@ package com.amazon.ion.impl; +import com.amazon.ion.IonBufferConfiguration; import com.amazon.ion.IonCursor; import com.amazon.ion.IonException; import org.junit.jupiter.api.Test; @@ -27,6 +28,8 @@ import static com.amazon.ion.impl.IonCursorTestUtilities.endStream; import static com.amazon.ion.impl.IonCursorTestUtilities.scalar; import static com.amazon.ion.impl.IonCursorTestUtilities.startContainer; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.containsString; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows; @@ -397,4 +400,31 @@ public void expectMissingAnnotationWrapperAnnotationLengthToFailCleanly() { assertThrows(IonException.class, cursor::nextValue); cursor.close(); } + + @Test + public void expectValueLargerThanIntMaxToFailCleanly() { + int[] data = new int[] { + 0xE0, 0x01, 0x00, 0xEA, + 0x2E, // Integer with VarUInt length, because that's clearly a reasonable thing to find + 0x07, 0x7f, 0x7f, 0x7f, 0xf9 // VarUInt length 2147483647 (Integer.MAX_LENGTH) + }; + ByteArrayInputStream in = new ByteArrayInputStream(bytes(data)); + + // We need a custom initial buffer size so that the cursor doesn't know there are fewer bytes remaining + // than the value header promises + IonBufferConfiguration config = IonBufferConfiguration.Builder.standard() + .withInitialBufferSize(8) + .build(); + + IonCursorBinary cursor = new IonCursorBinary(config, in, null, 0, 0); + + cursor.nextValue(); // Position cursor on unreal oversized value + + // Try to get all the value bytes, and fail because arrays can't be that large + IonException ie = assertThrows(IonException.class, cursor::fillValue); + assertThat(ie.getMessage(), + containsString("An oversized value was found even though no maximum size was configured.")); + + cursor.close(); + } }