|
17 | 17 |
|
18 | 18 | import static org.assertj.core.api.Assertions.assertThat;
|
19 | 19 | import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
| 20 | +import static org.assertj.core.api.Assertions.fail; |
20 | 21 |
|
21 | 22 | import com.datastax.oss.driver.api.core.type.codec.TypeCodecs;
|
22 | 23 | import com.datastax.oss.driver.internal.SerializationHelper;
|
23 | 24 | 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; |
24 | 28 | import java.util.AbstractList;
|
25 | 29 | import java.util.ArrayList;
|
26 | 30 | import java.util.Arrays;
|
27 | 31 | import java.util.Collections;
|
28 | 32 | import java.util.List;
|
29 | 33 | import java.util.stream.Collectors;
|
| 34 | +import org.apache.commons.codec.DecoderException; |
| 35 | +import org.apache.commons.codec.binary.Hex; |
30 | 36 | import org.assertj.core.util.Lists;
|
31 | 37 | import org.junit.Test;
|
32 | 38 |
|
@@ -231,4 +237,20 @@ public int size() {
|
231 | 237 | CqlVector<Float> deserialized = SerializationHelper.serializeAndDeserialize(initial);
|
232 | 238 | assertThat(deserialized).isEqualTo(initial);
|
233 | 239 | }
|
| 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 | + } |
234 | 256 | }
|
0 commit comments