Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 34 additions & 28 deletions src/main/java/com/amazon/ion/impl/IonReaderTextRawX.java
Original file line number Diff line number Diff line change
Expand Up @@ -1085,35 +1085,41 @@ else if (_current_value_save_point_loaded) {
// _scanner.load_save_point_contents( _current_value_save_point, sb);

_scanner.save_point_activate(_current_value_save_point);
switch (token_type) {
default:
_scanner.load_raw_characters(sb);
break;
case IonTokenConstsX.TOKEN_SYMBOL_IDENTIFIER:
_scanner.load_symbol_identifier(sb);
_value_type = IonType.SYMBOL;
break;
case IonTokenConstsX.TOKEN_SYMBOL_OPERATOR:
_scanner.load_symbol_operator(sb);
_value_type = IonType.SYMBOL;
break;
case IonTokenConstsX.TOKEN_SYMBOL_QUOTED:
clob_chars_only = (IonType.CLOB == _value_type);
_scanner.load_single_quoted_string(sb, clob_chars_only);
_value_type = IonType.SYMBOL;
break;
case IonTokenConstsX.TOKEN_STRING_DOUBLE_QUOTE:
clob_chars_only = (IonType.CLOB == _value_type);
_scanner.load_double_quoted_string(sb, clob_chars_only);
_value_type = IonType.STRING;
break;
case IonTokenConstsX.TOKEN_STRING_TRIPLE_QUOTE:
clob_chars_only = (IonType.CLOB == _value_type);
_scanner.load_triple_quoted_string(sb, clob_chars_only);
_value_type = IonType.STRING;
break;
try {
switch (token_type) {
default:
_scanner.load_raw_characters(sb);
break;
case IonTokenConstsX.TOKEN_SYMBOL_IDENTIFIER:
_scanner.load_symbol_identifier(sb);
_value_type = IonType.SYMBOL;
break;
case IonTokenConstsX.TOKEN_SYMBOL_OPERATOR:
_scanner.load_symbol_operator(sb);
_value_type = IonType.SYMBOL;
break;
case IonTokenConstsX.TOKEN_SYMBOL_QUOTED:
clob_chars_only = (IonType.CLOB == _value_type);
_scanner.load_single_quoted_string(sb, clob_chars_only);
_value_type = IonType.SYMBOL;
break;
case IonTokenConstsX.TOKEN_STRING_DOUBLE_QUOTE:
clob_chars_only = (IonType.CLOB == _value_type);
_scanner.load_double_quoted_string(sb, clob_chars_only);
_value_type = IonType.STRING;
break;
case IonTokenConstsX.TOKEN_STRING_TRIPLE_QUOTE:
clob_chars_only = (IonType.CLOB == _value_type);
_scanner.load_triple_quoted_string(sb, clob_chars_only);
_value_type = IonType.STRING;
break;
}
}
finally {
// Deactivate even if the read throws: an active save point keeps the
// stream inside the value's text and cannot be cleared.
_scanner.save_point_deactivate(_current_value_save_point);
}
_scanner.save_point_deactivate(_current_value_save_point);
_current_value_buffer_loaded = true;
}
else {
Expand Down
10 changes: 8 additions & 2 deletions src/main/java/com/amazon/ion/impl/IonReaderTextSystemX.java
Original file line number Diff line number Diff line change
Expand Up @@ -744,8 +744,14 @@ private int load_lob_contents() throws IOException
try {
assert(_current_value_save_point_loaded && _current_value_save_point.isDefined());
_scanner.save_point_activate(_current_value_save_point);
_lob_actual_len = readBytes(_lob_bytes, 0, (int)raw_size);
_scanner.save_point_deactivate(_current_value_save_point);
try {
_lob_actual_len = readBytes(_lob_bytes, 0, (int)raw_size);
}
finally {
// Deactivate even if the read throws: an active save point keeps the
// stream inside the value's text and cannot be cleared.
_scanner.save_point_deactivate(_current_value_save_point);
}
}
catch (IOException e) {
throw new IonException(e);
Expand Down
30 changes: 15 additions & 15 deletions src/test/java/com/amazon/ion/BadIonTest.java
Original file line number Diff line number Diff line change
@@ -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;

import static com.amazon.ion.TestUtils.BAD_IONTESTS_FILES;
Expand Down Expand Up @@ -64,6 +51,19 @@ public void testLoadFile()
}


/**
* load(byte[]) closes its reader after the failure, unlike load(File) and load(String); closing must not
* replace the IonException.
*/
@Test
public void testLoadBytes()
throws Exception
{
byte[] bytes = _Private_Utils.loadFileBytes(myTestFile);
assertThrows(IonException.class, () -> loader().load(bytes));
}


@Test
public void testLoadString()
throws Exception
Expand Down
54 changes: 39 additions & 15 deletions src/test/java/com/amazon/ion/IonReaderCloseableTest.java
Original file line number Diff line number Diff line change
@@ -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;

import com.amazon.ion.system.IonReaderBuilder;
Expand Down Expand Up @@ -161,5 +148,42 @@ public void testBinaryReaderIsAtEndAfterClose() throws IOException {
IonReader reader = IonReaderBuilder.standard().build(binary);
assertAtEndAfterClose(reader);
}

// Malformed values that the text reader reads back from a save point: lobs and quoted field names.
private static final String[] MALFORMED_SAVE_POINT_VALUES = {
"{{invalid_blob}}",
"{{\"\\q\"}}",
"{{'''\\q'''}}",
"{\"\\q\":1}",
"{'\\q':1}",
"{a:{{invalid_blob}}}",
"{a:[{b:{{invalid_blob}}}]}",
"{id:12345,name:\"x\",items:[{kind:\"k\",type:\"t\",data:{a:{{invalid_blob}}}}]}",
};

@Test
public void testTextReaderClosesAfterMalformedValue() throws IOException {
IonSystem system = IonSystemBuilder.standard().build();
for (String ion : MALFORMED_SAVE_POINT_VALUES) {
byte[] bytes = ion.getBytes("UTF-8");
String message = null;
for (IonReader reader : new IonReader[] {
IonReaderBuilder.standard().build(ion),
IonReaderBuilder.standard().build(bytes),
}) {
IonException e = assertThrows("Expected IonException for " + ion, IonException.class,
() -> system.getLoader().load(reader));
message = e.getMessage();
reader.close();
assertNull("next() after close() should report end of stream for " + ion, reader.next());
}

// singleValue(byte[]) closes its reader in a finally block: the caller must get the value's own
// IonException, not one thrown by close().
IonException e = assertThrows("Expected IonException for " + ion, IonException.class,
() -> system.singleValue(bytes));
assertEquals("IonException from singleValue(byte[]) for " + ion, message, e.getMessage());
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@
import com.amazon.ion.system.IonSystemBuilder;
import com.amazon.ion.system.IonTextWriterBuilder;
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.EnumSource;

import java.io.ByteArrayInputStream;
import java.io.FilterInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
Expand Down Expand Up @@ -325,6 +325,56 @@ public void nonAsciiInBase64IsRejectedAsBefore(Source source) {
}
}

/**
* A malformed lob or quoted field name, which the reader skips first and reads back later from where it starts,
* throws an IonException and leaves the reader usable: reading it again throws an IonException again, the reader
* moves on to the next value, and close() succeeds. Before, the error left the reader reading from where the value
* starts, and these failed with an AssertionError (an IllegalArgumentException without -ea).
*/
@ParameterizedTest
@EnumSource(Source.class)
public void malformedValuesLeaveTheReaderUsable(Source source) throws IOException {
List<String> lobs = new ArrayList<>(Arrays.asList("{{invalid_blob}} 7", "{{\"x\\q\"}} 7"));
List<String> 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");
fieldNames.add("{'" + repeat("x", 2 * PAGE) + "\\q':1, b:2} 7");
}
for (String text : lobs) {
for (boolean inPieces : new boolean[] {false, true}) {
String label = source + ": " + shorten(text) + (inPieces ? " read with getBytes()" : " read with newBytes()");
IonReader reader = source.open(text);
Executable read = inPieces ? () -> reader.getBytes(new byte[3], 0, 3) : reader::newBytes;
assertTrue(IonType.isLob(reader.next()), label);
assertThrows(IonException.class, read, label);
assertThrows(IonException.class, read, label + ", read again");
assertEquals(IonType.INT, reader.next(), label + ", next value");
assertEquals(7, reader.intValue(), label + ", next value");
reader.close();
assertNull(reader.next(), label + ", after close()");
}
}
for (String text : fieldNames) {
String label = source + ": " + shorten(text);
IonReader reader = source.open(text);
assertEquals(IonType.STRUCT, reader.next(), label);
reader.stepIn();
assertThrows(IonException.class, reader::next, label);
assertThrows(IonException.class, reader::next, label + ", next() again");
reader.close();
assertNull(reader.next(), label + ", after close()");
}
}

private static String shorten(String text) {
return text.length() <= 40 ? escape(text) : escape(text.substring(0, 20)) + "... (" + text.length() + " characters)";
}

/**
* After byteSize(), which decodes and caches the whole blob, one getBytes() shorter than the blob returns its
* first bytes from that cache, and the reader then moves on to the next value. This does not reach the point
Expand Down
Loading