From edbca3fed85786ef71c5e8b76649b77e55c01508 Mon Sep 17 00:00:00 2001 From: Tyler Gregg Date: Tue, 29 Oct 2024 16:38:55 -0700 Subject: [PATCH] Fixes a bug that caused the binary reader to prematurely assume EOF had been reached in an e-expression. --- .../com/amazon/ion/impl/IonCursorBinary.java | 1 + .../IonReaderContinuableTopLevelBinary.java | 4 +++- .../IonReaderContinuableCoreBinaryTest.java | 18 ++++++++++++++++++ ...onReaderContinuableTopLevelBinaryTest.java | 19 +++++++++++++++++++ 4 files changed, 41 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/amazon/ion/impl/IonCursorBinary.java b/src/main/java/com/amazon/ion/impl/IonCursorBinary.java index faa80f5956..b9fcd4b872 100644 --- a/src/main/java/com/amazon/ion/impl/IonCursorBinary.java +++ b/src/main/java/com/amazon/ion/impl/IonCursorBinary.java @@ -2692,6 +2692,7 @@ void stepIntoEExpression() { pushContainer(); parent.typeId = valueTid; // TODO support length prefixed e-expressions. + // TODO when the length is known to be within the buffer, exit slow mode. parent.endIndex = DELIMITED_MARKER; valueTid = null; event = Event.NEEDS_INSTRUCTION; diff --git a/src/main/java/com/amazon/ion/impl/IonReaderContinuableTopLevelBinary.java b/src/main/java/com/amazon/ion/impl/IonReaderContinuableTopLevelBinary.java index e46118bc0f..5fba999a33 100644 --- a/src/main/java/com/amazon/ion/impl/IonReaderContinuableTopLevelBinary.java +++ b/src/main/java/com/amazon/ion/impl/IonReaderContinuableTopLevelBinary.java @@ -1,6 +1,5 @@ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. // SPDX-License-Identifier: Apache-2.0 - package com.amazon.ion.impl; import com.amazon.ion.IonBufferConfiguration; @@ -219,6 +218,9 @@ void prepareScalar() { if (event == Event.NEEDS_INSTRUCTION) { throw new OversizedValueException(); } + } else { + super.prepareScalar(); + return; } } throw new IonException("Unexpected EOF."); diff --git a/src/test/java/com/amazon/ion/impl/IonReaderContinuableCoreBinaryTest.java b/src/test/java/com/amazon/ion/impl/IonReaderContinuableCoreBinaryTest.java index fd61332745..6de633abf9 100644 --- a/src/test/java/com/amazon/ion/impl/IonReaderContinuableCoreBinaryTest.java +++ b/src/test/java/com/amazon/ion/impl/IonReaderContinuableCoreBinaryTest.java @@ -1079,4 +1079,22 @@ public void taglessCompactSymbols(boolean constructFromBytes) throws Exception { ); } } + + @ParameterizedTest(name = "constructFromBytes={0}") + @ValueSource(booleans = {true, false}) + public void addSymbolsSystemMacro(boolean constructFromBytes) throws Exception { + byte[] data = withIvm(1, bytes( + 0xEF, 0x0C, // system macro add_symbols + 0x02, // AEB: 0b------aa; a=10, expression group + 0x01, // FlexInt 0, a delimited expression group + 0x93, 0x61, 0x62, 0x63, // 3-byte string, utf-8 "abc" + 0xF0, // delimited end... of expression group + 0xE1, // SID single byte + 0x42 // SID $66 + )); + try (IonReaderContinuableCoreBinary reader = initializeReader(constructFromBytes, data)) { + assertEquals(START_SCALAR, reader.nextValue()); + assertEquals(66, reader.symbolValueId()); + } + } } diff --git a/src/test/java/com/amazon/ion/impl/IonReaderContinuableTopLevelBinaryTest.java b/src/test/java/com/amazon/ion/impl/IonReaderContinuableTopLevelBinaryTest.java index 9655eeb602..dca82e1d4e 100644 --- a/src/test/java/com/amazon/ion/impl/IonReaderContinuableTopLevelBinaryTest.java +++ b/src/test/java/com/amazon/ion/impl/IonReaderContinuableTopLevelBinaryTest.java @@ -5985,5 +5985,24 @@ private void invokeValuesUsingSystemMacroOpcodeHelper(boolean constructFromBytes closeAndCount(); } + @ParameterizedTest(name = "constructFromBytes={0}") + @ValueSource(booleans = {true, false}) + public void addSymbolsSystemMacro(boolean constructFromBytes) throws Exception { + int[] data = new int[] { + 0xE0, 0x01, 0x01, 0xEA, // Ion 1.1 IVM + 0xEF, 0x0C, // system macro add_symbols + 0x02, // AEB: 0b------aa; a=10, expression group + 0x01, // FlexInt 0, a delimited expression group + 0x93, 0x61, 0x62, 0x63, // 3-byte string, utf-8 "abc" + 0xF0, // delimited end... of expression group + 0xE1, // SID single byte + 0x42 // SID $66 + }; + try (IonReader reader = readerFor(constructFromBytes,data)) { + assertEquals(IonType.SYMBOL, reader.next()); + assertEquals("abc", reader.stringValue()); + } + } + // TODO Ion 1.1 symbol tables with all kinds of annotation encodings (opcodes E4 - E9, inline and SID) }