diff --git a/src/main/java/com/amazon/ion/impl/IonReaderContinuableApplicationBinary.java b/src/main/java/com/amazon/ion/impl/IonReaderContinuableApplicationBinary.java index 332a9efd4..be097c977 100644 --- a/src/main/java/com/amazon/ion/impl/IonReaderContinuableApplicationBinary.java +++ b/src/main/java/com/amazon/ion/impl/IonReaderContinuableApplicationBinary.java @@ -2,6 +2,7 @@ // SPDX-License-Identifier: Apache-2.0 package com.amazon.ion.impl; +import com.amazon.ion.IntegerSize; import com.amazon.ion.IonCatalog; import com.amazon.ion.IonException; import com.amazon.ion.IonReader; @@ -20,6 +21,7 @@ import java.io.IOException; import java.io.InputStream; +import java.math.BigInteger; import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.Arrays; @@ -33,9 +35,11 @@ import static com.amazon.ion.SystemSymbols.IMPORTS_SID; import static com.amazon.ion.SystemSymbols.ION; import static com.amazon.ion.SystemSymbols.ION_SYMBOL_TABLE_SID; +import static com.amazon.ion.SystemSymbols.MAX_ID; import static com.amazon.ion.SystemSymbols.MAX_ID_SID; import static com.amazon.ion.SystemSymbols.NAME_SID; import static com.amazon.ion.SystemSymbols.SYMBOLS_SID; +import static com.amazon.ion.SystemSymbols.VERSION; import static com.amazon.ion.SystemSymbols.VERSION_SID; import static com.amazon.ion.impl._Private_Utils.safeEquals; @@ -688,8 +692,9 @@ String getSymbol(int sid) { * @return a SymbolToken. */ private SymbolToken getSymbolToken(int sid) { - int symbolTableSize = localSymbolMaxOffset + firstLocalSymbolId + 1; // +1 because the max ID is 0-indexed. - if (sid >= symbolTableSize) { + // Note: the max ID may be Integer.MAX_VALUE, so it is not safe to add 1 to it. + int maxId = localSymbolMaxOffset + firstLocalSymbolId; + if (sid > maxId) { throw new UnknownSymbolException(sid); } String text = getSymbolString(sid, imports, symbols); @@ -744,6 +749,10 @@ private void finishReadingSymbolTableStruct() { } if (newSymbols != null) { int numberOfNewSymbols = newSymbols.size(); + // The local symbol table's max ID (imports' max_id plus the number of local symbols) must fit in an int. + if (numberOfNewSymbols > Integer.MAX_VALUE - (firstLocalSymbolId + localSymbolMaxOffset)) { + throw new IonException("Symbol table has a combined max_id that is too large to fit in an int."); + } int numberOfAvailableSlots = symbols.length - (localSymbolMaxOffset + 1); int shortfall = numberOfNewSymbols - numberOfAvailableSlots; if (shortfall > 0) { @@ -860,16 +869,37 @@ private void readImportName() { state = State.READING_SYMBOL_TABLE_IMPORT_STRUCT; } + /** + * Reads the current Ion int, throwing if it does not fit in a Java int. A BigInteger is only materialized + * when the value may not fit in a long, so zero-padded encodings of small values are still accepted. + * @param fieldName the name of the field being read, for use in the error message. + * @return the value. + */ + private int intValueExact(String fieldName) { + if (getIntegerSize() == IntegerSize.BIG_INTEGER) { + BigInteger value = bigIntegerValue(); + if (value.bitLength() > 31) { + throw new IonException("Found a " + fieldName + " that was too large to fit in an int."); + } + return value.intValue(); + } + long value = longValue(); + if (value > Integer.MAX_VALUE || value < Integer.MIN_VALUE) { + throw new IonException("Found a " + fieldName + " that was too large to fit in an int."); + } + return (int) value; + } + private void readImportVersion() { if (IonReaderContinuableApplicationBinary.super.getType() == IonType.INT && !IonReaderContinuableApplicationBinary.super.isNullValue()) { - version = Math.max(1, intValue()); + version = Math.max(1, intValueExact(VERSION)); } state = State.READING_SYMBOL_TABLE_IMPORT_STRUCT; } private void readImportMaxId() { if (IonReaderContinuableApplicationBinary.super.getType() == IonType.INT && !IonReaderContinuableApplicationBinary.super.isNullValue()) { - maxId = intValue(); + maxId = intValueExact(MAX_ID); } state = State.READING_SYMBOL_TABLE_IMPORT_STRUCT; } diff --git a/src/main/java/com/amazon/ion/impl/LocalSymbolTable.java b/src/main/java/com/amazon/ion/impl/LocalSymbolTable.java index 22ac71689..fe3fc8c44 100644 --- a/src/main/java/com/amazon/ion/impl/LocalSymbolTable.java +++ b/src/main/java/com/amazon/ion/impl/LocalSymbolTable.java @@ -1,27 +1,16 @@ -/* - * 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; import static com.amazon.ion.SystemSymbols.IMPORTS; import static com.amazon.ion.SystemSymbols.IMPORTS_SID; import static com.amazon.ion.SystemSymbols.ION; import static com.amazon.ion.SystemSymbols.ION_SYMBOL_TABLE; +import static com.amazon.ion.SystemSymbols.MAX_ID; import static com.amazon.ion.SystemSymbols.MAX_ID_SID; import static com.amazon.ion.SystemSymbols.NAME_SID; import static com.amazon.ion.SystemSymbols.SYMBOLS_SID; +import static com.amazon.ion.SystemSymbols.VERSION; import static com.amazon.ion.SystemSymbols.VERSION_SID; import static com.amazon.ion.impl._Private_Utils.copyOf; import static com.amazon.ion.impl._Private_Utils.getSidForSymbolTableField; @@ -171,6 +160,7 @@ protected LocalSymbolTable(LocalSymbolTableImports imports, List symbols myImportsList = imports; myFirstLocalSid = myImportsList.getMaxId() + 1; + checkLocalSymbolCount(mySymbolsCount); // Copy locally declared symbols to mySymbolsMap // The initial size is chosen so that resizing is avoided. The default load factor is 0.75. Resizing @@ -553,6 +543,8 @@ int putSymbol(String symbolName) throw new ReadOnlyValueException(SymbolTable.class); } + checkLocalSymbolCount(mySymbolsCount + 1); + if (mySymbolsCount == mySymbolNames.length) { int newlen = mySymbolsCount * 2; @@ -579,6 +571,19 @@ int putSymbol(String symbolName) return sid; } + /** + * Ensures that the given number of local symbols can be added after the imports without the max ID overflowing. + */ + private void checkLocalSymbolCount(int numberOfLocalSymbols) + { + // Note: the imports' max ID is non-negative and less than Integer.MAX_VALUE (see LocalSymbolTableImports), so + // this subtraction cannot overflow. + if (numberOfLocalSymbols > Integer.MAX_VALUE - myImportsList.getMaxId()) + { + throw new IonException("Symbol table has a combined max_id that is too large to fit in an int."); + } + } + private static void putToMapIfNotThere(Map symbolsMap, String text, int sid) @@ -702,13 +707,13 @@ private static SymbolTable readOneImport(IonReader ionRep, case VERSION_SID: if (t == IonType.INT) { - version = ionRep.intValue(); + version = _Private_Utils.intValueExact(ionRep, VERSION); } break; case MAX_ID_SID: if (t == IonType.INT) { - maxid = ionRep.intValue(); + maxid = _Private_Utils.intValueExact(ionRep, MAX_ID); } break; default: diff --git a/src/main/java/com/amazon/ion/impl/LocalSymbolTableImports.java b/src/main/java/com/amazon/ion/impl/LocalSymbolTableImports.java index 3f6efe9d5..eee9cff48 100644 --- a/src/main/java/com/amazon/ion/impl/LocalSymbolTableImports.java +++ b/src/main/java/com/amazon/ion/impl/LocalSymbolTableImports.java @@ -1,22 +1,10 @@ -/* - * 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; import static com.amazon.ion.SymbolTable.UNKNOWN_SYMBOL_ID; +import com.amazon.ion.IonException; import com.amazon.ion.SymbolTable; import com.amazon.ion.SymbolToken; import java.util.Arrays; @@ -158,7 +146,14 @@ private static int prepBaseSids(int[] baseSids, SymbolTable[] imports) } baseSids[i] = total; - total += imports[i].getMaxId(); + int maxId = importedTable.getMaxId(); + // The first local symbol ID (total + 1) must also fit in an int. + if (maxId >= Integer.MAX_VALUE - total) + { + String message = "Symbol table imports have a combined max_id that is too large to fit in an int"; + throw new IonException(message); + } + total += maxId; } return total; diff --git a/src/main/java/com/amazon/ion/impl/_Private_Utils.java b/src/main/java/com/amazon/ion/impl/_Private_Utils.java index 751fb4648..80af57c45 100644 --- a/src/main/java/com/amazon/ion/impl/_Private_Utils.java +++ b/src/main/java/com/amazon/ion/impl/_Private_Utils.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; import static com.amazon.ion.SymbolTable.UNKNOWN_SYMBOL_ID; @@ -29,6 +16,7 @@ import static com.amazon.ion.SystemSymbols.VERSION_SID; import static com.amazon.ion.util.IonStreamUtils.isIonBinary; +import com.amazon.ion.IntegerSize; import com.amazon.ion.IonException; import com.amazon.ion.IonReader; import com.amazon.ion.IonStruct; @@ -47,6 +35,7 @@ import java.io.IOException; import java.io.InputStream; import java.io.PushbackInputStream; +import java.math.BigInteger; import java.nio.ByteBuffer; import java.nio.CharBuffer; import java.nio.charset.CharacterCodingException; @@ -1177,4 +1166,32 @@ public static SymbolTable newSubstituteSymtab(SymbolTable original, return new SubstituteSymbolTable(original, version, maxId); } + /** + * Reads the Ion int on which the given reader is positioned, throwing if it does not fit in a Java int. Unlike + * {@link IonReader#intValue()}, this never silently narrows the value. A {@link BigInteger} is only materialized + * when the reader reports that the value may not fit in a long, so zero-padded encodings of small values are + * still accepted. + * @param reader a reader positioned on a non-null Ion int. + * @param fieldName the name of the field being read, for use in the error message. + * @return the value. + * @throws IonException if the value does not fit in a Java int. + */ + static int intValueExact(IonReader reader, String fieldName) + { + if (reader.getIntegerSize() == IntegerSize.BIG_INTEGER) + { + BigInteger value = reader.bigIntegerValue(); + if (value.bitLength() > 31) + { + throw new IonException("Found a " + fieldName + " that was too large to fit in an int."); + } + return value.intValue(); + } + long value = reader.longValue(); + if (value > Integer.MAX_VALUE || value < Integer.MIN_VALUE) + { + throw new IonException("Found a " + fieldName + " that was too large to fit in an int."); + } + return (int) value; + } } diff --git a/src/test/java/com/amazon/ion/impl/IonReaderContinuableTopLevelBinaryTest.java b/src/test/java/com/amazon/ion/impl/IonReaderContinuableTopLevelBinaryTest.java index d0d3e47a3..99867154e 100644 --- a/src/test/java/com/amazon/ion/impl/IonReaderContinuableTopLevelBinaryTest.java +++ b/src/test/java/com/amazon/ion/impl/IonReaderContinuableTopLevelBinaryTest.java @@ -2128,6 +2128,211 @@ public void symbolTableWithMultipleImportsCorrectlyAssignsImportLocations(boolea closeAndCount(); } + @ParameterizedTest(name = "constructFromBytes={0}") + @ValueSource(booleans = {true, false}) + public void symbolTableImportsWithCombinedMaxIdLargerThanIntFails(boolean constructFromBytes) throws Exception { + reader = readerFor( + (writer, out) -> { + SymbolTable systemTable = SharedSymbolTable.getSystemSymbolTable(1); + writer.addTypeAnnotationSymbol(systemTable.findSymbol("$ion_symbol_table")); + writer.stepIn(IonType.STRUCT); + writer.setFieldNameSymbol(systemTable.findSymbol("imports")); + writer.stepIn(IonType.LIST); + writer.stepIn(IonType.STRUCT); + writer.setFieldNameSymbol(systemTable.findSymbol("name")); + writer.writeString("foo"); + writer.setFieldNameSymbol(systemTable.findSymbol("version")); + writer.writeInt(1); + writer.setFieldNameSymbol(systemTable.findSymbol("max_id")); + writer.writeInt(Integer.MAX_VALUE); + writer.stepOut(); + writer.stepIn(IonType.STRUCT); + writer.setFieldNameSymbol(systemTable.findSymbol("name")); + writer.writeString("bar"); + writer.setFieldNameSymbol(systemTable.findSymbol("version")); + writer.writeInt(1); + writer.setFieldNameSymbol(systemTable.findSymbol("max_id")); + // With the 9 system symbols, the total is 2^32, which wraps to 0 in an int. + writer.writeInt(Integer.MAX_VALUE - 7); + writer.stepOut(); + writer.stepOut(); + writer.setFieldNameSymbol(systemTable.findSymbol("symbols")); + writer.stepIn(IonType.LIST); + writer.writeString("abc"); + writer.stepOut(); + writer.stepOut(); + writer.writeSymbolToken(1); // $ion, which would otherwise read as abc + }, + constructFromBytes + ); + assertThrows(IonException.class, () -> reader.next()); + reader.close(); + } + + @ParameterizedTest(name = "constructFromBytes={0}") + @ValueSource(booleans = {true, false}) + public void symbolTableImportWithMaxIdLargerThanIntFails(boolean constructFromBytes) throws Exception { + reader = readerFor( + (writer, out) -> { + SymbolTable systemTable = SharedSymbolTable.getSystemSymbolTable(1); + writer.addTypeAnnotationSymbol(systemTable.findSymbol("$ion_symbol_table")); + writer.stepIn(IonType.STRUCT); + writer.setFieldNameSymbol(systemTable.findSymbol("imports")); + writer.stepIn(IonType.LIST); + writer.stepIn(IonType.STRUCT); + writer.setFieldNameSymbol(systemTable.findSymbol("name")); + writer.writeString("foo"); + writer.setFieldNameSymbol(systemTable.findSymbol("version")); + writer.writeInt(1); + writer.setFieldNameSymbol(systemTable.findSymbol("max_id")); + writer.writeInt((1L << 32) + 9); // Would be narrowed to 9. + writer.stepOut(); + writer.stepOut(); + writer.setFieldNameSymbol(systemTable.findSymbol("symbols")); + writer.stepIn(IonType.LIST); + writer.writeString("abc"); + writer.stepOut(); + writer.stepOut(); + writer.writeSymbolToken(19); // Within the import's range, but would read as abc + }, + constructFromBytes + ); + assertThrows(IonException.class, () -> reader.next()); + reader.close(); + } + + /** + * Writes a local symbol table that imports a single shared table named "foo" and declares the given symbols. + */ + private static void writeSymbolTableWithOneImport( + _Private_IonRawWriter writer, + long version, + long maxId, + String... symbols + ) throws IOException { + SymbolTable systemTable = SharedSymbolTable.getSystemSymbolTable(1); + writer.addTypeAnnotationSymbol(systemTable.findSymbol("$ion_symbol_table")); + writer.stepIn(IonType.STRUCT); + writer.setFieldNameSymbol(systemTable.findSymbol("imports")); + writer.stepIn(IonType.LIST); + writer.stepIn(IonType.STRUCT); + writer.setFieldNameSymbol(systemTable.findSymbol("name")); + writer.writeString("foo"); + writer.setFieldNameSymbol(systemTable.findSymbol("version")); + writer.writeInt(version); + writer.setFieldNameSymbol(systemTable.findSymbol("max_id")); + writer.writeInt(maxId); + writer.stepOut(); + writer.stepOut(); + writer.setFieldNameSymbol(systemTable.findSymbol("symbols")); + writer.stepIn(IonType.LIST); + for (String symbol : symbols) { + writer.writeString(symbol); + } + writer.stepOut(); + writer.stepOut(); + } + + @ParameterizedTest(name = "constructFromBytes={0}") + @ValueSource(booleans = {true, false}) + public void symbolTableImportWithVersionLargerThanIntFails(boolean constructFromBytes) throws Exception { + reader = readerFor( + (writer, out) -> { + writeSymbolTableWithOneImport(writer, (1L << 32) + 2, 1, "abc"); // Would be narrowed to version 2. + writer.writeSymbolToken(11); + }, + constructFromBytes + ); + assertThrows(IonException.class, () -> reader.next()); + reader.close(); + } + + @ParameterizedTest(name = "constructFromBytes={0}") + @ValueSource(booleans = {true, false}) + public void symbolTableWithLocalSymbolsThatOverflowMaxIdFails(boolean constructFromBytes) throws Exception { + reader = readerFor( + (writer, out) -> { + // With the 9 system symbols, the imports end at Integer.MAX_VALUE - 1, leaving room for only one + // local symbol. + writeSymbolTableWithOneImport(writer, 1, Integer.MAX_VALUE - 10, "abc", "def"); + writer.writeSymbolToken(Integer.MAX_VALUE); + }, + constructFromBytes + ); + assertThrows(IonException.class, () -> reader.next()); + reader.close(); + } + + @ParameterizedTest(name = "constructFromBytes={0}") + @ValueSource(booleans = {true, false}) + public void appendedLocalSymbolsThatOverflowMaxIdFails(boolean constructFromBytes) throws Exception { + reader = readerFor( + (writer, out) -> { + writeSymbolTableWithOneImport(writer, 1, Integer.MAX_VALUE - 10, "abc"); + writer.writeSymbolToken(Integer.MAX_VALUE); + SymbolTable systemTable = SharedSymbolTable.getSystemSymbolTable(1); + writer.addTypeAnnotationSymbol(systemTable.findSymbol("$ion_symbol_table")); + writer.stepIn(IonType.STRUCT); + writer.setFieldNameSymbol(systemTable.findSymbol("imports")); + writer.writeSymbolToken(systemTable.findSymbol("$ion_symbol_table")); + writer.setFieldNameSymbol(systemTable.findSymbol("symbols")); + writer.stepIn(IonType.LIST); + writer.writeString("def"); + writer.stepOut(); + writer.stepOut(); + writer.writeSymbolToken(Integer.MAX_VALUE); + }, + constructFromBytes + ); + assertSequence( + next(IonType.SYMBOL), stringValue("abc") + ); + assertThrows(IonException.class, () -> reader.next()); + reader.close(); + } + + @ParameterizedTest(name = "constructFromBytes={0}") + @ValueSource(booleans = {true, false}) + public void localSymbolWithMaxIntIdCanBeRead(boolean constructFromBytes) throws Exception { + reader = readerFor( + (writer, out) -> { + // With the 9 system symbols, the imports end at Integer.MAX_VALUE - 1. + writeSymbolTableWithOneImport(writer, 1, Integer.MAX_VALUE - 10, "abc"); + writer.writeSymbolToken(Integer.MAX_VALUE); + }, + constructFromBytes + ); + assertSequence( + next(IonType.SYMBOL), stringValue("abc"), symbolValue("abc"), + next(null) + ); + closeAndCount(); + } + + @ParameterizedTest(name = "constructFromBytes={0}") + @ValueSource(booleans = {true, false}) + public void symbolTableImportWithZeroPaddedMaxIdLargerThanLong(boolean constructFromBytes) throws Exception { + reader = readerFor( + constructFromBytes, + 0xEE, 0x9C, // Annotation wrapper, length 28. + 0x81, 0x83, // $ion_symbol_table + 0xDE, 0x98, // Struct, length 24. + 0x86, // imports: + 0xBE, 0x91, // List, length 17. + 0xDE, 0x8F, // Struct, length 15. + 0x84, 0x81, 'a', // name: "a" + // max_id: 1, zero-padded to 10 bytes, which is longer than a long. + 0x88, 0x2A, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, + 0x87, 0xB2, 0x81, 'x', // symbols: ["x"] + 0x71, 0x0B // $11 (x) + ); + assertSequence( + next(IonType.SYMBOL), stringValue("x"), + next(null) + ); + closeAndCount(); + } + @ParameterizedTest(name = "constructFromBytes={0}") @ValueSource(booleans = {true, false}) public void symbolTableSnapshotImplementsBasicMethods(boolean constructFromBytes) throws Exception { diff --git a/src/test/java/com/amazon/ion/impl/SymbolTableTest.java b/src/test/java/com/amazon/ion/impl/SymbolTableTest.java index 944ac5903..455860459 100644 --- a/src/test/java/com/amazon/ion/impl/SymbolTableTest.java +++ b/src/test/java/com/amazon/ion/impl/SymbolTableTest.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; import static com.amazon.ion.SymbolTable.UNKNOWN_SYMBOL_ID; @@ -1017,6 +1004,84 @@ public void testImportWithBadMaxId(SymbolTable expected, String maxIdText) assertSame(expected, v.getSymbolTable().getImportedTables()[0]); } + @Test + public void testImportsWithMaxIdLargerThanInt() + { + // With the system symbols, the combined max_id is 2^32, which wraps to 0 + // in an int and would put the local symbols at $1 through $4. + badValue(LocalSymbolTablePrefix + + "{" + + " imports:[{name:\"a\", version:1, max_id:2147483647}," + + " {name:\"b\", version:1, max_id:2147483640}]," + + " symbols:['''x''', '''y''', '''z''', '''local''']" + + "}\n" + + "$4"); + + badValue(LocalSymbolTablePrefix + + "{" + + " imports:[{name:\"a\", version:1, max_id:4294967305}]," + + " symbols:['''local''']" + + "}\n" + + "$19"); + + // The largest import range that still leaves room for a local symbol. + IonValue v = oneValue(LocalSymbolTablePrefix + + "{" + + " imports:[{name:\"a\", version:1," + + " max_id:" + (Integer.MAX_VALUE - systemMaxId() - 1) + "}]," + + " symbols:['''local''']" + + "}\n" + + "null"); + assertEquals(Integer.MAX_VALUE - 1, v.getSymbolTable().getImportedMaxId()); + } + + @Test + public void testLocalSymbolsThatOverflowMaxId() + { + int importMaxId = Integer.MAX_VALUE - systemMaxId() - 1; + + // The imports end at Integer.MAX_VALUE - 1, which leaves room for one local symbol only. + badValue(LocalSymbolTablePrefix + + "{" + + " imports:[{name:\"a\", version:1, max_id:" + importMaxId + "}]," + + " symbols:['''x''', '''y''']" + + "}\n" + + "null"); + + // The same, but the second symbol is appended to the existing local symbol table. + badValue(LocalSymbolTablePrefix + + "{" + + " imports:[{name:\"a\", version:1, max_id:" + importMaxId + "}]," + + " symbols:['''x''']" + + "}\n" + + LocalSymbolTablePrefix + + "{" + + " imports:$ion_symbol_table," + + " symbols:['''y''']" + + "}\n" + + "null"); + + IonValue v = oneValue(LocalSymbolTablePrefix + + "{" + + " imports:[{name:\"a\", version:1, max_id:" + importMaxId + "}]," + + " symbols:['''x''']" + + "}\n" + + "null"); + assertEquals(Integer.MAX_VALUE, v.getSymbolTable().getMaxId()); + } + + @Test + public void testImportWithVersionLargerThanInt() + { + // 4294967298 would be narrowed to version 2. + badValue(LocalSymbolTablePrefix + + "{" + + " imports:[{name:\"a\", version:4294967298, max_id:1}]," + + " symbols:['''local''']" + + "}\n" + + "null"); + } + @Test(expected = IonException.class) public void testImportWithInexactMatchAndUndefinedMaxId() { registerSharedSymtab(IMPORTED_1_SERIALIZED);