Skip to content

Commit

Permalink
Adds checks for timestamp values with length 0.
Browse files Browse the repository at this point in the history
  • Loading branch information
tgregg committed Aug 6, 2024
1 parent 86861f9 commit f7cf5e2
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -750,6 +750,9 @@ public Timestamp timestampValue() {
}
prepareScalar();
peekIndex = valueMarker.startIndex;
if (peekIndex >= valueMarker.endIndex) {
throw new IonException("Timestamp value cannot have length 0.");
}
return minorVersion == 0 ? readTimestamp_1_0() : readTimestamp_1_1();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

package com.amazon.ion.impl;

import com.amazon.ion.IonCursor;
Expand All @@ -13,6 +12,7 @@
import java.io.ByteArrayInputStream;

import static com.amazon.ion.BitUtils.bytes;
import static com.amazon.ion.IonCursor.Event.START_SCALAR;
import static com.amazon.ion.impl.IonCursorTestUtilities.STANDARD_BUFFER_CONFIGURATION;
import static com.amazon.ion.impl.IonCursorTestUtilities.Expectation;
import static com.amazon.ion.impl.IonCursorTestUtilities.ExpectationProvider;
Expand Down Expand Up @@ -631,4 +631,37 @@ public void expectIncompleteAnnotationHeaderToFailCleanly() {
assertThrows(IonException.class, reader::nextValue);
reader.close();
}

@Test
public void timestampLengthZeroAtStreamEndFailsCleanly() {
try (
IonReaderContinuableCoreBinary reader = initializeReader(
// Note: a refillable reader would await more bytes before throwing. See the next test.
true,
0xE0, 0x01, 0x00, 0xEA, // IVM
0x6E, // Timestamp value, variable length.
0x80 // VarUInt 0 at stream end. This is an error because there is no length 0 timestamp.
)
) {
assertEquals(START_SCALAR, reader.nextValue());
assertThrows(IonException.class, reader::timestampValue);
}
}

@ParameterizedTest(name = "constructFromBytes={0}")
@ValueSource(booleans = {true, false})
public void timestampLengthZeroFailsCleanly(boolean constructFromBytes) {
try (
IonReaderContinuableCoreBinary reader = initializeReader(
constructFromBytes,
0xE0, 0x01, 0x00, 0xEA, // IVM
0x6E, // Timestamp value, variable length.
0x80, // VarUInt 0 at stream end. This is an error because there is no length 0 timestamp.
0x20 // Value byte to pad the input. A refillable reader expects at least this many bytes to compose a valid timestamp.
)
) {
assertEquals(START_SCALAR, reader.nextValue());
assertThrows(IonException.class, reader::timestampValue);
}
}
}

0 comments on commit f7cf5e2

Please sign in to comment.