From 2981cfb42be863b9001230f4a4998d745809ec40 Mon Sep 17 00:00:00 2001 From: Radhika Date: Sat, 14 Nov 2026 22:11:50 +0530 Subject: [PATCH 1/2] reject symbol table imports whose max_id overflows int The first local symbol ID is the sum of the imports' max_id values, accumulated in an int by LocalSymbolTableImports.prepBaseSids. Imports of max_id 2147483647 and 2147483640 wrap the total to 0, so local symbols land on the system symbol IDs in the text, DOM and binary readers. Reject a combined max_id that leaves no int-sized first local ID, and range check max_id before narrowing it in both import parsers. --- ...IonReaderContinuableApplicationBinary.java | 7 +- .../com/amazon/ion/impl/LocalSymbolTable.java | 25 +++---- .../ion/impl/LocalSymbolTableImports.java | 27 +++---- ...onReaderContinuableTopLevelBinaryTest.java | 73 +++++++++++++++++++ .../com/amazon/ion/impl/SymbolTableTest.java | 48 ++++++++---- 5 files changed, 132 insertions(+), 48 deletions(-) diff --git a/src/main/java/com/amazon/ion/impl/IonReaderContinuableApplicationBinary.java b/src/main/java/com/amazon/ion/impl/IonReaderContinuableApplicationBinary.java index 332a9efd46..bf2a61f94f 100644 --- a/src/main/java/com/amazon/ion/impl/IonReaderContinuableApplicationBinary.java +++ b/src/main/java/com/amazon/ion/impl/IonReaderContinuableApplicationBinary.java @@ -20,6 +20,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; @@ -869,7 +870,11 @@ private void readImportVersion() { private void readImportMaxId() { if (IonReaderContinuableApplicationBinary.super.getType() == IonType.INT && !IonReaderContinuableApplicationBinary.super.isNullValue()) { - maxId = intValue(); + BigInteger value = bigIntegerValue(); + if (value.bitLength() > 31) { + throw new IonException("Found a max_id that was too large to fit in an int."); + } + maxId = value.intValue(); } 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 22ac71689a..a664d88a42 100644 --- a/src/main/java/com/amazon/ion/impl/LocalSymbolTable.java +++ b/src/main/java/com/amazon/ion/impl/LocalSymbolTable.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.SystemSymbols.IMPORTS; @@ -37,6 +24,7 @@ import com.amazon.ion.SymbolToken; import com.amazon.ion.util.IonTextUtils; import java.io.IOException; +import java.math.BigInteger; import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; @@ -708,7 +696,12 @@ private static SymbolTable readOneImport(IonReader ionRep, case MAX_ID_SID: if (t == IonType.INT) { - maxid = ionRep.intValue(); + BigInteger value = ionRep.bigIntegerValue(); + if (value.bitLength() > 31) + { + throw new IonException("Found a max_id that was too large to fit in an int."); + } + maxid = value.intValue(); } 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 3f6efe9d5c..eee9cff48a 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/test/java/com/amazon/ion/impl/IonReaderContinuableTopLevelBinaryTest.java b/src/test/java/com/amazon/ion/impl/IonReaderContinuableTopLevelBinaryTest.java index d0d3e47a36..5e9b7ebd09 100644 --- a/src/test/java/com/amazon/ion/impl/IonReaderContinuableTopLevelBinaryTest.java +++ b/src/test/java/com/amazon/ion/impl/IonReaderContinuableTopLevelBinaryTest.java @@ -2128,6 +2128,79 @@ 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(); + } + @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 944ac5903e..f7e504c4c2 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,37 @@ 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(expected = IonException.class) public void testImportWithInexactMatchAndUndefinedMaxId() { registerSharedSymtab(IMPORTED_1_SERIALIZED); From db36d11ace3be6416aa022930fd26caf7948240f Mon Sep 17 00:00:00 2001 From: Tyler Gregg Date: Fri, 4 Dec 2026 00:41:29 +0000 Subject: [PATCH 2/2] Range check import version, avoid BigInteger allocation for max_id, and reject local symbol counts that overflow max_id - Read import version and max_id via a helper that checks getIntegerSize() and only allocates a BigInteger when the value may not fit in a long. - Reject a version that does not fit in an int instead of silently narrowing it. - Reject symbol tables whose imports' max_id plus local symbol count overflows int, in both the binary reader and LocalSymbolTable. - Avoid overflow in the binary reader's getSymbolToken when the max ID is Integer.MAX_VALUE. --- ...IonReaderContinuableApplicationBinary.java | 41 ++++-- .../com/amazon/ion/impl/LocalSymbolTable.java | 28 ++-- .../com/amazon/ion/impl/_Private_Utils.java | 47 +++++-- ...onReaderContinuableTopLevelBinaryTest.java | 132 ++++++++++++++++++ .../com/amazon/ion/impl/SymbolTableTest.java | 47 +++++++ 5 files changed, 264 insertions(+), 31 deletions(-) diff --git a/src/main/java/com/amazon/ion/impl/IonReaderContinuableApplicationBinary.java b/src/main/java/com/amazon/ion/impl/IonReaderContinuableApplicationBinary.java index bf2a61f94f..be097c9774 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; @@ -34,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; @@ -689,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); @@ -745,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) { @@ -861,20 +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()) { - BigInteger value = bigIntegerValue(); - if (value.bitLength() > 31) { - throw new IonException("Found a max_id that was too large to fit in an int."); - } - maxId = value.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 a664d88a42..fe3fc8c447 100644 --- a/src/main/java/com/amazon/ion/impl/LocalSymbolTable.java +++ b/src/main/java/com/amazon/ion/impl/LocalSymbolTable.java @@ -6,9 +6,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; +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; @@ -24,7 +26,6 @@ import com.amazon.ion.SymbolToken; import com.amazon.ion.util.IonTextUtils; import java.io.IOException; -import java.math.BigInteger; import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; @@ -159,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 @@ -541,6 +543,8 @@ int putSymbol(String symbolName) throw new ReadOnlyValueException(SymbolTable.class); } + checkLocalSymbolCount(mySymbolsCount + 1); + if (mySymbolsCount == mySymbolNames.length) { int newlen = mySymbolsCount * 2; @@ -567,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) @@ -690,18 +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) { - BigInteger value = ionRep.bigIntegerValue(); - if (value.bitLength() > 31) - { - throw new IonException("Found a max_id that was too large to fit in an int."); - } - maxid = value.intValue(); + maxid = _Private_Utils.intValueExact(ionRep, MAX_ID); } break; default: 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 751fb46489..80af57c45a 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 5e9b7ebd09..99867154ec 100644 --- a/src/test/java/com/amazon/ion/impl/IonReaderContinuableTopLevelBinaryTest.java +++ b/src/test/java/com/amazon/ion/impl/IonReaderContinuableTopLevelBinaryTest.java @@ -2201,6 +2201,138 @@ public void symbolTableImportWithMaxIdLargerThanIntFails(boolean constructFromBy 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 f7e504c4c2..455860459f 100644 --- a/src/test/java/com/amazon/ion/impl/SymbolTableTest.java +++ b/src/test/java/com/amazon/ion/impl/SymbolTableTest.java @@ -1035,6 +1035,53 @@ public void testImportsWithMaxIdLargerThanInt() 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);