From 244f2ab66a2c2a3aeaff9fe2d215f98c87e875ed Mon Sep 17 00:00:00 2001 From: Junyan Shi Date: Wed, 2 Dec 2026 01:25:55 +0000 Subject: [PATCH 1/2] Fix triple-quoted clobs ending across pages When the closing quotes of a clob or field name fall on different pages, marking where its text ends stepped back with the wrong sign and at most one page, so valid input threw. Any page end can split them, and streams returning a few bytes per read split them often. Fixes #1187 --- .../ion/impl/UnifiedSavePointManagerX.java | 44 ++++++------ .../ion/impl/IonReaderTextFastPathsTest.java | 72 +++++++++++++++++-- 2 files changed, 88 insertions(+), 28 deletions(-) diff --git a/src/main/java/com/amazon/ion/impl/UnifiedSavePointManagerX.java b/src/main/java/com/amazon/ion/impl/UnifiedSavePointManagerX.java index aa8bddf0de..fe31874117 100644 --- a/src/main/java/com/amazon/ion/impl/UnifiedSavePointManagerX.java +++ b/src/main/java/com/amazon/ion/impl/UnifiedSavePointManagerX.java @@ -1,18 +1,5 @@ -/* - * Copyright 2007-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"). - * You may not use this file except in compliance with the License. - * A copy of the License is located at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * or in the "license" file accompanying this file. This file is distributed - * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either - * express or implied. See the License for the specific language governing - * permissions and limitations under the License. - */ - +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 package com.amazon.ion.impl; @@ -248,11 +235,22 @@ private final void save_point_mark_end(SavePoint sp, int offset) { curr_idx++; curr = _buffer.getPage(curr_idx); } - else if (curr_pos < curr.getStartingOffset()) { - int pos_offset = curr_pos - curr.getOriginalStartingOffset(); - curr_idx--; - curr = _buffer.getPage(curr_idx); - curr_pos = curr.getBufferLimit() - pos_offset; + else { + // The end point can be on an earlier page, even several pages back: each refill takes + // a new page while a save point is open, so a page may hold only a few characters. + // Step back a page at a time, but not before the page the save point starts in. + while (curr_pos < curr.getStartingOffset()) { + if (curr_idx <= sp.getStartIdx()) { + end_point_too_far(curr_idx); + } + // How far the end point is before this page's first character (negative). This + // page starts where the previous one ends, so that is how far it is before the + // previous page's limit. + int pos_offset = curr_pos - curr.getOriginalStartingOffset(); + curr_idx--; + curr = _buffer.getPage(curr_idx); + curr_pos = curr.getBufferLimit() + pos_offset; + } } if (curr == null || curr_pos >= curr.getBufferLimit() || curr_pos < curr.getStartingOffset()) { end_point_too_far(curr_idx); @@ -264,11 +262,11 @@ else if (curr_pos < curr.getStartingOffset()) { return; } private final void end_point_too_far(int curr_idx) { - String message = "end point [" + String message = "end point is outside the pages held for the save point (reached page [" + curr_idx - + "] must be within 1 page of current [" + + "], current page [" + _buffer.getCurrentPageIdx() - + "]"; + + "])"; throw new IllegalArgumentException(message); } diff --git a/src/test/java/com/amazon/ion/impl/IonReaderTextFastPathsTest.java b/src/test/java/com/amazon/ion/impl/IonReaderTextFastPathsTest.java index bb336d46b6..fb966a4d16 100644 --- a/src/test/java/com/amazon/ion/impl/IonReaderTextFastPathsTest.java +++ b/src/test/java/com/amazon/ion/impl/IonReaderTextFastPathsTest.java @@ -90,8 +90,12 @@ public int read(byte[] b, int off, int len) throws IOException { } private static IonDatagram load(Source source, String text) { + return load(source.open(text)); + } + + private static IonDatagram load(IonReader input) { IonDatagram datagram = SYSTEM.newDatagram(); - try (IonReader reader = source.open(text)) { + try (IonReader reader = input) { IonWriter writer = SYSTEM.newWriter(datagram); writer.writeValues(reader); writer.close(); @@ -152,6 +156,67 @@ public void blobsDecodeTheSameWithWhitespaceAndPadding(Source source) { } } + /** + * Triple-quoted clobs and field names read the same wherever pages end around their closing quotes. After the + * closing quotes, the reader marks the end of the text at the first of them, which can be on an earlier page, or + * several pages back when each read returns a few bytes. A malformed one throws the same IonException as before. + * See: https://github.com/amazon-ion/ion-java/issues/1187 + */ + @Test + public void tripleQuotedTextReadsTheSameWhereverPagesEnd() throws IOException { + String[] texts = {"{{'''abc'''}}", "{{''''''}}", "{{'''a''b''' '''c'''}} 7", "{a:{{'''abc'''}}, b:[{{'''d'''}}]}", + "{'''abc''':1}"}; + for (String text : texts) { + byte[] bytes = text.getBytes(StandardCharsets.UTF_8); + IonDatagram expected = load(Source.BYTES, text); + for (int max = 1; max <= bytes.length; max++) { + assertEquals(expected, load(SYSTEM.newReader(new LimitedReads(bytes, max))), text + " read " + max + " bytes at a time"); + } + } + for (String text : new String[] {"{{'''abc''", "{'''abc''"}) { + byte[] bytes = text.getBytes(StandardCharsets.UTF_8); + Class expected = assertThrows(IonException.class, () -> load(Source.BYTES, text), text).getClass(); + for (int max = 1; max <= bytes.length; max++) { + LimitedReads input = new LimitedReads(bytes, max); + String label = text + " read " + max + " bytes at a time"; + assertEquals(expected, assertThrows(IonException.class, () -> load(SYSTEM.newReader(input)), label).getClass(), label); + } + } + byte[] bytes = "{{'''abc'''}}".getBytes(StandardCharsets.UTF_8); + byte[] abc = "abc".getBytes(StandardCharsets.US_ASCII); + for (int max = 1; max <= bytes.length; max++) { + String label = "{{'''abc'''}} read " + max + " bytes at a time"; + try (IonReader reader = SYSTEM.newReader(new LimitedReads(bytes, max))) { + assertEquals(IonType.CLOB, reader.next(), label); + assertArrayEquals(abc, reader.newBytes(), label); + } + try (IonReader reader = SYSTEM.newReader(new LimitedReads(bytes, max))) { + assertEquals(IonType.CLOB, reader.next(), label); + assertEquals(abc.length, reader.byteSize(), label); + } + try (IonReader reader = SYSTEM.newReader(new LimitedReads(bytes, max))) { + assertEquals(IonType.CLOB, reader.next(), label); + byte[] buffer = new byte[abc.length]; + assertEquals(abc.length, reader.getBytes(buffer, 0, buffer.length), label); + assertArrayEquals(abc, buffer, label); + } + } + } + + /** The same when the closing quotes of a clob longer than a page straddle the end of a full page. */ + @ParameterizedTest + @EnumSource(value = Source.class, names = "STREAM_1_BYTE_READS", mode = EnumSource.Mode.EXCLUDE) + public void tripleQuotedClobsEndingAcrossAPageEnd(Source source) throws IOException { + for (int length = PAGE - 40; length <= PAGE; length++) { + String content = repeat("x", length); + try (IonReader reader = source.open("{{'''" + content + "'''}} 7")) { + assertEquals(IonType.CLOB, reader.next(), "length " + length); + assertArrayEquals(content.getBytes(StandardCharsets.US_ASCII), reader.newBytes(), "length " + length); + assertEquals(IonType.INT, reader.next(), "length " + length); + } + } + } + @ParameterizedTest @EnumSource(value = Source.class, names = "STREAM_1_BYTE_READS", mode = EnumSource.Mode.EXCLUDE) public void skippedValuesLeaveTheReaderOnTheNextValue(Source source) { @@ -334,12 +399,9 @@ public void nonAsciiInBase64IsRejectedAsBefore(Source source) { @ParameterizedTest @EnumSource(Source.class) public void malformedValuesLeaveTheReaderUsable(Source source) throws IOException { - List lobs = new ArrayList<>(Arrays.asList("{{invalid_blob}} 7", "{{\"x\\q\"}} 7")); + List lobs = new ArrayList<>(Arrays.asList("{{invalid_blob}} 7", "{{\"x\\q\"}} 7", "{{'''x\\q'''}} 7")); List fieldNames = new ArrayList<>(Arrays.asList("{'x\\q':1, b:2} 7", "{\"x\\q\":1, b:2} 7")); if (source != Source.STREAM_1_BYTE_READS) { - // A triple-quoted clob read a byte at a time fails even when it is valid. - // See: https://github.com/amazon-ion/ion-java/issues/1187 - lobs.add("{{'''x\\q'''}} 7"); // Values longer than a page. lobs.add("{{" + repeat("A", 2 * PAGE) + "_}} 7"); lobs.add("{{\"" + repeat("x", 2 * PAGE) + "\\q\"}} 7"); From 5563b51e74edf7e1e8447be21ed9495ab36ff285 Mon Sep 17 00:00:00 2001 From: Junyan Shi Date: Wed, 2 Dec 2026 06:13:39 +0000 Subject: [PATCH 2/2] Split the triple-quoted text test So each input and read size runs as its own case, and a failure names the input and read size instead of stopping the whole test. Addresses review comment on #1188 --- .../ion/impl/IonReaderTextFastPathsTest.java | 103 +++++++++++------- 1 file changed, 64 insertions(+), 39 deletions(-) diff --git a/src/test/java/com/amazon/ion/impl/IonReaderTextFastPathsTest.java b/src/test/java/com/amazon/ion/impl/IonReaderTextFastPathsTest.java index fb966a4d16..b051203ad3 100644 --- a/src/test/java/com/amazon/ion/impl/IonReaderTextFastPathsTest.java +++ b/src/test/java/com/amazon/ion/impl/IonReaderTextFastPathsTest.java @@ -21,7 +21,9 @@ import org.junit.jupiter.api.Test; import org.junit.jupiter.api.function.Executable; import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.EnumSource; +import org.junit.jupiter.params.provider.MethodSource; import java.io.ByteArrayInputStream; import java.io.FilterInputStream; @@ -33,6 +35,8 @@ import java.util.Base64; import java.util.List; import java.util.Random; +import java.util.stream.IntStream; +import java.util.stream.Stream; import static org.junit.jupiter.api.Assertions.assertArrayEquals; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -156,54 +160,75 @@ public void blobsDecodeTheSameWithWhitespaceAndPadding(Source source) { } } + /** Each text with every number of bytes per read, from 1 to the text's length. */ + private static Stream everyReadSize(String... texts) { + return Arrays.stream(texts).flatMap(text -> IntStream.rangeClosed(1, text.getBytes(StandardCharsets.UTF_8).length) + .mapToObj(bytesPerRead -> Arguments.of(text, bytesPerRead))); + } + + private static IonReader inReadsOf(int bytesPerRead, String text) { + return SYSTEM.newReader(new LimitedReads(text.getBytes(StandardCharsets.UTF_8), bytesPerRead)); + } + + private static Stream tripleQuotedText() { + return everyReadSize("{{'''abc'''}}", "{{''''''}}", "{{'''a''b''' '''c'''}} 7", "{a:{{'''abc'''}}, b:[{{'''d'''}}]}", + "{'''abc''':1}"); + } + /** * Triple-quoted clobs and field names read the same wherever pages end around their closing quotes. After the * closing quotes, the reader marks the end of the text at the first of them, which can be on an earlier page, or - * several pages back when each read returns a few bytes. A malformed one throws the same IonException as before. - * See: https://github.com/amazon-ion/ion-java/issues/1187 + * several pages back when each read returns a few bytes. See: https://github.com/amazon-ion/ion-java/issues/1187 */ - @Test - public void tripleQuotedTextReadsTheSameWhereverPagesEnd() throws IOException { - String[] texts = {"{{'''abc'''}}", "{{''''''}}", "{{'''a''b''' '''c'''}} 7", "{a:{{'''abc'''}}, b:[{{'''d'''}}]}", - "{'''abc''':1}"}; - for (String text : texts) { - byte[] bytes = text.getBytes(StandardCharsets.UTF_8); - IonDatagram expected = load(Source.BYTES, text); - for (int max = 1; max <= bytes.length; max++) { - assertEquals(expected, load(SYSTEM.newReader(new LimitedReads(bytes, max))), text + " read " + max + " bytes at a time"); - } + @ParameterizedTest(name = "{0} read {1} bytes at a time") + @MethodSource("tripleQuotedText") + public void tripleQuotedTextReadsTheSameWhereverPagesEnd(String text, int bytesPerRead) { + assertEquals(load(Source.BYTES, text), load(inReadsOf(bytesPerRead, text))); + } + + private static Stream malformedTripleQuotedText() { + return everyReadSize("{{'''abc''", "{'''abc''"); + } + + /** Unterminated triple-quoted text throws the same IonException wherever pages end. */ + @ParameterizedTest(name = "{0} read {1} bytes at a time") + @MethodSource("malformedTripleQuotedText") + public void malformedTripleQuotedTextThrowsTheSameWhereverPagesEnd(String text, int bytesPerRead) { + IonException expected = assertThrows(IonException.class, () -> load(Source.BYTES, text)); + IonException actual = assertThrows(IonException.class, () -> load(inReadsOf(bytesPerRead, text))); + assertEquals(expected.getClass(), actual.getClass()); + } + + private static Stream tripleQuotedClob() { + return everyReadSize("{{'''abc'''}}"); + } + + /** newBytes(), byteSize() and getBytes() return a triple-quoted clob's bytes wherever pages end. */ + @ParameterizedTest(name = "{0} read {1} bytes at a time") + @MethodSource("tripleQuotedClob") + public void tripleQuotedClobBytesWhereverPagesEnd(String text, int bytesPerRead) throws IOException { + byte[] expected; + try (IonReader reader = Source.BYTES.open(text)) { + assertEquals(IonType.CLOB, reader.next()); + expected = reader.newBytes(); } - for (String text : new String[] {"{{'''abc''", "{'''abc''"}) { - byte[] bytes = text.getBytes(StandardCharsets.UTF_8); - Class expected = assertThrows(IonException.class, () -> load(Source.BYTES, text), text).getClass(); - for (int max = 1; max <= bytes.length; max++) { - LimitedReads input = new LimitedReads(bytes, max); - String label = text + " read " + max + " bytes at a time"; - assertEquals(expected, assertThrows(IonException.class, () -> load(SYSTEM.newReader(input)), label).getClass(), label); - } + try (IonReader reader = inReadsOf(bytesPerRead, text)) { + assertEquals(IonType.CLOB, reader.next()); + assertArrayEquals(expected, reader.newBytes()); } - byte[] bytes = "{{'''abc'''}}".getBytes(StandardCharsets.UTF_8); - byte[] abc = "abc".getBytes(StandardCharsets.US_ASCII); - for (int max = 1; max <= bytes.length; max++) { - String label = "{{'''abc'''}} read " + max + " bytes at a time"; - try (IonReader reader = SYSTEM.newReader(new LimitedReads(bytes, max))) { - assertEquals(IonType.CLOB, reader.next(), label); - assertArrayEquals(abc, reader.newBytes(), label); - } - try (IonReader reader = SYSTEM.newReader(new LimitedReads(bytes, max))) { - assertEquals(IonType.CLOB, reader.next(), label); - assertEquals(abc.length, reader.byteSize(), label); - } - try (IonReader reader = SYSTEM.newReader(new LimitedReads(bytes, max))) { - assertEquals(IonType.CLOB, reader.next(), label); - byte[] buffer = new byte[abc.length]; - assertEquals(abc.length, reader.getBytes(buffer, 0, buffer.length), label); - assertArrayEquals(abc, buffer, label); - } + try (IonReader reader = inReadsOf(bytesPerRead, text)) { + assertEquals(IonType.CLOB, reader.next()); + assertEquals(expected.length, reader.byteSize()); + } + try (IonReader reader = inReadsOf(bytesPerRead, text)) { + assertEquals(IonType.CLOB, reader.next()); + byte[] buffer = new byte[expected.length]; + assertEquals(expected.length, reader.getBytes(buffer, 0, buffer.length)); + assertArrayEquals(expected, buffer); } } - /** The same when the closing quotes of a clob longer than a page straddle the end of a full page. */ + /** Triple-quoted clobs longer than a page read the same when their closing quotes straddle the end of a page. */ @ParameterizedTest @EnumSource(value = Source.class, names = "STREAM_1_BYTE_READS", mode = EnumSource.Mode.EXCLUDE) public void tripleQuotedClobsEndingAcrossAPageEnd(Source source) throws IOException {