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
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
}
Expand Down
39 changes: 22 additions & 17 deletions src/main/java/com/amazon/ion/impl/LocalSymbolTable.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -171,6 +160,7 @@ protected LocalSymbolTable(LocalSymbolTableImports imports, List<String> 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
Expand Down Expand Up @@ -553,6 +543,8 @@ int putSymbol(String symbolName)
throw new ReadOnlyValueException(SymbolTable.class);
}

checkLocalSymbolCount(mySymbolsCount + 1);

if (mySymbolsCount == mySymbolNames.length)
{
int newlen = mySymbolsCount * 2;
Expand All @@ -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<String, Integer> symbolsMap,
String text,
int sid)
Expand Down Expand Up @@ -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:
Expand Down
27 changes: 11 additions & 16 deletions src/main/java/com/amazon/ion/impl/LocalSymbolTableImports.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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)
Comment thread
tgregg marked this conversation as resolved.
Resolved
{
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;
Expand Down
47 changes: 32 additions & 15 deletions src/main/java/com/amazon/ion/impl/_Private_Utils.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.impl;

import static com.amazon.ion.SymbolTable.UNKNOWN_SYMBOL_ID;
Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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;
}
}
Loading
Loading