Skip to content

Commit d990f92

Browse files
authored
JAVA-3104: Do not eagerly pre-allocate array when deserializing CqlVector (#1714)
1 parent ebd63ff commit d990f92

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

core/src/main/java/com/datastax/oss/driver/api/core/data/CqlVector.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ private void readObject(ObjectInputStream stream) throws IOException, ClassNotFo
218218
stream.defaultReadObject();
219219

220220
int size = stream.readInt();
221-
list = new ArrayList<>(size);
221+
list = new ArrayList<>();
222222
for (int i = 0; i < size; i++) {
223223
list.add((T) stream.readObject());
224224
}

core/src/test/java/com/datastax/oss/driver/api/core/data/CqlVectorTest.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,22 @@
1717

1818
import static org.assertj.core.api.Assertions.assertThat;
1919
import static org.assertj.core.api.Assertions.assertThatThrownBy;
20+
import static org.assertj.core.api.Assertions.fail;
2021

2122
import com.datastax.oss.driver.api.core.type.codec.TypeCodecs;
2223
import com.datastax.oss.driver.internal.SerializationHelper;
2324
import com.datastax.oss.driver.shaded.guava.common.collect.Iterators;
25+
import java.io.ByteArrayInputStream;
26+
import java.io.ObjectInputStream;
27+
import java.io.ObjectStreamException;
2428
import java.util.AbstractList;
2529
import java.util.ArrayList;
2630
import java.util.Arrays;
2731
import java.util.Collections;
2832
import java.util.List;
2933
import java.util.stream.Collectors;
34+
import org.apache.commons.codec.DecoderException;
35+
import org.apache.commons.codec.binary.Hex;
3036
import org.assertj.core.util.Lists;
3137
import org.junit.Test;
3238

@@ -231,4 +237,20 @@ public int size() {
231237
CqlVector<Float> deserialized = SerializationHelper.serializeAndDeserialize(initial);
232238
assertThat(deserialized).isEqualTo(initial);
233239
}
240+
241+
@Test
242+
public void should_not_use_preallocate_serialized_size() throws DecoderException {
243+
// serialized CqlVector<Float>(1.0f, 2.5f, 3.0f) with size field adjusted to Integer.MAX_VALUE
244+
byte[] suspiciousBytes =
245+
Hex.decodeHex(
246+
"aced000573720042636f6d2e64617461737461782e6f73732e6472697665722e6170692e636f72652e646174612e43716c566563746f722453657269616c697a6174696f6e50726f78790000000000000001030000787077047fffffff7372000f6a6176612e6c616e672e466c6f6174daedc9a2db3cf0ec02000146000576616c7565787200106a6176612e6c616e672e4e756d62657286ac951d0b94e08b02000078703f8000007371007e0002402000007371007e00024040000078"
247+
.toCharArray());
248+
try {
249+
new ObjectInputStream(new ByteArrayInputStream(suspiciousBytes)).readObject();
250+
fail("Should not be able to deserialize bytes with incorrect size field");
251+
} catch (Exception e) {
252+
// check we fail to deserialize, rather than OOM
253+
assertThat(e).isInstanceOf(ObjectStreamException.class);
254+
}
255+
}
234256
}

0 commit comments

Comments
 (0)